Skip to content

Commit df13753

Browse files
committed
refactor(benchmark): allow to take duration as a params
1 parent 15cdc86 commit df13753

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

benchmark/boringnode/harness.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises'
12
import { Redis } from 'ioredis'
23
import { Worker } from '#src/worker'
34
import { Job } from '#src/job'
@@ -6,12 +7,15 @@ import { redis } from '#drivers/redis_adapter'
67
import { barrier, type BenchmarkOptions, type BenchmarkResult } from '../helpers.ts'
78
import type { QueueManagerConfig } from '#types/main'
89

9-
// Barrier callback - set before each benchmark run
10+
// Barrier callback and job duration - set before each benchmark run
1011
let onJobComplete: (() => boolean) | null = null
12+
let jobDuration: number = 0
1113

1214
class BenchmarkJob extends Job<{ i: number }> {
1315
async execute() {
14-
// No-op - just measure queue overhead
16+
if (jobDuration > 0) {
17+
await setTimeout(jobDuration)
18+
}
1519
onJobComplete?.()
1620
}
1721
}
@@ -37,9 +41,10 @@ export async function run(options: BenchmarkOptions): Promise<BenchmarkResult> {
3741

3842
await clearQueue(host, port)
3943

40-
// Setup barrier for completion tracking
44+
// Setup barrier for completion tracking and job duration
4145
const { done, next } = barrier(options.numRuns)
4246
onJobComplete = next
47+
jobDuration = options.jobDuration ?? 0
4348

4449
const config: QueueManagerConfig = {
4550
default: 'redis',

benchmark/bullmq/harness.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises'
12
import { Queue, Worker, type ConnectionOptions } from 'bullmq'
23
import { Redis } from 'ioredis'
34
import { barrier, type BenchmarkOptions, type BenchmarkResult } from '../helpers.ts'
@@ -36,10 +37,13 @@ export async function run(options: BenchmarkOptions): Promise<BenchmarkResult> {
3637
const startTime = Date.now()
3738

3839
// Create worker AFTER all jobs are enqueued (pure dequeue test)
40+
const jobDuration = options.jobDuration ?? 0
3941
const worker = new Worker(
4042
'benchmark',
4143
async () => {
42-
// No-op - just measure queue overhead
44+
if (jobDuration > 0) {
45+
await setTimeout(jobDuration)
46+
}
4347
next()
4448
},
4549
{

benchmark/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function barrier(n: number = 1) {
3737
export interface BenchmarkOptions {
3838
numRuns: number
3939
concurrency: number
40+
jobDuration?: number // Simulated work duration in ms
4041
}
4142

4243
export interface BenchmarkResult {

benchmark/run.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { run as runBullMQ } from './bullmq/harness.ts'
55
interface BenchmarkConfig {
66
numRuns: number[]
77
concurrency: number[]
8+
jobDuration?: number // Simulated work duration in ms
89
}
910

1011
const defaultConfig: BenchmarkConfig = {
@@ -17,12 +18,15 @@ async function runBenchmarks(config: BenchmarkConfig = defaultConfig) {
1718

1819
console.log('='.repeat(60))
1920
console.log('Queue Benchmark Suite')
21+
if (config.jobDuration) {
22+
console.log(`Job duration: ${config.jobDuration}ms`)
23+
}
2024
console.log('='.repeat(60))
2125
console.log()
2226

2327
for (const numRuns of config.numRuns) {
2428
for (const concurrency of config.concurrency) {
25-
const options: BenchmarkOptions = { numRuns, concurrency }
29+
const options: BenchmarkOptions = { numRuns, concurrency, jobDuration: config.jobDuration }
2630

2731
console.log(`\nBenchmark: ${numRuns} jobs, concurrency ${concurrency}`)
2832
console.log('-'.repeat(50))
@@ -112,6 +116,19 @@ if (args.includes('--quick')) {
112116
numRuns: [100, 500, 1000, 2500, 5000, 10000],
113117
concurrency: [1, 5, 10, 25, 50],
114118
}
119+
} else if (args.includes('--realistic')) {
120+
// Simulate real jobs with 5ms work duration
121+
config = {
122+
numRuns: [100, 500, 1000],
123+
concurrency: [1, 5, 10],
124+
jobDuration: 5,
125+
}
126+
}
127+
128+
// Allow --duration=N to set job duration
129+
const durationArg = args.find((arg) => arg.startsWith('--duration='))
130+
if (durationArg) {
131+
config.jobDuration = Number.parseInt(durationArg.split('=')[1], 10)
115132
}
116133

117134
runBenchmarks(config)

0 commit comments

Comments
 (0)