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.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.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);