Skip to content

Instantly share code, notes, and snippets.

@TrueWill
Created May 23, 2020 22:15
Show Gist options
  • Save TrueWill/c9f4427cafbfaa620feb5480f73b474a to your computer and use it in GitHub Desktop.
Save TrueWill/c9f4427cafbfaa620feb5480f73b474a to your computer and use it in GitHub Desktop.
Tzientist demo
import * as scientist from 'tzientist';
// Gregory-Leibniz series to calculate Pi
// from https://stackoverflow.com/questions/39574989/calculating-pi-in-javascript-using-gregory-leibniz-series
function oldPi(): number {
const n = 4000;
let v = 0;
for (let i = 1; i <= n; i += 4) {
v += 1 / i - 1 / (i + 2);
}
return 4 * v;
}
function newPi(): number {
return Math.PI;
}
function publish(results: scientist.Results<[], number>): void {
if (results.candidateResult !== results.controlResult) {
console.log(
`Experiment ${results.experimentName}: expected "${results.controlResult}" but got "${results.candidateResult}"`
);
}
console.log(
`Control took ${results.controlTimeMs} ms and candidate took ${results.candidateTimeMs} ms`
);
}
const experiment = scientist.experiment({
name: 'Pi',
control: oldPi,
candidate: newPi,
options: {
publish,
},
});
console.log(`Pi is ${experiment()}`);
{
"name": "tzdemo",
"version": "1.0.0",
"description": "Tzientist demo",
"main": "index.js",
"scripts": {
"start": "tsc && node index.js"
},
"author": "Bill Sorensen",
"license": "MIT",
"dependencies": {
"typescript": "^3.9.3",
"tzientist": "^2.2.0"
}
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment