From a78797bb9c63f73f2744a30d588f76371771c998 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sun, 18 Sep 2022 19:14:32 +0400 Subject: [PATCH] benchmarking and testing scripts for squiggle-lang --- packages/squiggle-lang/scripts/README.md | 25 ++++++++ .../squiggle-lang/scripts/bench-map-reduce.js | 27 +++++++++ packages/squiggle-lang/scripts/bench-map.js | 27 +++++++++ packages/squiggle-lang/scripts/run-file.mjs | 57 +++++++++++++++++++ packages/squiggle-lang/scripts/run.js | 18 ++++++ 5 files changed, 154 insertions(+) create mode 100644 packages/squiggle-lang/scripts/README.md create mode 100755 packages/squiggle-lang/scripts/bench-map-reduce.js create mode 100755 packages/squiggle-lang/scripts/bench-map.js create mode 100755 packages/squiggle-lang/scripts/run-file.mjs create mode 100755 packages/squiggle-lang/scripts/run.js diff --git a/packages/squiggle-lang/scripts/README.md b/packages/squiggle-lang/scripts/README.md new file mode 100644 index 00000000..df7956ae --- /dev/null +++ b/packages/squiggle-lang/scripts/README.md @@ -0,0 +1,25 @@ +Various scripts used for development, benchmarking and testing. + +None of these are bundled in the NPM package yet. + +# run.js + +`scripts/run.js` allows quick testing of Squiggle programs: + +``` +$ ./scripts/run.js '2+2' +Running 2+2 +Ok 4 +@{__result__: 4} +``` + +# run-file.mjs + +`scripts/run-file.mjs` can be used to run and benchmark squiggle scripts stored in files: + +``` +$ ./scripts/run-file.mjs ./path/to/file.squiggle +Time: 3.18 Ok +``` + +To see the result and bindings, add the `-o` or `--output` flag. diff --git a/packages/squiggle-lang/scripts/bench-map-reduce.js b/packages/squiggle-lang/scripts/bench-map-reduce.js new file mode 100755 index 00000000..ffada066 --- /dev/null +++ b/packages/squiggle-lang/scripts/bench-map-reduce.js @@ -0,0 +1,27 @@ +#!/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.js b/packages/squiggle-lang/scripts/bench-map.js new file mode 100755 index 00000000..befa1a7b --- /dev/null +++ b/packages/squiggle-lang/scripts/bench-map.js @@ -0,0 +1,27 @@ +#!/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/run-file.mjs b/packages/squiggle-lang/scripts/run-file.mjs new file mode 100755 index 00000000..90fa6203 --- /dev/null +++ b/packages/squiggle-lang/scripts/run-file.mjs @@ -0,0 +1,57 @@ +#!/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 = (s) => `\x1b[31m${s}\x1b[0m`; +const green = (s) => `\x1b[32m${s}\x1b[0m`; + +const program = new Command(); + +program.option("-o, --output"); +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 t = 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(t), + result.tag === "Error" ? red(result.tag) : green(result.tag), + result.tag === "Error" ? result.value.toString() : "" +); diff --git a/packages/squiggle-lang/scripts/run.js b/packages/squiggle-lang/scripts/run.js new file mode 100755 index 00000000..e77b953b --- /dev/null +++ b/packages/squiggle-lang/scripts/run.js @@ -0,0 +1,18 @@ +#!/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.asValue().toString());