|
| 1 | +// Flags: --experimental-bench --no-warnings |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { createRunner } = require('node:bench'); |
| 7 | + |
| 8 | +function waitForTick() { |
| 9 | + const started = process.hrtime.bigint(); |
| 10 | + while (process.hrtime.bigint() === started); |
| 11 | +} |
| 12 | + |
| 13 | +const runner = createRunner({ yieldBetweenSamples: false }); |
| 14 | + |
| 15 | +runner.bench('default operations', { samples: 1, warmup: 0 }, |
| 16 | + common.mustCall((b) => { |
| 17 | + b.start(); |
| 18 | + waitForTick(); |
| 19 | + const sample = b.end(); |
| 20 | + assert.strictEqual(sample.operations, 1); |
| 21 | + })); |
| 22 | + |
| 23 | +runner.bench('undefined operations with options', { samples: 1, warmup: 0 }, |
| 24 | + common.mustCall((b) => { |
| 25 | + b.start(); |
| 26 | + waitForTick(); |
| 27 | + const sample = b.end(undefined, { detail: 'value' }); |
| 28 | + assert.strictEqual(sample.operations, 1); |
| 29 | + assert.strictEqual(sample.detail, 'value'); |
| 30 | + })); |
| 31 | + |
| 32 | +const invalid = [0, -1, 0.5, NaN, Infinity, null, '1']; |
| 33 | +for (const operations of invalid) { |
| 34 | + runner.bench(`invalid ${String(operations)}`, { samples: 1, warmup: 0 }, |
| 35 | + (b) => { |
| 36 | + b.start(); |
| 37 | + waitForTick(); |
| 38 | + b.end(operations); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +runner.bench('record default operations', { samples: 1, warmup: 0 }, |
| 43 | + common.mustCall((b) => { |
| 44 | + const sample = b.record({ duration_ns: 10n }); |
| 45 | + assert.strictEqual(sample.operations, 1); |
| 46 | + assert.strictEqual(sample.rate, 100_000_000); |
| 47 | + })); |
| 48 | + |
| 49 | +runner.bench('record undefined operations', { samples: 1, warmup: 0 }, |
| 50 | + common.mustCall((b) => { |
| 51 | + const sample = b.record({ duration_ns: 10n, operations: undefined }); |
| 52 | + assert.strictEqual(sample.operations, 1); |
| 53 | + })); |
| 54 | + |
| 55 | +for (const operations of invalid) { |
| 56 | + runner.bench(`record invalid ${String(operations)}`, |
| 57 | + { samples: 1, warmup: 0 }, |
| 58 | + (b) => b.record({ duration_ns: 10n, operations })); |
| 59 | +} |
| 60 | + |
| 61 | +(async () => { |
| 62 | + const records = await runner.run().toArray(); |
| 63 | + const byName = new Map(records |
| 64 | + .filter(({ type }) => type === 'bench:complete') |
| 65 | + .map(({ data }) => [data.name, data])); |
| 66 | + |
| 67 | + assert.deepStrictEqual( |
| 68 | + byName.get('default operations').samples.map(({ operations }) => operations), |
| 69 | + [1]); |
| 70 | + assert.deepStrictEqual( |
| 71 | + byName.get('undefined operations with options').samples |
| 72 | + .map(({ operations }) => operations), |
| 73 | + [1]); |
| 74 | + |
| 75 | + for (const name of ['record default operations', |
| 76 | + 'record undefined operations']) { |
| 77 | + assert.deepStrictEqual( |
| 78 | + byName.get(name).samples.map(({ operations }) => operations), [1]); |
| 79 | + } |
| 80 | + |
| 81 | + for (const operations of invalid) { |
| 82 | + const code = typeof operations === 'number' ? |
| 83 | + 'ERR_OUT_OF_RANGE' : 'ERR_INVALID_ARG_TYPE'; |
| 84 | + for (const prefix of ['invalid', 'record invalid']) { |
| 85 | + const { error, samples } = |
| 86 | + byName.get(`${prefix} ${String(operations)}`); |
| 87 | + assert.strictEqual(error.code, code); |
| 88 | + assert.strictEqual(samples.length, 0); |
| 89 | + } |
| 90 | + } |
| 91 | +})().then(common.mustCall()); |
0 commit comments