diff --git a/src/benchmark.d.ts b/src/benchmark.d.ts index b933124..672462a 100644 --- a/src/benchmark.d.ts +++ b/src/benchmark.d.ts @@ -22,6 +22,8 @@ export interface BenchmarkOptions { skip?: boolean /** Number of warmup iterations to run before the benchmark (default: 0) */ warmup?: number + /** Print benchmark results to console (default: true) */ + print?: boolean } /** diff --git a/src/benchmark.js b/src/benchmark.js index 62a6749..59ffd8c 100644 --- a/src/benchmark.js +++ b/src/benchmark.js @@ -21,7 +21,7 @@ let gcWarned = false export async function benchmark(name, options, fn) { if (typeof options === 'function') [fn, options] = [options, undefined] if (options?.skip) return - const { args, timeout = 1000, warmup = 0 } = options ?? {} + const { args, timeout = 1000, warmup = 0, print = true } = options ?? {} // This will pause us for a bit, but we don't care - having a non-busy process is more important await new Promise((resolve) => setTimeout(resolve, 0)) @@ -65,7 +65,7 @@ export async function benchmark(name, options, fn) { const rps = (1e9 * count) / Number(total) // Loss in precision to doubles on very fast ops, but this is better than mean rounding let res = `${name} x ${fRps(rps)} ops/sec @ ${fTime(mean)}/op` if (fTime(min) !== fTime(max)) res += ` (${fTime(min)}..${fTime(max)})` - console.log(res) + if (print) console.log(res) if (gc) for (let i = 0; i < 4; i++) gc() return { rps, total, count, mean, min, max } diff --git a/tests/benchmark.test.js b/tests/benchmark.test.js index a0f8c8a..662056d 100644 --- a/tests/benchmark.test.js +++ b/tests/benchmark.test.js @@ -116,3 +116,77 @@ test('benchmark warmup with zero value', async () => { console.log = originalLog } }) + +test('benchmark with print: false option', async () => { + let callCount = 0 + const fn = () => { + callCount++ + } + + // Capture console.log output + const originalLog = console.log + let logged = '' + console.log = (msg) => { + logged += msg + '\n' + } + + try { + await benchmark('test no print', { print: false, timeout: 10 }, fn) + + // Check that no benchmark output was logged + assert.strictEqual(logged, '', 'Expected no output when print: false') + // But the benchmark should still run + assert(callCount > 0, `Expected callCount > 0, got ${callCount}`) + } finally { + console.log = originalLog + } +}) + +test('benchmark with print: true option (explicit)', async () => { + let callCount = 0 + const fn = () => { + callCount++ + } + + // Capture console.log output + const originalLog = console.log + let logged = '' + console.log = (msg) => { + logged += msg + '\n' + } + + try { + await benchmark('test with print', { print: true, timeout: 10 }, fn) + + // Check that benchmark output was logged + assert(logged.includes('test with print'), 'Expected benchmark output when print: true') + assert(callCount > 0, `Expected callCount > 0, got ${callCount}`) + } finally { + console.log = originalLog + } +}) + +test('benchmark with default print option (should print)', async () => { + let callCount = 0 + const fn = () => { + callCount++ + } + + // Capture console.log output + const originalLog = console.log + let logged = '' + console.log = (msg) => { + logged += msg + '\n' + } + + try { + // Not specifying print option should default to true + await benchmark('test default print', { timeout: 10 }, fn) + + // Check that benchmark output was logged by default + assert(logged.includes('test default print'), 'Expected benchmark output by default') + assert(callCount > 0, `Expected callCount > 0, got ${callCount}`) + } finally { + console.log = originalLog + } +})