Skip to content

Commit

Permalink
feat: remove done.resolve()
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
now we only support non-defer functions, which removes the need
to call `done.resolve()`

Async ("defer") functions don't really fit the case of micro-benchmarking,
they better be tested with e.g Autocannot (runCannon)
  • Loading branch information
kirillgroshkov committed May 10, 2024
1 parent 52b60ad commit 340c16d
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 132 deletions.
25 changes: 12 additions & 13 deletions scripts/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ runScript(async () => {
// // await pDelay()
// done.resolve()
// },
noop: done => done.resolve(),
random: done => {
noop: () => {},
random: () => {
const _ = Math.random()
done.resolve()
},
timeout: done => {
setTimeout(() => done.resolve(), 0)
},
immediate: done => {
setImmediate(() => done.resolve())
},
asyncAwait: async done => {
await new Promise<void>(resolve => resolve())
done.resolve()
},
// timeout: done => {
// setTimeout(() => done.resolve(), 0)
// },
// immediate: done => {
// setImmediate(() => done.resolve())
// },
// asyncAwait: async done => {
// await new Promise<void>(resolve => resolve())
// done.resolve()
// },
},
runs: 2,
// writePlot: true,
Expand Down
25 changes: 12 additions & 13 deletions scripts/demoBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import { runBenchScript } from '../src'

runBenchScript({
fns: {
noop: done => done.resolve(),
random: done => {
noop: () => {},
random: () => {
const _ = Math.random()
done.resolve()
},
timeout: done => {
setTimeout(() => done.resolve(), 0)
},
asyncAwait: async done => {
await new Promise<void>(resolve => resolve())
done.resolve()
},
immediate: done => {
setImmediate(() => done.resolve())
},
// timeout: done => {
// setTimeout(() => done.resolve(), 0)
// },
// asyncAwait: async done => {
// await new Promise<void>(resolve => resolve())
// done.resolve()
// },
// immediate: done => {
// setImmediate(() => done.resolve())
// },
},
runs: 1,
reportDirPath: './demo',
Expand Down
19 changes: 0 additions & 19 deletions scripts/filterBench.script.ts

This file was deleted.

21 changes: 8 additions & 13 deletions scripts/nanoidBench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@ yarn tsn nanoidBench
// import { nanoid as nanoidNonSecure } from 'nanoid/non-secure'
import { nanoid } from '@naturalcycles/nodejs-lib'
import { runBenchScript } from '../src'
const { nanoid: nanoidAsync } = require('nanoid/async')
const { nanoid: nanoidNonSecure } = require('nanoid/non-secure')

/* eslint-disable no-useless-assignment */

runBenchScript({
fns: {
nanoidNonSecure: done => {
nanoidNonSecure: () => {
let _count = 0
const id = nanoidNonSecure()
if (id) _count++
done.resolve()
},
nanoidSync: done => {
nanoidSync: () => {
let _count = 0
const id = nanoid()
if (id) _count++
done.resolve()
},
nanoidAsync: async done => {
let _count = 0
const id = await nanoidAsync()
if (id) _count++
done.resolve()
},
// nanoidAsync: async done => {
// let _count = 0
// const id = await nanoidAsync()
// if (id) _count++
// done.resolve()
// },
},
runs: 2,
asciiPlot: true,
})
10 changes: 2 additions & 8 deletions scripts/startsWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@ const strings = _range(100).map(String)

runBenchScript({
fns: {
startsWith: done => {
startsWith: () => {
let _count = 0
strings.forEach(s => {
if (s.startsWith('2')) _count++
})

done.resolve()
},
indexOf: done => {
indexOf: () => {
let _count = 0
strings.forEach(s => {
if (s.indexOf('2') === 0) _count++
})

done.resolve()
},
},
runs: 1,
asciiPlot: true,
})
12 changes: 3 additions & 9 deletions src/bench.model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
export interface BenchDeferred {
resolve: () => void
}

export type BenchDeferredFunction = (done: BenchDeferred) => any
export type BenchFunction = () => void

export interface RunBenchOptions {
/**
* Functions to benchmark.
* Record from function name to a `BenchDeferredFunction`.
* BenchDeferredFunction takes `done` callback as a first argument,
* `done.resolve()` HAS to be called at the end of your function.
* Record from function name to a `BenchFunction`.
*/
fns: Record<string, BenchDeferredFunction>
fns: Record<string, BenchFunction>

/**
* How many times to run.
Expand Down
14 changes: 4 additions & 10 deletions src/bench.profles.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { pDelay, _randomInt } from '@naturalcycles/js-lib'
import { BenchDeferredFunction } from './bench.model'
import { _randomInt } from '@naturalcycles/js-lib'
import { BenchFunction } from './bench.model'

export const benchNoopFn: BenchDeferredFunction = done => done.resolve()
export const benchNoopFn: BenchFunction = () => {}

export const benchRandomFn: BenchDeferredFunction = done => {
export const benchRandomFn: BenchFunction = () => {
const _ = _randomInt(1, 10)
done.resolve()
}

export const benchPDelayFn: BenchDeferredFunction = async done => {
await pDelay()
done.resolve()
}
12 changes: 6 additions & 6 deletions src/bench.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ test('runBench', async () => {

const r = await runBench({
fns: {
noop: done => done.resolve(),
immediate: done => {
setImmediate(() => done.resolve())
},
noop: () => {},
// immediate: done => {
// setImmediate(() => done.resolve())
// },
},
runs: 1,
reportDirPath,
Expand All @@ -21,13 +21,13 @@ test('runBench', async () => {

expect(r).toMatchObject({
noop: expect.any(Number),
immediate: expect.any(Number),
// immediate: expect.any(Number),
})

const summary = fs2.readJson(`${reportDirPath}/runBench.json`)
expect(summary).toMatchObject({
noop: expect.any(Number),
immediate: expect.any(Number),
// immediate: expect.any(Number),
})

// expect(fs2.pathExists(`${reportDirPath}/runBench.svg`)).toBe(true)
Expand Down
2 changes: 1 addition & 1 deletion src/bench.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async function runBenchOnce(opt: RunBenchOptions, run: number): Promise<HertzMap
if (run % 2 === 0) fnNames.reverse()
fnNames.forEach(name => {
suite.add(opt.fns[name]!, {
defer: true,
defer: false, // used to be true
name,
})
})
Expand Down
46 changes: 6 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
import { BenchDeferredFunction, HertzMap, RunBenchOptions } from './bench.model'
import { benchNoopFn } from './bench.profles'
import { runBench, runBenchScript } from './bench.util'
import {
AutocannonOptions,
AutocannonResult,
AutocannonSummary,
HttpServerFactory,
RunCannonOptions,
} from './cannon.model'
import {
bareExpressServerFactory,
bareNodeServerFactory,
expressFunctionFactory,
expressWithMiddlewaresServerFactory,
} from './cannon.profiles'
import { runCannon, runCannonScript } from './cannon.util'

export type {
RunBenchOptions,
BenchDeferredFunction,
HertzMap,
HttpServerFactory,
RunCannonOptions,
AutocannonSummary,
AutocannonResult,
AutocannonOptions,
}

export {
runBenchScript,
runBench,
benchNoopFn,
runCannon,
runCannonScript,
bareNodeServerFactory,
bareExpressServerFactory,
expressWithMiddlewaresServerFactory,
expressFunctionFactory,
}
export * from './bench.model'
export * from './bench.profles'
export * from './bench.util'
export * from './cannon.model'
export * from './cannon.profiles'
export * from './cannon.util'

0 comments on commit 340c16d

Please sign in to comment.