Merge pull request #1140 from quantified-uncertainty/scripts
benchmarking and testing scripts for squiggle-lang
This commit is contained in:
		
						commit
						2aaf6816f6
					
				
							
								
								
									
										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.mjs
 | 
			
		||||
 | 
			
		||||
`scripts/run.mjs` allows quick testing of Squiggle programs:
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
$ ./scripts/run.mjs '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.mjs
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										27
									
								
								packages/squiggle-lang/scripts/bench-map-reduce.mjs
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,27 @@
 | 
			
		|||
#!/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;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const maxP = 5;
 | 
			
		||||
 | 
			
		||||
for (let p = 0; p <= maxP; p++) {
 | 
			
		||||
  const size = Math.pow(10, p);
 | 
			
		||||
  const project = SqProject.create();
 | 
			
		||||
  project.setSource(
 | 
			
		||||
    "main",
 | 
			
		||||
    `List.upTo(1, ${size}) |> map({|x| List.upTo(1, 100) |> reduce(0, {|a,b|a+b})})`
 | 
			
		||||
  );
 | 
			
		||||
  const time = measure(() => {
 | 
			
		||||
    project.run("main");
 | 
			
		||||
  });
 | 
			
		||||
  console.log(`1e${p}`, "\t", time);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								packages/squiggle-lang/scripts/bench-map.mjs
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										27
									
								
								packages/squiggle-lang/scripts/bench-map.mjs
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,27 @@
 | 
			
		|||
#!/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;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const maxP = 7;
 | 
			
		||||
 | 
			
		||||
for (let p = 0; p <= maxP; p++) {
 | 
			
		||||
  const size = Math.pow(10, p);
 | 
			
		||||
  const project = 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 = (str) => `\x1b[31m${str}\x1b[0m`;
 | 
			
		||||
const green = (str) => `\x1b[32m${str}\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 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() : ""
 | 
			
		||||
);
 | 
			
		||||
							
								
								
									
										18
									
								
								packages/squiggle-lang/scripts/run.mjs
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										18
									
								
								packages/squiggle-lang/scripts/run.mjs
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
#!/usr/bin/env node
 | 
			
		||||
import { SqProject } from "@quri/squiggle-lang";
 | 
			
		||||
 | 
			
		||||
const project = SqProject.create();
 | 
			
		||||
 | 
			
		||||
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());
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user