From 4f90dccdf12494a2f97b2b615592cb02136483ed Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Tue, 20 Sep 2022 00:55:32 +0400 Subject: [PATCH] refactor scripts --- .../squiggle-lang/scripts/bench-map-reduce.js | 27 ------------ .../scripts/bench-map-reduce.mjs | 11 +---- packages/squiggle-lang/scripts/bench-map.js | 27 ------------ packages/squiggle-lang/scripts/bench-map.mjs | 11 +---- packages/squiggle-lang/scripts/lib.mjs | 41 +++++++++++++++++++ packages/squiggle-lang/scripts/run-file.mjs | 39 +----------------- packages/squiggle-lang/scripts/run.js | 18 -------- packages/squiggle-lang/scripts/run.mjs | 12 +----- 8 files changed, 47 insertions(+), 139 deletions(-) delete mode 100755 packages/squiggle-lang/scripts/bench-map-reduce.js delete mode 100755 packages/squiggle-lang/scripts/bench-map.js create mode 100644 packages/squiggle-lang/scripts/lib.mjs delete mode 100755 packages/squiggle-lang/scripts/run.js diff --git a/packages/squiggle-lang/scripts/bench-map-reduce.js b/packages/squiggle-lang/scripts/bench-map-reduce.js deleted file mode 100755 index ffada066..00000000 --- a/packages/squiggle-lang/scripts/bench-map-reduce.js +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env node -const s = require("@quri/squiggle-lang"); - -const measure = (cb, times = 1) => { - const t1 = new Date(); - - for (let i = 1; i <= times; i++) { - cb(); - } - const t2 = new Date(); - return (t2 - t1) / 1000; -}; - -const maxP = 5; - -for (let p = 0; p <= maxP; p++) { - const size = Math.pow(10, p); - const prj = s.SqProject.create(); - prj.setSource( - "main", - `List.upTo(1, ${size}) |> map({|x| List.upTo(1, 100) |> reduce(0, {|a,b|a+b})})` - ); - const t = measure(() => { - prj.run("main"); - }); - console.log(`1e${p}`, "\t", t); -} diff --git a/packages/squiggle-lang/scripts/bench-map-reduce.mjs b/packages/squiggle-lang/scripts/bench-map-reduce.mjs index 5dfbca0f..82c6d8e8 100755 --- a/packages/squiggle-lang/scripts/bench-map-reduce.mjs +++ b/packages/squiggle-lang/scripts/bench-map-reduce.mjs @@ -1,15 +1,6 @@ #!/usr/bin/env node import { SqProject } from "@quri/squiggle-lang"; - -const measure = (cb, times = 1) => { - const t1 = new Date(); - - for (let i = 1; i <= times; i++) { - cb(); - } - const t2 = new Date(); - return (t2 - t1) / 1000; -}; +import { measure } from "./lib.mjs"; const maxP = 5; diff --git a/packages/squiggle-lang/scripts/bench-map.js b/packages/squiggle-lang/scripts/bench-map.js deleted file mode 100755 index befa1a7b..00000000 --- a/packages/squiggle-lang/scripts/bench-map.js +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env node -const s = require("@quri/squiggle-lang"); - -const measure = (cb, times = 1) => { - const t1 = new Date(); - - for (let i = 1; i <= times; i++) { - cb(); - } - const t2 = new Date(); - return (t2 - t1) / 1000; -}; - -const maxP = 7; - -for (let p = 0; p <= maxP; p++) { - const size = Math.pow(10, p); - const project = s.SqProject.create(); - project.setSource("list", `l = List.upTo(1,${size})`); - project.run("list"); - project.setSource("map", "l |> map({|x| x})"); - project.setContinues("map", ["list"]); - const time = measure(() => { - project.run("map"); - }); - console.log(`1e${p}`, "\t", time); -} diff --git a/packages/squiggle-lang/scripts/bench-map.mjs b/packages/squiggle-lang/scripts/bench-map.mjs index c7a00477..5959a2fb 100755 --- a/packages/squiggle-lang/scripts/bench-map.mjs +++ b/packages/squiggle-lang/scripts/bench-map.mjs @@ -1,15 +1,6 @@ #!/usr/bin/env node import { SqProject } from "@quri/squiggle-lang"; - -const measure = (cb, times = 1) => { - const t1 = new Date(); - - for (let i = 1; i <= times; i++) { - cb(); - } - const t2 = new Date(); - return (t2 - t1) / 1000; -}; +import { measure } from "./lib.mjs"; const maxP = 7; diff --git a/packages/squiggle-lang/scripts/lib.mjs b/packages/squiggle-lang/scripts/lib.mjs new file mode 100644 index 00000000..ffa1d84f --- /dev/null +++ b/packages/squiggle-lang/scripts/lib.mjs @@ -0,0 +1,41 @@ +import { SqProject } from "@quri/squiggle-lang"; + +export const measure = (cb, times = 1) => { + const t1 = new Date(); + + for (let i = 1; i <= times; i++) { + cb(); + } + const t2 = new Date(); + return (t2 - t1) / 1000; +}; + +export const red = (str) => `\x1b[31m${str}\x1b[0m`; +export const green = (str) => `\x1b[32m${str}\x1b[0m`; + +export const run = (src, { output, sampleCount }) => { + const project = SqProject.create(); + if (sampleCount) { + project.setEnvironment({ + sampleCount: Number(sampleCount), + xyPointLength: Number(sampleCount), + }); + } + project.setSource("main", src); + const time = measure(() => project.run("main")); + + const bindings = project.getBindings("main"); + const result = project.getResult("main"); + + if (output) { + console.log("Result:", result.tag, result.value.toString()); + console.log("Bindings:", bindings.toString()); + } + + console.log( + "Time:", + String(time), + result.tag === "Error" ? red(result.tag) : green(result.tag), + result.tag === "Error" ? result.value.toString() : "" + ); +}; diff --git a/packages/squiggle-lang/scripts/run-file.mjs b/packages/squiggle-lang/scripts/run-file.mjs index 23041a5a..21d9de2d 100755 --- a/packages/squiggle-lang/scripts/run-file.mjs +++ b/packages/squiggle-lang/scripts/run-file.mjs @@ -1,21 +1,9 @@ #!/usr/bin/env node -import { SqProject } from "@quri/squiggle-lang"; import fs from "fs"; import { Command } from "commander"; -const measure = (cb, times = 1) => { - const t1 = new Date(); - - for (let i = 1; i <= times; i++) { - cb(); - } - const t2 = new Date(); - return (t2 - t1) / 1000; -}; - -const red = (str) => `\x1b[31m${str}\x1b[0m`; -const green = (str) => `\x1b[32m${str}\x1b[0m`; +import { run } from "./lib.mjs"; const program = new Command(); @@ -24,34 +12,11 @@ program.arguments(""); const options = program.parse(process.argv); -const project = SqProject.create(); const sampleCount = process.env.SAMPLE_COUNT; -if (sampleCount) { - project.setEnvironment({ - sampleCount: Number(sampleCount), - xyPointLength: Number(sampleCount), - }); -} const src = fs.readFileSync(program.args[0], "utf-8"); if (!src) { throw new Error("Expected src"); } -project.setSource("main", src); -const time = measure(() => project.run("main")); - -const bindings = project.getBindings("main"); -const result = project.getResult("main"); - -if (options.output) { - console.log("Result:", result.tag, result.value.toString()); - console.log("Bindings:", bindings.toString()); -} - -console.log( - "Time:", - String(time), - result.tag === "Error" ? red(result.tag) : green(result.tag), - result.tag === "Error" ? result.value.toString() : "" -); +run(src, { output: options.output, sampleCount }); diff --git a/packages/squiggle-lang/scripts/run.js b/packages/squiggle-lang/scripts/run.js deleted file mode 100755 index 97ae76bc..00000000 --- a/packages/squiggle-lang/scripts/run.js +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env node -const s = require("@quri/squiggle-lang"); - -const p = s.SqProject.create(); - -const src = process.argv[2]; -if (!src) { - throw new Error("Expected src"); -} -console.log(`Running ${src}`); -p.setSource("a", src); -p.run("a"); - -const result = p.getResult("a"); -console.log(result.tag, result.value.toString()); - -const bindings = p.getBindings("a"); -console.log(bindings.toString()); diff --git a/packages/squiggle-lang/scripts/run.mjs b/packages/squiggle-lang/scripts/run.mjs index 6f842eef..bc17314b 100755 --- a/packages/squiggle-lang/scripts/run.mjs +++ b/packages/squiggle-lang/scripts/run.mjs @@ -1,18 +1,10 @@ #!/usr/bin/env node -import { SqProject } from "@quri/squiggle-lang"; - -const project = SqProject.create(); +import { run } from "./lib.mjs"; const src = process.argv[2]; if (!src) { throw new Error("Expected src"); } console.log(`Running ${src}`); -project.setSource("a", src); -project.run("a"); -const result = project.getResult("a"); -console.log(result.tag, result.value.toString()); - -const bindings = project.getBindings("a"); -console.log(bindings.asValue().toString()); +run(src);