refactor scripts

This commit is contained in:
Vyacheslav Matyukhin 2022-09-20 00:55:32 +04:00
parent 8e13caae84
commit 4f90dccdf1
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
8 changed files with 47 additions and 139 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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;

View File

@ -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() : ""
);
};

View File

@ -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("<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() : ""
);
run(src, { output: options.output, sampleCount });

View File

@ -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());

View File

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