Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/benchmark.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 }
Expand Down
74 changes: 74 additions & 0 deletions tests/benchmark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
Loading