squiggle/packages/squiggle-lang/benchmark/lib.ts

43 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2022-04-26 18:26:02 +00:00
import { run, squiggleExpression, errorValueToString } from "../src/js/index";
2022-04-26 20:38:17 +00:00
import * as chalk from "chalk";
2022-04-26 18:26:02 +00:00
let testRun = (x: string): squiggleExpression => {
let result = run(x, { sampleCount: 100, xyPointLength: 100 });
if (result.tag === "Ok") {
return result.value;
} else {
throw Error(
"Expected squiggle expression to evaluate but got error: " +
errorValueToString(result.value)
);
}
};
export function test(name: string, fn: () => void) {
console.log(chalk.cyan.bold(name));
2022-04-26 18:26:02 +00:00
fn();
}
export function expectEqual(expression1: string, expression2: string) {
let result1 = testRun(expression1);
let result2 = testRun(expression2);
if (result1.tag === "number" && result2.tag === "number") {
2022-04-26 20:38:17 +00:00
let logloss = Math.log(Math.abs(result1.value - result2.value));
let isBadLogless = logloss > 1;
console.log(chalk.blue(`${expression1} = ${expression2}`));
console.log(`${result1.value} = ${result2.value}`);
console.log(
`logloss = ${
isBadLogless
? chalk.red(logloss.toFixed(2))
: chalk.green(logloss.toFixed(2))
}`
);
console.log();
} else {
throw Error(
`Expected both to be number, but got ${result1.tag} and ${result2.tag}`
);
2022-04-26 18:26:02 +00:00
}
}