buffalo-bench
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -0,1 +1,8 @@ | ||
### [1.0.2](https://github.com/Masquerade-Circus/buffalo-bench/compare/1.0.1...1.0.2) (2021-08-10) | ||
### Code Refactoring | ||
* **main:** minor fixes and improvements ([1f7e341](https://github.com/Masquerade-Circus/buffalo-bench/commit/1f7e341cf3eb883e857c2f2fe9f8023c851f6570)) | ||
### [1.0.1](https://github.com/Masquerade-Circus/buffalo-bench/compare/1.0.0...1.0.1) (2021-08-09) | ||
@@ -2,0 +9,0 @@ |
@@ -1,1 +0,1 @@ | ||
(()=>{var e,t,r=Object.defineProperty,a=(e,t,a)=>(((e,t,a)=>{t in e?r(e,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[t]=a})(e,"symbol"!=typeof t?t+"":t,a),a),i=class extends Error{code;message;name;statusCode=0;constructor(e="Something went wrong",t){super(),this.message=e,this.code=t,this.name=this.constructor.name}},s={BenchmarkError:i,BeforeEachError:class extends i{statusCode=1;name="BeforeEachError"},AfterEachError:class extends i{statusCode=2;name="AfterEachError"},RunError:class extends i{statusCode=3;name="RunError"},AfterError:class extends i{statusCode=4;name="AfterError"},BeforeError:class extends i{statusCode=5;name="BeforeError"},FatalError:class extends i{statusCode=7;name="FatalError"}};function n(e,t,r){let a=new s[r](t);return a.stack=e.stack,a}async function m(e,t,r,...a){if(r)try{await r.bind(e)(...a)}catch(a){return n(a,`Benchmark \`${e.name}\` failed to run \`${r.name}\` callback: ${a.message}`,t)}}(t=e||(e={})).MeanTime="meanTime",t.MedianTime="medianTime",t.StandardDeviation="standardDeviation",t.MaxTime="maxTime",t.MinTime="minTime",t.Hz="hz",t.RunTime="runTime",t.Cycles="cycles",t.Percent="percent";var o=class{name;error;cycles=0;samples=0;hz=0;meanTime=0;medianTime=0;standardDeviation=0;maxTime=0;minTime=0;times=[];options;stamp;runTime=0;totalTime=0;constructor(e,t,r={}){this.name=e;let a={...o.defaults,...r};"function"==typeof t?a.fn=t:a={...a,...t},this.options=a}toJSON(){let{name:e,error:t,cycles:r,hz:a,runTime:i,totalTime:s,samples:n,meanTime:m,medianTime:o,standardDeviation:h,maxTime:c,minTime:l}=this;return{name:e,errorMessage:t?t.message:void 0,cycles:r,samples:n,hz:a,meanTime:m,medianTime:o,standardDeviation:h,maxTime:c,minTime:l,runTime:i,totalTime:s}}compareWith(t,r=e.Percent){let{error:a,cycles:i,hz:s,meanTime:n,medianTime:m,standardDeviation:o,maxTime:h,minTime:c,runTime:l}=this;if(a)return-1;if(t.error)return 1;switch(r){case"meanTime":return n-t.meanTime;case"medianTime":return m-t.medianTime;case"standardDeviation":return o-t.standardDeviation;case"maxTime":return h-t.maxTime;case"minTime":return c-t.minTime;case"hz":return s-t.hz;case"runTime":return l-t.runTime;case"cycles":return i-t.cycles;case"percent":return Math.trunc(100/t.hz*s-100);default:throw new Error(`Unknown compare field: ${r}`)}}async runSample(){let{beforeEach:e,afterEach:t,fn:r}=this.options,a=performance.now();for(;performance.now()-a<1e3;){let a=performance.now();this.cycles++;let i,s=await m(this,"BeforeEachError",e);if(s)throw s;try{if("AsyncFunction"===r.constructor.name){let e=performance.now();await r(),i=performance.now()-e}else{let e=performance.now();r(),i=performance.now()-e}}catch(e){throw n(e,`Benchmark \`${this.name}\` failed to run \`fn\`: ${e.message}`,"RunError")}this.times.push(i),this.runTime+=i;let o=await m(this,"AfterEachError",t);if(o)throw o;this.totalTime+=performance.now()-a}}async run(){this.stamp=performance.now();let{maxTime:e,minSamples:t,after:r,before:a,onError:i,fn:s}=this.options,n=1e3*e;try{let e=await m(this,"BeforeError",a);if(e)throw e;for(;this.samples<t||this.totalTime<n;)this.samples++,await this.runSample();this.hz=this.cycles/(this.runTime/1e3),this.meanTime=this.runTime/this.times.length,this.medianTime=this.times.sort((e,t)=>e-t)[Math.floor(this.times.length/2)]||0,this.standardDeviation=Math.sqrt(this.times.map(e=>Math.pow(e-this.meanTime,2)).reduce((e,t)=>e+t,0)/this.times.length),this.maxTime=this.times.reduce((e,t)=>Math.max(e,t),0),this.minTime=this.times.reduce((e,t)=>Math.min(e,t),1/0);let i=await m(this,"AfterError",r);if(i)throw i}catch(e){this.error=e;let t=await m(this,"FatalError",i,e);if(t)throw t}}},h=o;a(h,"Suite"),a(h,"version","1.0.0"),a(h,"defaults",{maxTime:5,minSamples:1});var c=class{name;error;options;stamp;runTime=0;totalTime=0;benchmarks=[];constructor(e,t={}){this.name=e,this.options={...c.defaults,...t}}toJSON(){let{error:e,name:t,runTime:r,totalTime:a}=this;return{name:t,errorMessage:e?e.message:void 0,runTime:r,totalTime:a,passed:!e,benchmarks:this.benchmarks.map(e=>e.toJSON())}}add(e,t,r={}){let a={minSamples:this.options.minSamples,maxTime:this.options.maxTime,...r};"function"==typeof t?a.fn=t:a={...a,...t};let i=new h(e,a);return this.benchmarks.push(i),i}async run(){this.stamp=performance.now();let{beforeEach:e,afterEach:t,after:r,before:a,onError:i}=this.options;try{let i=await m(this,"BeforeError",a);if(i)throw i;for(let r=0,a=this.benchmarks.length;r<a;r++){let a=this.benchmarks[r],i=await m(this,"BeforeEachError",e,a,r);if(i)throw i;await a.run(),this.runTime+=a.runTime,this.totalTime+=a.totalTime;let s=await m(this,"AfterEachError",t,a,r);if(s)throw s}let s=await m(this,"AfterError",r);if(s)throw s}catch(e){this.error=e;let t=await m(this,"FatalError",i,e);if(t)throw t}}getSortedBenchmarksBy(e){return this.benchmarks.slice().sort((t,r)=>{let a=r.compareWith(t,e);return a>0?1:a<0?-1:0})}getFastest(e){return this.getSortedBenchmarksBy(e)[0]}getSlowest(e){let t=this.getSortedBenchmarksBy(e);return t[t.length-1]}compareFastestWithSlowest(e){let t=this.getFastest(e),r=this.getSlowest(e);return{fastest:t,slowest:r,by:t.compareWith(r,e)}}},l=c;a(l,"defaults",{maxTime:5,minSamples:1}),h.Suite=l;var u={CompareBy:e,default:h};"undefined"!=typeof module?module.exports=u:self.Benchmark=u})(); | ||
(()=>{var e,t,r=Object.defineProperty,a=(e,t,a)=>(((e,t,a)=>{t in e?r(e,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[t]=a})(e,"symbol"!=typeof t?t+"":t,a),a),i="undefined"==typeof performance?()=>Date.now():()=>performance.now(),s=class extends Error{code;message;name;statusCode=0;constructor(e="Something went wrong",t){super(),this.message=e,this.code=t,this.name=this.constructor.name}},n={BenchmarkError:s,BeforeEachError:class extends s{statusCode=1;name="BeforeEachError"},AfterEachError:class extends s{statusCode=2;name="AfterEachError"},RunError:class extends s{statusCode=3;name="RunError"},AfterError:class extends s{statusCode=4;name="AfterError"},BeforeError:class extends s{statusCode=5;name="BeforeError"},FatalError:class extends s{statusCode=7;name="FatalError"}};function m(e,t,r){let a=new n[r](t);a.stack=e.stack;for(let t in e)e.hasOwnProperty(t)&&(a[t]=e[t]);return a}async function o(e,t,r,...a){if(r)try{await r.bind(e)(...a)}catch(a){return m(a,`Benchmark \`${e.name}\` failed to run \`${r.name}\` callback: ${a.message}`,t)}}(t=e||(e={})).MeanTime="meanTime",t.MedianTime="medianTime",t.StandardDeviation="standardDeviation",t.MaxTime="maxTime",t.MinTime="minTime",t.Hz="hz",t.RunTime="runTime",t.Cycles="cycles",t.Percent="percent";var h=class{name;error;cycles=0;samples=0;hz=0;meanTime=0;medianTime=0;standardDeviation=0;maxTime=0;minTime=0;times=[];options;stamp;runTime=0;totalTime=0;constructor(e,t,r={}){this.name=e;let a={...h.defaults,...r};"function"==typeof t?a.fn=t:a={...a,...t},this.options=a}toJSON(){let{name:e,error:t,cycles:r,hz:a,runTime:i,totalTime:s,samples:n,meanTime:m,medianTime:o,standardDeviation:h,maxTime:c,minTime:l}=this;return{name:e,errorMessage:t?t.message:void 0,cycles:r,samples:n,hz:a,meanTime:m,medianTime:o,standardDeviation:h,maxTime:c,minTime:l,runTime:i,totalTime:s}}compareWith(t,r=e.Percent){let{error:a,cycles:i,hz:s,meanTime:n,medianTime:m,standardDeviation:o,maxTime:h,minTime:c,runTime:l}=this;if(a)return-1;if(t.error)return 1;switch(r){case"meanTime":return t.meanTime-n;case"medianTime":return t.medianTime-m;case"standardDeviation":return o-t.standardDeviation;case"maxTime":return h-t.maxTime;case"minTime":return t.minTime-c;case"hz":return s-t.hz;case"runTime":return l-t.runTime;case"cycles":return i-t.cycles;case"percent":return Math.trunc(100*(100/n*t.meanTime-100))/100;default:throw new Error(`Unknown compare field: ${r}`)}}async runSample(){let{beforeEach:e,afterEach:t,fn:r}=this.options,a=i();for(;i()-a<1e3;){let a=i();this.cycles++;let s,n=await o(this,"BeforeEachError",e);if(n)throw n;try{if("AsyncFunction"===r.constructor.name){let e=i();await r(),s=i()-e}else{let e=i();r(),s=i()-e}}catch(e){throw m(e,`Benchmark \`${this.name}\` failed to run \`fn\`: ${e.message}`,"RunError")}this.times.push(s),this.runTime+=s;let h=await o(this,"AfterEachError",t);if(h)throw h;this.totalTime+=i()-a}}async run(){this.stamp=i();let{maxTime:e,minSamples:t,after:r,before:a,onError:s}=this.options,n=1e3*e;try{let e=await o(this,"BeforeError",a);if(e)throw e;for(;this.samples<t||this.totalTime<n;)this.samples++,await this.runSample();this.hz=this.cycles/(this.runTime/1e3),this.meanTime=this.runTime/this.times.length,this.medianTime=this.times.sort((e,t)=>e-t)[Math.floor(this.times.length/2)]||0,this.standardDeviation=Math.sqrt(this.times.map(e=>Math.pow(e-this.meanTime,2)).reduce((e,t)=>e+t,0)/this.times.length),this.maxTime=this.times.reduce((e,t)=>Math.max(e,t),0),this.minTime=this.times.reduce((e,t)=>Math.min(e,t),1/0);let i=await o(this,"AfterError",r);if(i)throw i}catch(e){this.error=e;let t=await o(this,"FatalError",s,e);if(t)throw t}}},c=h;a(c,"Suite"),a(c,"version","1.0.1"),a(c,"defaults",{maxTime:5,minSamples:1});var l=class{name;error;options;stamp;runTime=0;totalTime=0;benchmarks=[];constructor(e,t={}){this.name=e,this.options={...l.defaults,...t}}toJSON(){let{error:t,name:r,runTime:a,totalTime:i}=this;return{name:r,errorMessage:t?t.message:void 0,runTime:a,totalTime:i,passed:!t,benchmarks:this.getSortedBenchmarksBy(e.MeanTime).map(e=>e.toJSON())}}add(e,t,r={}){let a={minSamples:this.options.minSamples,maxTime:this.options.maxTime,...r};"function"==typeof t?a.fn=t:a={...a,...t};let i=new c(e,a);return this.benchmarks.push(i),i}async run(){this.stamp=i();let{beforeEach:e,afterEach:t,after:r,before:a,onError:s}=this.options;try{let i=await o(this,"BeforeError",a);if(i)throw i;for(let r=0,a=this.benchmarks.length;r<a;r++){let a=this.benchmarks[r],i=await o(this,"BeforeEachError",e,a,r);if(i)throw i;await a.run(),this.runTime+=a.runTime,this.totalTime+=a.totalTime;let s=await o(this,"AfterEachError",t,a,r);if(s)throw s}let s=await o(this,"AfterError",r);if(s)throw s}catch(e){this.error=e;let t=await o(this,"FatalError",s,e);if(t)throw t}}getSortedBenchmarksBy(e){return this.benchmarks.slice().sort((t,r)=>{let a=r.compareWith(t,e);return a>0?1:a<0?-1:0})}getFastest(e){return this.getSortedBenchmarksBy(e)[0]}getSlowest(e){let t=this.getSortedBenchmarksBy(e);return t[t.length-1]}compareFastestWithSlowest(t){let r=t===e.Percent?e.MeanTime:t,a=this.getFastest(r),i=this.getSlowest(r);return{fastest:a,slowest:i,by:a.compareWith(i,t)}}},u=l;a(u,"defaults",{maxTime:5,minSamples:1}),c.Suite=u;var f={CompareBy:e,default:c};"undefined"!=typeof module?module.exports=f:self.Benchmark=f})(); |
@@ -105,3 +105,3 @@ // A benchmarking library that supports async hooks and benchmarks by default. | ||
declare const performance: any; | ||
let now = typeof performance === "undefined" ? () => Date.now() : () => performance.now(); | ||
@@ -116,2 +116,3 @@ //*** Errors ***// | ||
statusCode = 0; | ||
[key: string]: any; | ||
@@ -262,2 +263,7 @@ constructor(message = "Something went wrong", code?: string) { | ||
benchmarkError.stack = error.stack; | ||
for (let i in error) { | ||
if (error.hasOwnProperty(i)) { | ||
benchmarkError[i] = (error as any)[i]; | ||
} | ||
} | ||
return benchmarkError; | ||
@@ -365,5 +371,5 @@ } | ||
case "meanTime": | ||
return meanTime - other.meanTime; | ||
return other.meanTime - meanTime; | ||
case "medianTime": | ||
return medianTime - other.medianTime; | ||
return other.medianTime - medianTime; | ||
case "standardDeviation": | ||
@@ -374,3 +380,3 @@ return standardDeviation - other.standardDeviation; | ||
case "minTime": | ||
return minTime - other.minTime; | ||
return other.minTime - minTime; | ||
case "hz": | ||
@@ -383,3 +389,3 @@ return hz - other.hz; | ||
case "percent": | ||
return Math.trunc((100 / other.hz) * hz - 100); | ||
return Math.trunc(((100 / meanTime) * other.meanTime - 100) * 100) / 100; | ||
default: | ||
@@ -393,6 +399,6 @@ throw new Error(`Unknown compare field: ${compareBy}`); | ||
let sampleMaxTime = 1000; | ||
let startTime = performance.now(); | ||
let startTime = now(); | ||
while (performance.now() - startTime < sampleMaxTime) { | ||
const startCycleTime = performance.now(); | ||
while (now() - startTime < sampleMaxTime) { | ||
const startCycleTime = now(); | ||
this.cycles++; | ||
@@ -407,9 +413,9 @@ const BeforeEachError = await runCallback(this, "BeforeEachError", beforeEach); | ||
if (isAsync(fn)) { | ||
let start = performance.now(); | ||
let start = now(); | ||
await fn(); | ||
time = performance.now() - start; | ||
time = now() - start; | ||
} else { | ||
let start = performance.now(); | ||
let start = now(); | ||
fn(); | ||
time = performance.now() - start; | ||
time = now() - start; | ||
} | ||
@@ -428,3 +434,3 @@ } catch (error) { | ||
this.totalTime += performance.now() - startCycleTime; | ||
this.totalTime += now() - startCycleTime; | ||
} | ||
@@ -435,4 +441,4 @@ } | ||
async run(): Promise<void> { | ||
this.stamp = performance.now(); | ||
const { maxTime, minSamples, after, before, onError, fn } = this.options; | ||
this.stamp = now(); | ||
const { maxTime, minSamples, after, before, onError } = this.options; | ||
let maxTimeInMilliseconds = maxTime * 1000; | ||
@@ -526,3 +532,3 @@ | ||
getSortedBenchmarks(sortedBy: CompareBy): Benchmark[]; | ||
getSortedBenchmarksBy(sortedBy: CompareBy): Benchmark[]; | ||
getFastest(sortedBy: CompareBy): Benchmark; | ||
@@ -564,3 +570,3 @@ getSlowest(sortedBy: CompareBy): Benchmark; | ||
passed: !error, | ||
benchmarks: this.benchmarks.map((benchmark) => benchmark.toJSON()) | ||
benchmarks: this.getSortedBenchmarksBy(CompareBy.MeanTime).map((benchmark) => benchmark.toJSON()) | ||
}; | ||
@@ -596,3 +602,3 @@ } | ||
async run(): Promise<void> { | ||
this.stamp = performance.now(); | ||
this.stamp = now(); | ||
const { beforeEach, afterEach, after, before, onError } = this.options; | ||
@@ -658,4 +664,5 @@ | ||
compareFastestWithSlowest(compareBy: CompareBy) { | ||
const fastest = this.getFastest(compareBy); | ||
const slowest = this.getSlowest(compareBy); | ||
let sortBy = compareBy === CompareBy.Percent ? CompareBy.MeanTime : compareBy; | ||
const fastest = this.getFastest(sortBy); | ||
const slowest = this.getSlowest(sortBy); | ||
@@ -662,0 +669,0 @@ return { |
{ | ||
"name": "buffalo-bench", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A benchmarking library that supports async hooks and benchmarks by default.", | ||
@@ -5,0 +5,0 @@ "source": "lib/index.ts", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89643
650