Skip to content

Commit 431aa3b

Browse files
authored
feat: add print option to benchmark (#58)
1 parent 00aaa98 commit 431aa3b

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

src/benchmark.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export interface BenchmarkOptions {
2222
skip?: boolean
2323
/** Number of warmup iterations to run before the benchmark (default: 0) */
2424
warmup?: number
25+
/** Print benchmark results to console (default: true) */
26+
print?: boolean
2527
}
2628

2729
/**

src/benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let gcWarned = false
2121
export async function benchmark(name, options, fn) {
2222
if (typeof options === 'function') [fn, options] = [options, undefined]
2323
if (options?.skip) return
24-
const { args, timeout = 1000, warmup = 0 } = options ?? {}
24+
const { args, timeout = 1000, warmup = 0, print = true } = options ?? {}
2525

2626
// This will pause us for a bit, but we don't care - having a non-busy process is more important
2727
await new Promise((resolve) => setTimeout(resolve, 0))
@@ -65,7 +65,7 @@ export async function benchmark(name, options, fn) {
6565
const rps = (1e9 * count) / Number(total) // Loss in precision to doubles on very fast ops, but this is better than mean rounding
6666
let res = `${name} x ${fRps(rps)} ops/sec @ ${fTime(mean)}/op`
6767
if (fTime(min) !== fTime(max)) res += ` (${fTime(min)}..${fTime(max)})`
68-
console.log(res)
68+
if (print) console.log(res)
6969

7070
if (gc) for (let i = 0; i < 4; i++) gc()
7171
return { rps, total, count, mean, min, max }

tests/benchmark.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,77 @@ test('benchmark warmup with zero value', async () => {
116116
console.log = originalLog
117117
}
118118
})
119+
120+
test('benchmark with print: false option', async () => {
121+
let callCount = 0
122+
const fn = () => {
123+
callCount++
124+
}
125+
126+
// Capture console.log output
127+
const originalLog = console.log
128+
let logged = ''
129+
console.log = (msg) => {
130+
logged += msg + '\n'
131+
}
132+
133+
try {
134+
await benchmark('test no print', { print: false, timeout: 10 }, fn)
135+
136+
// Check that no benchmark output was logged
137+
assert.strictEqual(logged, '', 'Expected no output when print: false')
138+
// But the benchmark should still run
139+
assert(callCount > 0, `Expected callCount > 0, got ${callCount}`)
140+
} finally {
141+
console.log = originalLog
142+
}
143+
})
144+
145+
test('benchmark with print: true option (explicit)', async () => {
146+
let callCount = 0
147+
const fn = () => {
148+
callCount++
149+
}
150+
151+
// Capture console.log output
152+
const originalLog = console.log
153+
let logged = ''
154+
console.log = (msg) => {
155+
logged += msg + '\n'
156+
}
157+
158+
try {
159+
await benchmark('test with print', { print: true, timeout: 10 }, fn)
160+
161+
// Check that benchmark output was logged
162+
assert(logged.includes('test with print'), 'Expected benchmark output when print: true')
163+
assert(callCount > 0, `Expected callCount > 0, got ${callCount}`)
164+
} finally {
165+
console.log = originalLog
166+
}
167+
})
168+
169+
test('benchmark with default print option (should print)', async () => {
170+
let callCount = 0
171+
const fn = () => {
172+
callCount++
173+
}
174+
175+
// Capture console.log output
176+
const originalLog = console.log
177+
let logged = ''
178+
console.log = (msg) => {
179+
logged += msg + '\n'
180+
}
181+
182+
try {
183+
// Not specifying print option should default to true
184+
await benchmark('test default print', { timeout: 10 }, fn)
185+
186+
// Check that benchmark output was logged by default
187+
assert(logged.includes('test default print'), 'Expected benchmark output by default')
188+
assert(callCount > 0, `Expected callCount > 0, got ${callCount}`)
189+
} finally {
190+
console.log = originalLog
191+
}
192+
})

0 commit comments

Comments
 (0)