Repository navigation
Expand file tree
/
Copy pathbenchmark.test.js
More file actions
192 lines (159 loc) · 4.96 KB
/
Copy pathbenchmark.test.js
File metadata and controls
192 lines (159 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { test } from 'node:test'
import { strict as assert } from 'node:assert'
import { benchmark } from '../src/benchmark.js'
test('benchmark with warmup 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 warmup', { warmup: 5, timeout: 10 }, fn)
// At least 5 warmup calls should have been made plus some benchmark calls
assert(callCount > 5, `Expected callCount > 5, got ${callCount}`)
// Check that benchmark output was logged
assert(logged.includes('test warmup'), 'Expected benchmark output')
} finally {
console.log = originalLog
}
})
test('benchmark without warmup option', async () => {
let callCount = 0
const fn = () => {
callCount++
}
// Capture console.log output
const originalLog = console.log
console.log = () => {} // Suppress output
try {
await benchmark('test no warmup', { timeout: 10 }, fn)
// Only benchmark calls should have been made (no warmup)
assert(callCount > 0, `Expected callCount > 0, got ${callCount}`)
} finally {
console.log = originalLog
}
})
test('benchmark warmup with args', async () => {
const callArgs = []
const fn = (arg) => {
callArgs.push(arg)
}
// Capture console.log output
const originalLog = console.log
console.log = () => {} // Suppress output
try {
const args = ['a', 'b', 'c']
await benchmark('test warmup with args', { args, warmup: 7, timeout: 10 }, fn)
// Check that warmup used the args in the correct order (cycling through)
// First 7 calls are warmup: a, b, c, a, b, c, a
assert.strictEqual(callArgs[0], 'a')
assert.strictEqual(callArgs[1], 'b')
assert.strictEqual(callArgs[2], 'c')
assert.strictEqual(callArgs[3], 'a')
assert.strictEqual(callArgs[4], 'b')
assert.strictEqual(callArgs[5], 'c')
assert.strictEqual(callArgs[6], 'a')
} finally {
console.log = originalLog
}
})
test('benchmark warmup with async function', async () => {
let callCount = 0
const fn = async () => {
callCount++
await new Promise((resolve) => setTimeout(resolve, 1))
}
// Capture console.log output
const originalLog = console.log
console.log = () => {} // Suppress output
try {
await benchmark('test async warmup', { warmup: 3, timeout: 10 }, fn)
// At least 3 warmup calls should have been made plus some benchmark calls
assert(callCount > 3, `Expected callCount > 3, got ${callCount}`)
} finally {
console.log = originalLog
}
})
test('benchmark warmup with zero value', async () => {
let callCount = 0
const fn = () => {
callCount++
}
// Capture console.log output
const originalLog = console.log
console.log = () => {} // Suppress output
try {
await benchmark('test zero warmup', { warmup: 0, timeout: 10 }, fn)
// Only benchmark calls should have been made (no warmup)
assert(callCount > 0, `Expected callCount > 0, got ${callCount}`)
} finally {
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
}
})