Comparing version 0.0.3 to 0.0.4
CHANGELOG | ||
v0.0.4 | ||
- Markdown report option | ||
- Bench test case priorities | ||
- FIX: Cap iteration count for long running test cases (regression) | ||
v0.0.3 | ||
@@ -4,0 +9,0 @@ - Bench: Initial regression testing, local storage |
@@ -1,1 +0,1 @@ | ||
import{formatSI}from'./utils/formatters';import{autobind}from'./utils/autobind';import LocalStorage from'./utils/local-storage';import assert from'assert';const noop=()=>{},TIME_THRESHOLD_MS=30,TIME_COOLDOWN_MS=5,MIN_ITERATIONS=1,CALIBRATION_TESTS=[{id:'warmup',initFunc:noop,testFunc:()=>100,opts:{}}];export default class Bench{constructor({id:a,log:b=console.log.bind(console),time:c=TIME_THRESHOLD_MS,delay:d=TIME_COOLDOWN_MS,minIterations:e=MIN_ITERATIONS}={}){this.id=a,this.opts={log:b,time:c,delay:d,minIterations:e},this.tests={},this.results={},this.table={},autobind(this),Object.seal(this)}calibrate(){return this}run(){const a=new Date,b=this.tests,c=this.onBenchmarkComplete,d=runAsyncTests({tests:b,onBenchmarkComplete:c});return d.then(()=>{const b=(new Date-a)/1e3;this.opts.log(''),this.opts.log(`Completed benchmark in ${b}s`),this.onSuiteComplete()}),d}group(a){return assert(!this.tests[a],'tests need unique id strings'),this.tests[a]={id:a,group:!0,opts:this.opts},this}add(a,b,c){assert(a),assert('function'==typeof b);let d=null,e=b;return'function'==typeof c&&(d=b,e=c),assert(!this.tests[a],'tests need unique id strings'),this.tests[a]={id:a,initFunc:d,testFunc:e,opts:this.opts},this}onBenchmarkComplete({id:a,time:b,iterations:c,itersPerSecond:d}){const e=Math.round(c/b);this.table[a]={percent:'',iterations:`${d}/s`,current:e,max:''}}onSuiteComplete(){let a;try{a=new LocalStorage}catch(a){}if(a){const b=a.getObject(this.id),c=this.updateTable(this.table,b);a.setObject(this.id,c),console.table(c)}}updateTable(a,b){for(const c in this.table)if(b[c]&&b[c].max!==void 0){a[c].max=Math.max(a[c].current,b[c].max);const d=a[c].current/b[c].max;a[c].percent=`${Math.round(100*d)}%`}else a[c].max=a[c].current;return a}}function runCalibrationTests({tests:a}){let b=Promise.resolve(!0);for(const c of CALIBRATION_TESTS)b=b.then(()=>runAsyncTest({test:c,silent:!0}));return b}function runAsyncTests({tests:a,onBenchmarkComplete:b=noop}){let c=runCalibrationTests({tests:a,onBenchmarkComplete:b});for(const d in a){const e=a[d];c=c.then(()=>runAsyncTest({test:e,onBenchmarkComplete:b}))}return c}function runAsyncTest({test:a,onBenchmarkComplete:b,silent:c=!1}){return new Promise((d)=>{setTimeout(()=>{try{if(a.group)a.opts.log(''),a.opts.log(`${a.id}`);else{var e=runBenchTest(a);const d=e.time,f=e.iterations,g=f/d,h=formatSI(g);c||a.opts.log(`├─ ${a.id}: ${h} iterations/s (${d.toFixed(2)}s elapsed)`),b&&b({id:a.id,time:d,iterations:f,iterationsPerSecond:g,itersPerSecond:h})}}finally{d(!0)}},a.opts.delay)})}function runBenchTest(a){let b=a.opts.minIterations,c=0;for(;c<a.opts.time;){let d=10;1<c&&(d=1.1*(a.opts.time/c)),b*=d;const e=new Date;runBenchTestIterations(a,b),c=new Date-e}const d=c/1e3;return{time:d,iterations:b}}function runBenchTestIterations(a,b){const c=a.initFunc&&a.initFunc(),d=a.context,e=a.testFunc;if(d&&c)for(let a=0;a<b;a++)e.call(d,c);else for(let a=0;a<b;a++)e.call(d)} | ||
import{formatSI,rightPad}from'./utils/formatters';import{global}from'./utils/globals';import{autobind}from'./utils/autobind';import LocalStorage from'./utils/local-storage';import assert from'assert';const noop=()=>{},TIME_THRESHOLD_MS=80,TIME_COOLDOWN_MS=5,MIN_ITERATIONS=1;export const LOG_ENTRY={GROUP:'group',TEST:'test',COMPLETE:'complete'};const CALIBRATION_TESTS=[{id:'warmup',initFunc:noop,testFunc:()=>100,opts:{}}];export default class Bench{constructor({id:a,log:b,time:c=TIME_THRESHOLD_MS,delay:d=TIME_COOLDOWN_MS,minIterations:e=MIN_ITERATIONS}={}){if(!b){const a=global.probe&&global.probe.markdown;b=a?logResultsAsMarkdownTable:logResultsAsTree}this.id=a,this.opts={log:b,time:c,delay:d,minIterations:e},this.tests={},this.results={},this.table={},autobind(this),Object.seal(this)}calibrate(){return this}run(){const a=new Date,b=this.tests,c=this.onBenchmarkComplete,d=runAsyncTests({tests:b,onBenchmarkComplete:c});return d.then(()=>{const b=(new Date-a)/1e3;logEntry(this,{entry:LOG_ENTRY.COMPLETE,time:b}),this.onSuiteComplete()}),d}group(a){return assert(!this.tests[a],'tests need unique id strings'),this.tests[a]={id:a,group:!0,opts:this.opts},this}add(a,b,c,d){'string'==typeof a&&(d=c,c=b,b=a,a=0),assert(b),assert('function'==typeof c);let e=null,f=c;return'function'==typeof d&&(e=c,f=d),assert(!this.tests[b],'tests need unique id strings'),this.tests[b]={id:b,priority:a,initFunc:e,testFunc:f,opts:this.opts},this}onBenchmarkComplete({id:a,time:b,iterations:c,itersPerSecond:d}){const e=Math.round(c/b);this.table[a]={percent:'',iterations:`${d}/s`,current:e,max:''}}onSuiteComplete(){let a;try{a=new LocalStorage}catch(a){}if(a){const b=a.getObject(this.id),c=this.updateTable(this.table,b);a.setObject(this.id,c),console.table(c)}}updateTable(a,b){for(const c in this.table)if(b[c]&&b[c].max!==void 0){a[c].max=Math.max(a[c].current,b[c].max);const d=a[c].current/b[c].max;a[c].percent=`${Math.round(100*d-100)}%`}else a[c].max=a[c].current;return a}}function runCalibrationTests({tests:a}){let b=Promise.resolve(!0);for(const c of CALIBRATION_TESTS)b=b.then(()=>runAsyncTest({test:c,silent:!0}));return b}function runAsyncTests({tests:a,onBenchmarkComplete:b=noop}){let c=runCalibrationTests({tests:a,onBenchmarkComplete:b});for(const d in a){const e=a[d];c=c.then(()=>runAsyncTest({test:e,onBenchmarkComplete:b}))}return c}function runAsyncTest({test:a,onBenchmarkComplete:b,silent:c=!1}){return new Promise((d)=>{setTimeout(()=>{try{if(a.group)logEntry(a,{entry:LOG_ENTRY.GROUP,id:a.id});else{var e=runBenchTest(a);const d=e.time,f=e.iterations,g=f/d,h=formatSI(g);c||logEntry(a,{entry:LOG_ENTRY.TEST,id:a.id,priority:a.priority,itersPerSecond:h,time:d}),b&&b({id:a.id,time:d,iterations:f,iterationsPerSecond:g,itersPerSecond:h})}}finally{d(!0)}},a.opts.delay)})}function runBenchTest(a){let b=a.opts.minIterations/10,c=0;for(;c<a.opts.time;){let d=10;10<c&&(d=1.25*(a.opts.time/c)),b*=d;const e=new Date;runBenchTestIterations(a,b),c=new Date-e}const d=c/1e3;return{time:d,iterations:b}}function runBenchTestIterations(a,b){const c=a.initFunc&&a.initFunc(),d=a.context,e=a.testFunc;if(d&&c)for(let a=0;a<b;a++)e.call(d,c);else for(let a=0;a<b;a++)e.call(d)}function logEntry(a,b){const c=10|(global.probe&&global.probe.priority);(0|b.priority)<=c&&a.opts.log(b)}export function logResultsAsMarkdownTable({entry:a,id:b,itersPerSecond:c,time:d}){const e=50;switch(a){case LOG_ENTRY.GROUP:console.log(''),console.log(`| ${rightPad(b,e)} | iterations/s |`),console.log(`| ${rightPad('---',e)} | --- |`);break;case LOG_ENTRY.TEST:console.log(`| ${rightPad(b,e)} | ${rightPad(c,12)} |`);break;case LOG_ENTRY.COMPLETE:console.log(''),console.log(`Completed benchmark in ${d}s`);break;default:}}export function logResultsAsTree({entry:a,id:b,itersPerSecond:c,time:d}){switch(a){case LOG_ENTRY.GROUP:console.log(''),console.log(`${b}`);break;case LOG_ENTRY.TEST:console.log(`├─ ${b}: ${c} iterations/s`);break;case LOG_ENTRY.COMPLETE:console.log(''),console.log(`Completed benchmark in ${d}s`);break;default:}}export function logResultsAsTreeWithElapsed({entry:a,id:b,itersPerSecond:c,time:d}){a===LOG_ENTRY.TEST?console.log(`├─ ${b}: ${c} iterations/s (${d.toFixed(2)}s elapsed)`):logResultsAsTree({entry:a,id:b,itersPerSecond:c,time:d})} |
@@ -1,1 +0,1 @@ | ||
export{VERSION}from'./utils/globals';export{default as Log}from'./log';export{default as Bench}from'./bench';import{enableDOMLogging}from'./utils/log-to-dom';export const experimental={enableDOMLogging}; | ||
import'./init';export{VERSION}from'./utils/globals';export{default as Log}from'./log';export{default as Bench,logResultsAsMarkdownTable,logResultsAsTree,logResultsAsTreeWithElapsed}from'./bench';import{enableDOMLogging}from'./utils/log-to-dom';export const experimental={enableDOMLogging}; |
@@ -1,1 +0,1 @@ | ||
const d3Format=require('d3-format');export function formatSI(a){return d3Format.format('.3s')(a)}export function formatTime(a){let b;return b=10>a?`${a.toFixed(2)}ms`:100>a?`${a.toFixed(1)}ms`:1e3>a?`${a.toFixed(0)}ms`:`${(a/1e3).toFixed(2)}s`,b}export function leftPad(a,b=8){for(;a.length<b;)a=` ${a}`;return a}export function formatImage(a,b,c){Math.floor;const d=a.width*c,e=a.height*c,f=a.src.replace(/\(/g,'%28').replace(/\)/g,'%29');return[`${b} %c+`,'font-size:1px;color:transparent;']}export function formatValue(a,b={}){var c=Math.abs,d=b.isInteger;const e=d!==void 0&&d;if(Array.isArray(a)||ArrayBuffer.isView(a))return formatArrayValue(a,b);if(!Number.isFinite(a))return a+'';if(c(a)<1e-16)return e?'0':'0.';if(e)return a.toFixed(0);if(100<c(a)&&1e4>c(a))return a.toFixed(0);const f=a.toPrecision(2),g=f.indexOf('.0');return g===f.length-2?f.slice(0,-1):f}function formatArrayValue(a,b){var c=b.maxElts;const d=c===void 0?16:c;var e=b.size;const f=e===void 0?1:e;let g='[';for(let c=0;c<a.length&&c<d;++c)0<c&&(g+=`,${0==c%f?' ':''}`),g+=formatValue(a[c],b);const h=a.length>d?'...':']';return`${g}${h}`} | ||
const d3Format=require('d3-format');export function formatSI(a){return d3Format.format('.3s')(a)}export function formatTime(a){let b;return b=10>a?`${a.toFixed(2)}ms`:100>a?`${a.toFixed(1)}ms`:1e3>a?`${a.toFixed(0)}ms`:`${(a/1e3).toFixed(2)}s`,b}export function leftPad(a,b=8){const c=Math.max(b-a.length,0);return`${' '.repeat(c)}${a}`}export function rightPad(a,b=8){const c=Math.max(b-a.length,0);return`${a}${' '.repeat(c)}`}export function formatImage(a,b,c){Math.floor;const d=a.width*c,e=a.height*c,f=a.src.replace(/\(/g,'%28').replace(/\)/g,'%29');return[`${b} %c+`,'font-size:1px;color:transparent;']}export function formatValue(a,b={}){var c=Math.abs,d=b.isInteger;const e=d!==void 0&&d;if(Array.isArray(a)||ArrayBuffer.isView(a))return formatArrayValue(a,b);if(!Number.isFinite(a))return a+'';if(c(a)<1e-16)return e?'0':'0.';if(e)return a.toFixed(0);if(100<c(a)&&1e4>c(a))return a.toFixed(0);const f=a.toPrecision(2),g=f.indexOf('.0');return g===f.length-2?f.slice(0,-1):f}function formatArrayValue(a,b){var c=b.maxElts;const d=c===void 0?16:c;var e=b.size;const f=e===void 0?1:e;let g='[';for(let c=0;c<a.length&&c<d;++c)0<c&&(g+=`,${0==c%f?' ':''}`),g+=formatValue(a[c],b);const h=a.length>d?'...':']';return`${g}${h}`} |
@@ -1,1 +0,1 @@ | ||
const isBrowser='object'!=typeof process||'[object process]'!==process+''||process.browser,window_='undefined'==typeof window?global:window,document_='undefined'==typeof document?{}:document,global_='undefined'==typeof global?window:global,process_='object'==typeof process?process:{},VERSION='0.0.3';export{window_ as window,document_ as document,global_ as global,process_ as process,console,isBrowser,VERSION}; | ||
const isBrowser='object'!=typeof process||'[object process]'!==process+''||process.browser,window_='undefined'==typeof window?global:window,document_='undefined'==typeof document?{}:document,global_='undefined'==typeof global?window:global,process_='object'==typeof process?process:{},VERSION='0.0.4';export{window_ as window,document_ as document,global_ as global,process_ as process,console,isBrowser,VERSION}; |
@@ -1,1 +0,1 @@ | ||
import{document,console}from'./globals';let old=null;export function enableDOMLogging(a=!0){a&&!old&&(old=console.log.bind(console),console.log=(...a)=>{logLineToDOM(...a),old(...a)}),!a&&old&&(console.log=old,old=null)}let logDiv=null;export function logLineToDOM(a){logDiv||(logDiv=document.createElement('div'));const b=document.body.childNodes;document.body.insertBefore(logDiv,b&&b[0]),'string'==typeof a&&(logDiv.innerHTML+=`${a}<br />`)}export default enableDOMLogging; | ||
import{document,console,global}from'./globals';let old=null;export function enableDOMLogging(a=!0){a&&!old&&(old=console.log.bind(console),console.log=(...a)=>{logLineToDOM(...a),old(...a)}),!a&&old&&(console.log=old,old=null)}let logDiv=null;export function logLineToDOM(a){if(!logDiv){const a=global.probe.markdown;logDiv=document.createElement(a?'pre':'div')}const b=document.body.childNodes;document.body.insertBefore(logDiv,b&&b[0]),'string'==typeof a&&(logDiv.innerHTML+=`${a}<br />`)}export default enableDOMLogging; |
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0});var _formatters=require('./utils/formatters'),_autobind=require('./utils/autobind'),_localStorage=require('./utils/local-storage'),_localStorage2=_interopRequireDefault(_localStorage),_assert=require('assert'),_assert2=_interopRequireDefault(_assert);function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}const noop=()=>{},TIME_THRESHOLD_MS=30,TIME_COOLDOWN_MS=5,MIN_ITERATIONS=1,CALIBRATION_TESTS=[{id:'warmup',initFunc:noop,testFunc:()=>100,opts:{}}];class Bench{constructor({id:a,log:b=console.log.bind(console),time:c=TIME_THRESHOLD_MS,delay:d=TIME_COOLDOWN_MS,minIterations:e=MIN_ITERATIONS}={}){this.id=a,this.opts={log:b,time:c,delay:d,minIterations:e},this.tests={},this.results={},this.table={},(0,_autobind.autobind)(this),Object.seal(this)}calibrate(){return this}run(){const a=new Date,b=this.tests,c=this.onBenchmarkComplete,d=runAsyncTests({tests:b,onBenchmarkComplete:c});return d.then(()=>{const b=(new Date-a)/1e3;this.opts.log(''),this.opts.log(`Completed benchmark in ${b}s`),this.onSuiteComplete()}),d}group(a){return(0,_assert2.default)(!this.tests[a],'tests need unique id strings'),this.tests[a]={id:a,group:!0,opts:this.opts},this}add(a,b,c){(0,_assert2.default)(a),(0,_assert2.default)('function'==typeof b);let d=null,e=b;return'function'==typeof c&&(d=b,e=c),(0,_assert2.default)(!this.tests[a],'tests need unique id strings'),this.tests[a]={id:a,initFunc:d,testFunc:e,opts:this.opts},this}onBenchmarkComplete({id:a,time:b,iterations:c,itersPerSecond:d}){const e=Math.round(c/b);this.table[a]={percent:'',iterations:`${d}/s`,current:e,max:''}}onSuiteComplete(){let a;try{a=new _localStorage2.default}catch(a){}if(a){const b=a.getObject(this.id),c=this.updateTable(this.table,b);a.setObject(this.id,c),console.table(c)}}updateTable(a,b){for(const c in this.table)if(b[c]&&b[c].max!==void 0){a[c].max=Math.max(a[c].current,b[c].max);const d=a[c].current/b[c].max;a[c].percent=`${Math.round(100*d)}%`}else a[c].max=a[c].current;return a}}exports.default=Bench;function runCalibrationTests({tests:a}){let b=Promise.resolve(!0);for(const c of CALIBRATION_TESTS)b=b.then(()=>runAsyncTest({test:c,silent:!0}));return b}function runAsyncTests({tests:a,onBenchmarkComplete:b=noop}){let c=runCalibrationTests({tests:a,onBenchmarkComplete:b});for(const d in a){const e=a[d];c=c.then(()=>runAsyncTest({test:e,onBenchmarkComplete:b}))}return c}function runAsyncTest({test:a,onBenchmarkComplete:b,silent:c=!1}){return new Promise((d)=>{setTimeout(()=>{try{if(a.group)a.opts.log(''),a.opts.log(`${a.id}`);else{var e=runBenchTest(a);const d=e.time,f=e.iterations,g=f/d,h=(0,_formatters.formatSI)(g);c||a.opts.log(`├─ ${a.id}: ${h} iterations/s (${d.toFixed(2)}s elapsed)`),b&&b({id:a.id,time:d,iterations:f,iterationsPerSecond:g,itersPerSecond:h})}}finally{d(!0)}},a.opts.delay)})}function runBenchTest(a){let b=a.opts.minIterations,c=0;for(;c<a.opts.time;){let d=10;1<c&&(d=1.1*(a.opts.time/c)),b*=d;const e=new Date;runBenchTestIterations(a,b),c=new Date-e}const d=c/1e3;return{time:d,iterations:b}}function runBenchTestIterations(a,b){const c=a.initFunc&&a.initFunc(),d=a.context,e=a.testFunc;if(d&&c)for(let a=0;a<b;a++)e.call(d,c);else for(let a=0;a<b;a++)e.call(d)} | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.LOG_ENTRY=void 0,exports.logResultsAsMarkdownTable=logResultsAsMarkdownTable,exports.logResultsAsTree=logResultsAsTree,exports.logResultsAsTreeWithElapsed=logResultsAsTreeWithElapsed;var _formatters=require('./utils/formatters'),_globals=require('./utils/globals'),_autobind=require('./utils/autobind'),_localStorage=require('./utils/local-storage'),_localStorage2=_interopRequireDefault(_localStorage),_assert=require('assert'),_assert2=_interopRequireDefault(_assert);function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}const noop=()=>{},TIME_THRESHOLD_MS=80,TIME_COOLDOWN_MS=5,MIN_ITERATIONS=1,LOG_ENTRY=exports.LOG_ENTRY={GROUP:'group',TEST:'test',COMPLETE:'complete'},CALIBRATION_TESTS=[{id:'warmup',initFunc:noop,testFunc:()=>100,opts:{}}];class Bench{constructor({id:a,log:b,time:c=TIME_THRESHOLD_MS,delay:d=TIME_COOLDOWN_MS,minIterations:e=MIN_ITERATIONS}={}){if(!b){const a=_globals.global.probe&&_globals.global.probe.markdown;b=a?logResultsAsMarkdownTable:logResultsAsTree}this.id=a,this.opts={log:b,time:c,delay:d,minIterations:e},this.tests={},this.results={},this.table={},(0,_autobind.autobind)(this),Object.seal(this)}calibrate(){return this}run(){const a=new Date,b=this.tests,c=this.onBenchmarkComplete,d=runAsyncTests({tests:b,onBenchmarkComplete:c});return d.then(()=>{const b=(new Date-a)/1e3;logEntry(this,{entry:LOG_ENTRY.COMPLETE,time:b}),this.onSuiteComplete()}),d}group(a){return(0,_assert2.default)(!this.tests[a],'tests need unique id strings'),this.tests[a]={id:a,group:!0,opts:this.opts},this}add(a,b,c,d){'string'==typeof a&&(d=c,c=b,b=a,a=0),(0,_assert2.default)(b),(0,_assert2.default)('function'==typeof c);let e=null,f=c;return'function'==typeof d&&(e=c,f=d),(0,_assert2.default)(!this.tests[b],'tests need unique id strings'),this.tests[b]={id:b,priority:a,initFunc:e,testFunc:f,opts:this.opts},this}onBenchmarkComplete({id:a,time:b,iterations:c,itersPerSecond:d}){const e=Math.round(c/b);this.table[a]={percent:'',iterations:`${d}/s`,current:e,max:''}}onSuiteComplete(){let a;try{a=new _localStorage2.default}catch(a){}if(a){const b=a.getObject(this.id),c=this.updateTable(this.table,b);a.setObject(this.id,c),console.table(c)}}updateTable(a,b){for(const c in this.table)if(b[c]&&b[c].max!==void 0){a[c].max=Math.max(a[c].current,b[c].max);const d=a[c].current/b[c].max;a[c].percent=`${Math.round(100*d-100)}%`}else a[c].max=a[c].current;return a}}exports.default=Bench;function runCalibrationTests({tests:a}){let b=Promise.resolve(!0);for(const c of CALIBRATION_TESTS)b=b.then(()=>runAsyncTest({test:c,silent:!0}));return b}function runAsyncTests({tests:a,onBenchmarkComplete:b=noop}){let c=runCalibrationTests({tests:a,onBenchmarkComplete:b});for(const d in a){const e=a[d];c=c.then(()=>runAsyncTest({test:e,onBenchmarkComplete:b}))}return c}function runAsyncTest({test:a,onBenchmarkComplete:b,silent:c=!1}){return new Promise((d)=>{setTimeout(()=>{try{if(a.group)logEntry(a,{entry:LOG_ENTRY.GROUP,id:a.id});else{var e=runBenchTest(a);const d=e.time,f=e.iterations,g=f/d,h=(0,_formatters.formatSI)(g);c||logEntry(a,{entry:LOG_ENTRY.TEST,id:a.id,priority:a.priority,itersPerSecond:h,time:d}),b&&b({id:a.id,time:d,iterations:f,iterationsPerSecond:g,itersPerSecond:h})}}finally{d(!0)}},a.opts.delay)})}function runBenchTest(a){let b=a.opts.minIterations/10,c=0;for(;c<a.opts.time;){let d=10;10<c&&(d=1.25*(a.opts.time/c)),b*=d;const e=new Date;runBenchTestIterations(a,b),c=new Date-e}const d=c/1e3;return{time:d,iterations:b}}function runBenchTestIterations(a,b){const c=a.initFunc&&a.initFunc(),d=a.context,e=a.testFunc;if(d&&c)for(let a=0;a<b;a++)e.call(d,c);else for(let a=0;a<b;a++)e.call(d)}function logEntry(a,b){const c=10|(_globals.global.probe&&_globals.global.probe.priority);(0|b.priority)<=c&&a.opts.log(b)}function logResultsAsMarkdownTable({entry:a,id:b,itersPerSecond:c,time:d}){const e=50;switch(a){case LOG_ENTRY.GROUP:console.log(''),console.log(`| ${(0,_formatters.rightPad)(b,e)} | iterations/s |`),console.log(`| ${(0,_formatters.rightPad)('---',e)} | --- |`);break;case LOG_ENTRY.TEST:console.log(`| ${(0,_formatters.rightPad)(b,e)} | ${(0,_formatters.rightPad)(c,12)} |`);break;case LOG_ENTRY.COMPLETE:console.log(''),console.log(`Completed benchmark in ${d}s`);break;default:}}function logResultsAsTree({entry:a,id:b,itersPerSecond:c,time:d}){switch(a){case LOG_ENTRY.GROUP:console.log(''),console.log(`${b}`);break;case LOG_ENTRY.TEST:console.log(`├─ ${b}: ${c} iterations/s`);break;case LOG_ENTRY.COMPLETE:console.log(''),console.log(`Completed benchmark in ${d}s`);break;default:}}function logResultsAsTreeWithElapsed({entry:a,id:b,itersPerSecond:c,time:d}){a===LOG_ENTRY.TEST?console.log(`├─ ${b}: ${c} iterations/s (${d.toFixed(2)}s elapsed)`):logResultsAsTree({entry:a,id:b,itersPerSecond:c,time:d})} |
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.experimental=exports.Bench=exports.Log=exports.VERSION=void 0;var _globals=require('./utils/globals');Object.defineProperty(exports,'VERSION',{enumerable:!0,get:function get(){return _globals.VERSION}});var _log=require('./log');Object.defineProperty(exports,'Log',{enumerable:!0,get:function get(){return _interopRequireDefault(_log).default}});var _bench=require('./bench');Object.defineProperty(exports,'Bench',{enumerable:!0,get:function get(){return _interopRequireDefault(_bench).default}});var _logToDom=require('./utils/log-to-dom');function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}const experimental=exports.experimental={enableDOMLogging:_logToDom.enableDOMLogging}; | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.experimental=exports.logResultsAsTreeWithElapsed=exports.logResultsAsTree=exports.logResultsAsMarkdownTable=exports.Bench=exports.Log=exports.VERSION=void 0;var _globals=require('./utils/globals');Object.defineProperty(exports,'VERSION',{enumerable:!0,get:function get(){return _globals.VERSION}});var _log=require('./log');Object.defineProperty(exports,'Log',{enumerable:!0,get:function get(){return _interopRequireDefault(_log).default}});var _bench=require('./bench');Object.defineProperty(exports,'Bench',{enumerable:!0,get:function get(){return _interopRequireDefault(_bench).default}}),Object.defineProperty(exports,'logResultsAsMarkdownTable',{enumerable:!0,get:function get(){return _bench.logResultsAsMarkdownTable}}),Object.defineProperty(exports,'logResultsAsTree',{enumerable:!0,get:function get(){return _bench.logResultsAsTree}}),Object.defineProperty(exports,'logResultsAsTreeWithElapsed',{enumerable:!0,get:function get(){return _bench.logResultsAsTreeWithElapsed}}),require('./init');var _logToDom=require('./utils/log-to-dom');function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}const experimental=exports.experimental={enableDOMLogging:_logToDom.enableDOMLogging}; |
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.formatSI=formatSI,exports.formatTime=formatTime,exports.leftPad=leftPad,exports.formatImage=formatImage,exports.formatValue=formatValue;const d3Format=require('d3-format');function formatSI(a){return d3Format.format('.3s')(a)}function formatTime(a){let b;return b=10>a?`${a.toFixed(2)}ms`:100>a?`${a.toFixed(1)}ms`:1e3>a?`${a.toFixed(0)}ms`:`${(a/1e3).toFixed(2)}s`,b}function leftPad(a,b=8){for(;a.length<b;)a=` ${a}`;return a}function formatImage(a,b,c){Math.floor;const d=a.width*c,e=a.height*c,f=a.src.replace(/\(/g,'%28').replace(/\)/g,'%29');return[`${b} %c+`,'font-size:1px;color:transparent;']}function formatValue(a,b={}){var c=Math.abs,d=b.isInteger;const e=d!==void 0&&d;if(Array.isArray(a)||ArrayBuffer.isView(a))return formatArrayValue(a,b);if(!Number.isFinite(a))return a+'';if(c(a)<1e-16)return e?'0':'0.';if(e)return a.toFixed(0);if(100<c(a)&&1e4>c(a))return a.toFixed(0);const f=a.toPrecision(2),g=f.indexOf('.0');return g===f.length-2?f.slice(0,-1):f}function formatArrayValue(a,b){var c=b.maxElts;const d=c===void 0?16:c;var e=b.size;const f=e===void 0?1:e;let g='[';for(let c=0;c<a.length&&c<d;++c)0<c&&(g+=`,${0==c%f?' ':''}`),g+=formatValue(a[c],b);const h=a.length>d?'...':']';return`${g}${h}`} | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.formatSI=formatSI,exports.formatTime=formatTime,exports.leftPad=leftPad,exports.rightPad=rightPad,exports.formatImage=formatImage,exports.formatValue=formatValue;const d3Format=require('d3-format');function formatSI(a){return d3Format.format('.3s')(a)}function formatTime(a){let b;return b=10>a?`${a.toFixed(2)}ms`:100>a?`${a.toFixed(1)}ms`:1e3>a?`${a.toFixed(0)}ms`:`${(a/1e3).toFixed(2)}s`,b}function leftPad(a,b=8){const c=Math.max(b-a.length,0);return`${' '.repeat(c)}${a}`}function rightPad(a,b=8){const c=Math.max(b-a.length,0);return`${a}${' '.repeat(c)}`}function formatImage(a,b,c){Math.floor;const d=a.width*c,e=a.height*c,f=a.src.replace(/\(/g,'%28').replace(/\)/g,'%29');return[`${b} %c+`,'font-size:1px;color:transparent;']}function formatValue(a,b={}){var c=Math.abs,d=b.isInteger;const e=d!==void 0&&d;if(Array.isArray(a)||ArrayBuffer.isView(a))return formatArrayValue(a,b);if(!Number.isFinite(a))return a+'';if(c(a)<1e-16)return e?'0':'0.';if(e)return a.toFixed(0);if(100<c(a)&&1e4>c(a))return a.toFixed(0);const f=a.toPrecision(2),g=f.indexOf('.0');return g===f.length-2?f.slice(0,-1):f}function formatArrayValue(a,b){var c=b.maxElts;const d=c===void 0?16:c;var e=b.size;const f=e===void 0?1:e;let g='[';for(let c=0;c<a.length&&c<d;++c)0<c&&(g+=`,${0==c%f?' ':''}`),g+=formatValue(a[c],b);const h=a.length>d?'...':']';return`${g}${h}`} |
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0});const isBrowser='object'!=typeof process||'[object process]'!==process+''||process.browser,window_='undefined'==typeof window?global:window,document_='undefined'==typeof document?{}:document,global_='undefined'==typeof global?window:global,process_='object'==typeof process?process:{},VERSION='0.0.3';exports.window=window_,exports.document=document_,exports.global=global_,exports.process=process_,exports.console=console,exports.isBrowser=isBrowser,exports.VERSION=VERSION; | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0});const isBrowser='object'!=typeof process||'[object process]'!==process+''||process.browser,window_='undefined'==typeof window?global:window,document_='undefined'==typeof document?{}:document,global_='undefined'==typeof global?window:global,process_='object'==typeof process?process:{},VERSION='0.0.4';exports.window=window_,exports.document=document_,exports.global=global_,exports.process=process_,exports.console=console,exports.isBrowser=isBrowser,exports.VERSION=VERSION; |
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.enableDOMLogging=enableDOMLogging,exports.logLineToDOM=logLineToDOM;var _globals=require('./globals');let old=null;function enableDOMLogging(a=!0){a&&!old&&(old=_globals.console.log.bind(_globals.console),_globals.console.log=(...a)=>{logLineToDOM(...a),old(...a)}),!a&&old&&(_globals.console.log=old,old=null)}let logDiv=null;function logLineToDOM(a){logDiv||(logDiv=_globals.document.createElement('div'));const b=_globals.document.body.childNodes;_globals.document.body.insertBefore(logDiv,b&&b[0]),'string'==typeof a&&(logDiv.innerHTML+=`${a}<br />`)}exports.default=enableDOMLogging; | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.enableDOMLogging=enableDOMLogging,exports.logLineToDOM=logLineToDOM;var _globals=require('./globals');let old=null;function enableDOMLogging(a=!0){a&&!old&&(old=_globals.console.log.bind(_globals.console),_globals.console.log=(...a)=>{logLineToDOM(...a),old(...a)}),!a&&old&&(_globals.console.log=old,old=null)}let logDiv=null;function logLineToDOM(a){if(!logDiv){const a=_globals.global.probe.markdown;logDiv=_globals.document.createElement(a?'pre':'div')}const b=_globals.document.body.childNodes;_globals.document.body.insertBefore(logDiv,b&&b[0]),'string'==typeof a&&(logDiv.innerHTML+=`${a}<br />`)}exports.default=enableDOMLogging; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "javascript", |
108
src/bench.js
/* eslint-disable no-console */ | ||
/* global setTimeout, console */ | ||
import {formatSI} from './utils/formatters'; | ||
import {formatSI, rightPad} from './utils/formatters'; | ||
import {global} from './utils/globals'; | ||
import {autobind} from './utils/autobind'; | ||
@@ -10,6 +11,12 @@ import LocalStorage from './utils/local-storage'; | ||
const TIME_THRESHOLD_MS = 30; // Minimum number of milliseconds to iterate each bench test | ||
const TIME_THRESHOLD_MS = 80; // Minimum number of milliseconds to iterate each bench test | ||
const TIME_COOLDOWN_MS = 5; // milliseconds of "cooldown" between tests | ||
const MIN_ITERATIONS = 1; // Increase if OK to let slow benchmarks take long time | ||
export const LOG_ENTRY = { | ||
GROUP: 'group', | ||
TEST: 'test', | ||
COMPLETE: 'complete' | ||
}; | ||
const CALIBRATION_TESTS = [ | ||
@@ -27,3 +34,3 @@ { | ||
id, // Name is needed for regression (storing/loading) | ||
log = console.log.bind(console), | ||
log, | ||
time = TIME_THRESHOLD_MS, | ||
@@ -33,2 +40,7 @@ delay = TIME_COOLDOWN_MS, | ||
} = {}) { | ||
if (!log) { | ||
const markdown = global.probe && global.probe.markdown; | ||
log = markdown ? logResultsAsMarkdownTable : logResultsAsTree; | ||
} | ||
this.id = id; | ||
@@ -55,5 +67,3 @@ this.opts = {log, time, delay, minIterations}; | ||
const elapsed = (new Date() - timer) / 1000; | ||
this.opts.log(''); | ||
this.opts.log(`Completed benchmark in ${elapsed}s`); | ||
logEntry(this, {entry: LOG_ENTRY.COMPLETE, time: elapsed}); | ||
this.onSuiteComplete(); | ||
@@ -71,3 +81,15 @@ }); | ||
add(id, func1, func2, opts) { | ||
// Signatures: | ||
// add(priority, id, initFunc, testFunc) | ||
// add(priority, id, testFunc) | ||
// add(id, initFunc, testFunc) | ||
// add(id, testFunc) | ||
add(priority, id, func1, func2) { | ||
if (typeof priority === 'string') { | ||
func2 = func1; | ||
func1 = id; | ||
id = priority; | ||
priority = 0; | ||
} | ||
assert(id); | ||
@@ -84,3 +106,3 @@ assert(typeof func1 === 'function'); | ||
assert(!this.tests[id], 'tests need unique id strings'); | ||
this.tests[id] = {id, initFunc, testFunc, opts: this.opts}; | ||
this.tests[id] = {id, priority, initFunc, testFunc, opts: this.opts}; | ||
return this; | ||
@@ -122,3 +144,3 @@ } | ||
const delta = current[id].current / saved[id].max; | ||
current[id].percent = `${Math.round(delta * 100)}%`; | ||
current[id].percent = `${Math.round(delta * 100 - 100)}%`; | ||
} else { | ||
@@ -163,4 +185,3 @@ current[id].max = current[id].current; | ||
if (test.group) { | ||
test.opts.log(''); | ||
test.opts.log(`${test.id}`); | ||
logEntry(test, {entry: LOG_ENTRY.GROUP, id: test.id}); | ||
} else { | ||
@@ -172,4 +193,5 @@ const {time, iterations} = runBenchTest(test); | ||
if (!silent) { | ||
test.opts.log( | ||
`├─ ${test.id}: ${itersPerSecond} iterations/s (${time.toFixed(2)}s elapsed)`); | ||
logEntry(test, { | ||
entry: LOG_ENTRY.TEST, id: test.id, priority: test.priority, itersPerSecond, time | ||
}); | ||
} | ||
@@ -196,3 +218,3 @@ | ||
function runBenchTest(test) { | ||
let iterations = test.opts.minIterations; | ||
let iterations = test.opts.minIterations / 10; | ||
let elapsedMillis = 0; | ||
@@ -203,4 +225,4 @@ | ||
let multiplier = 10; | ||
if (elapsedMillis > 1) { | ||
multiplier = (test.opts.time / elapsedMillis) * 1.1; | ||
if (elapsedMillis > 10) { | ||
multiplier = (test.opts.time / elapsedMillis) * 1.25; | ||
} | ||
@@ -234,1 +256,55 @@ iterations *= multiplier; | ||
function logEntry(test, opts) { | ||
const priority = (global.probe && global.probe.priority) | 10; | ||
if ((opts.priority | 0) <= priority) { | ||
test.opts.log(opts); | ||
} | ||
} | ||
export function logResultsAsMarkdownTable({entry, id, itersPerSecond, time}) { | ||
const COL1 = 50; | ||
const COL2 = 12; | ||
switch (entry) { | ||
case LOG_ENTRY.GROUP: | ||
console.log(''); | ||
console.log(`| ${rightPad(id, COL1)} | iterations/s |`); | ||
console.log(`| ${rightPad('---', COL1)} | --- |`); | ||
break; | ||
case LOG_ENTRY.TEST: | ||
console.log(`| ${rightPad(id, COL1)} | ${rightPad(itersPerSecond, COL2)} |`); | ||
break; | ||
case LOG_ENTRY.COMPLETE: | ||
console.log(''); | ||
console.log(`Completed benchmark in ${time}s`); | ||
break; | ||
default: | ||
} | ||
} | ||
export function logResultsAsTree({entry, id, itersPerSecond, time}) { | ||
switch (entry) { | ||
case LOG_ENTRY.GROUP: | ||
console.log(''); | ||
console.log(`${id}`); | ||
break; | ||
case LOG_ENTRY.TEST: | ||
console.log(`├─ ${id}: ${itersPerSecond} iterations/s`); | ||
break; | ||
case LOG_ENTRY.COMPLETE: | ||
console.log(''); | ||
console.log(`Completed benchmark in ${time}s`); | ||
break; | ||
default: | ||
} | ||
} | ||
export function logResultsAsTreeWithElapsed({entry, id, itersPerSecond, time}) { | ||
switch (entry) { | ||
case LOG_ENTRY.TEST: | ||
console.log(`├─ ${id}: ${itersPerSecond} iterations/s (${time.toFixed(2)}s elapsed)`); | ||
break; | ||
default: | ||
logResultsAsTree({entry, id, itersPerSecond, time}); | ||
} | ||
} | ||
@@ -0,1 +1,3 @@ | ||
import './init'; | ||
export {VERSION} from './utils/globals'; | ||
@@ -5,3 +7,8 @@ | ||
export {default as Bench} from './bench'; | ||
export { | ||
default as Bench, | ||
logResultsAsMarkdownTable, | ||
logResultsAsTree, | ||
logResultsAsTreeWithElapsed | ||
} from './bench'; | ||
@@ -8,0 +15,0 @@ // // Make Probe available as global variable for debugging purposes |
@@ -23,8 +23,11 @@ const d3Format = require('d3-format'); | ||
export function leftPad(string, length = 8) { | ||
while (string.length < length) { | ||
string = ` ${string}`; | ||
} | ||
return string; | ||
const padLength = Math.max(length - string.length, 0); | ||
return `${' '.repeat(padLength)}${string}`; | ||
} | ||
export function rightPad(string, length = 8) { | ||
const padLength = Math.max(length - string.length, 0); | ||
return `${string}${' '.repeat(padLength)}`; | ||
} | ||
// Inspired by https://github.com/hughsk/console-image (MIT license) | ||
@@ -31,0 +34,0 @@ export function formatImage(image, message, scale) { |
/* eslint-disable no-console */ | ||
import {document, console} from './globals'; | ||
import {document, console, global} from './globals'; | ||
@@ -26,3 +26,4 @@ let old = null; | ||
if (!logDiv) { | ||
logDiv = document.createElement('div'); | ||
const markdown = global.probe.markdown; | ||
logDiv = document.createElement(markdown ? 'pre' : 'div'); | ||
} | ||
@@ -29,0 +30,0 @@ // Ensure the element comes first |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63023
37
936
0