From 340c16da0cfb81790f8b4d0c32374c9891729dfe Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Fri, 10 May 2024 15:47:45 +0200 Subject: [PATCH] feat: remove `done.resolve()` 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) --- scripts/bench.ts | 25 +++++++++---------- scripts/demoBench.ts | 25 +++++++++---------- scripts/filterBench.script.ts | 19 --------------- scripts/nanoidBench.ts | 21 ++++++---------- scripts/startsWith.ts | 10 ++------ src/bench.model.ts | 12 +++------ src/bench.profles.ts | 14 +++-------- src/bench.test.ts | 12 ++++----- src/bench.util.ts | 2 +- src/index.ts | 46 +++++------------------------------ 10 files changed, 54 insertions(+), 132 deletions(-) delete mode 100644 scripts/filterBench.script.ts diff --git a/scripts/bench.ts b/scripts/bench.ts index 089a16b..9452397 100644 --- a/scripts/bench.ts +++ b/scripts/bench.ts @@ -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(resolve => resolve()) - done.resolve() }, + // timeout: done => { + // setTimeout(() => done.resolve(), 0) + // }, + // immediate: done => { + // setImmediate(() => done.resolve()) + // }, + // asyncAwait: async done => { + // await new Promise(resolve => resolve()) + // done.resolve() + // }, }, runs: 2, // writePlot: true, diff --git a/scripts/demoBench.ts b/scripts/demoBench.ts index f557b5a..421402f 100644 --- a/scripts/demoBench.ts +++ b/scripts/demoBench.ts @@ -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(resolve => resolve()) - done.resolve() - }, - immediate: done => { - setImmediate(() => done.resolve()) }, + // timeout: done => { + // setTimeout(() => done.resolve(), 0) + // }, + // asyncAwait: async done => { + // await new Promise(resolve => resolve()) + // done.resolve() + // }, + // immediate: done => { + // setImmediate(() => done.resolve()) + // }, }, runs: 1, reportDirPath: './demo', diff --git a/scripts/filterBench.script.ts b/scripts/filterBench.script.ts deleted file mode 100644 index 5ee7245..0000000 --- a/scripts/filterBench.script.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - -yarn tsn filterBench - - */ - -import { runBenchScript } from '../src' - -runBenchScript({ - fns: { - a: done => { - done.resolve() - }, - b: done => { - done.resolve() - }, - }, - runs: 2, -}) diff --git a/scripts/nanoidBench.ts b/scripts/nanoidBench.ts index a33d77f..2187ded 100644 --- a/scripts/nanoidBench.ts +++ b/scripts/nanoidBench.ts @@ -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, }) diff --git a/scripts/startsWith.ts b/scripts/startsWith.ts index ae7c568..33569fd 100644 --- a/scripts/startsWith.ts +++ b/scripts/startsWith.ts @@ -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, }) diff --git a/src/bench.model.ts b/src/bench.model.ts index 01997a3..6a29aa7 100644 --- a/src/bench.model.ts +++ b/src/bench.model.ts @@ -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 + fns: Record /** * How many times to run. diff --git a/src/bench.profles.ts b/src/bench.profles.ts index cdce4f7..7e624d0 100644 --- a/src/bench.profles.ts +++ b/src/bench.profles.ts @@ -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() } diff --git a/src/bench.test.ts b/src/bench.test.ts index 4855442..b0eaa65 100644 --- a/src/bench.test.ts +++ b/src/bench.test.ts @@ -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, @@ -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) diff --git a/src/bench.util.ts b/src/bench.util.ts index bcc917a..24a7c0d 100644 --- a/src/bench.util.ts +++ b/src/bench.util.ts @@ -101,7 +101,7 @@ async function runBenchOnce(opt: RunBenchOptions, run: number): Promise { suite.add(opt.fns[name]!, { - defer: true, + defer: false, // used to be true name, }) }) diff --git a/src/index.ts b/src/index.ts index 0d340e0..7ad19a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'