benchmarking and testing scripts for squiggle-lang
This commit is contained in:
parent
7a220eae22
commit
a78797bb9c
25
packages/squiggle-lang/scripts/README.md
Normal file
25
packages/squiggle-lang/scripts/README.md
Normal file
|
@ -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.
|
27
packages/squiggle-lang/scripts/bench-map-reduce.js
Executable file
27
packages/squiggle-lang/scripts/bench-map-reduce.js
Executable file
|
@ -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);
|
||||||
|
}
|
27
packages/squiggle-lang/scripts/bench-map.js
Executable file
27
packages/squiggle-lang/scripts/bench-map.js
Executable file
|
@ -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);
|
||||||
|
}
|
57
packages/squiggle-lang/scripts/run-file.mjs
Executable file
57
packages/squiggle-lang/scripts/run-file.mjs
Executable file
|
@ -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("<string>");
|
||||||
|
|
||||||
|
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() : ""
|
||||||
|
);
|
18
packages/squiggle-lang/scripts/run.js
Executable file
18
packages/squiggle-lang/scripts/run.js
Executable file
|
@ -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());
|
Loading…
Reference in New Issue
Block a user