Skip to content

Commit abfe3f0

Browse files
committed
lib: FixedQueue add a benchmark
1 parent b10855f commit abfe3f0

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// Oscillates the FixedQueue between N and N+1 segments repeatedly to
6+
// exercise the spare-segment reuse path: push k items to force a new segment,
7+
// then shift k items to empty one segment; repeat.
8+
const bench = common.createBenchmark(main, {
9+
cycles: [5e3],
10+
}, { flags: ['--expose-internals'] });
11+
12+
function main({ cycles }) {
13+
const FixedQueue = require('internal/fixed_queue');
14+
const q = new FixedQueue();
15+
// Derive the internal segment size (kSize) from the current head.
16+
const k = q.head.list.length;
17+
18+
bench.start();
19+
for (let c = 0; c < cycles; c++) {
20+
// Grow to N+1 by pushing k items: k-1 fills the current segment,
21+
// the k-th push creates/uses the next segment.
22+
for (let i = 0; i < k; i++) q.push(i);
23+
// Shrink back to N by shifting k items: empties one segment and advances.
24+
for (let i = 0; i < k; i++) q.shift();
25+
}
26+
// Report total operations (push + shift) to normalize throughput.
27+
bench.end(cycles * 2 * k);
28+
}

benchmark/util/fixed-queue.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1e5],
7+
}, { flags: ['--expose-internals'] });
8+
9+
function main({ n }) {
10+
const FixedQueue = require('internal/fixed_queue');
11+
const queue = new FixedQueue();
12+
bench.start();
13+
for (let i = 0; i < n; i++)
14+
queue.push(i);
15+
for (let i = 0; i < n; i++)
16+
queue.shift();
17+
bench.end(n);
18+
}

0 commit comments

Comments
 (0)