FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

repl: add benchmarks · nodejs/node@825fcfe · GitHub

/ node Public

Commit 825fcfe

Browse files
authored andcommitted
repl: add benchmarks
Signed-off-by: avivkeller <me@aviv.sh> PR-URL: #64590 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0002f0a commit 825fcfe

6 files changed

Lines changed: 235 additions & 0 deletions

File tree

‎benchmark/repl/completion.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const repl = require('node:repl');
5+
const { PassThrough, Writable } = require('node:stream');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [5e3],
9+
query: [
10+
'cons',
11+
'console.lo',
12+
'Buffer.prototype.wri',
13+
"require('f",
14+
],
15+
useGlobal: [0, 1],
16+
});
17+
18+
function main({ n, query, useGlobal }) {
19+
const input = new PassThrough();
20+
const output = new Writable({ write(c, e, cb) { cb(); } });
21+
const server = new repl.REPLServer({
22+
input,
23+
output,
24+
terminal: false,
25+
useGlobal: !!useGlobal,
26+
});
27+
28+
// Inspector callbacks do not keep the event loop alive on their own.
29+
const keepAlive = setInterval(() => {}, 0x7fffffff);
30+
let remaining = n;
31+
32+
function complete() {
33+
server.complete(query, onComplete);
34+
}
35+
36+
function onComplete(err) {
37+
if (err) {
38+
throw err;
39+
}
40+
41+
if (--remaining === 0) {
42+
bench.end(n);
43+
clearInterval(keepAlive);
44+
server.close();
45+
return;
46+
}
47+
48+
setImmediate(complete);
49+
}
50+
51+
bench.start();
52+
setImmediate(complete);
53+
}

‎benchmark/repl/creation.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const repl = require('node:repl');
5+
const { PassThrough, Writable } = require('node:stream');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [500],
9+
preview: [0, 1],
10+
terminal: [0, 1],
11+
useGlobal: [0, 1],
12+
}, {
13+
combinationFilter: ({ preview, terminal }) => !!terminal || !preview,
14+
});
15+
16+
function main({ n, preview, terminal, useGlobal }) {
17+
const inputs = Array.from({ length: n }, () => new PassThrough());
18+
const outputs = Array.from(
19+
{ length: n },
20+
() => new Writable({ write(c, e, cb) { cb(); } }),
21+
);
22+
const servers = new Array(n);
23+
24+
bench.start();
25+
for (let i = 0; i < n; i++) {
26+
servers[i] = new repl.REPLServer({
27+
input: inputs[i],
28+
output: outputs[i],
29+
preview: !!preview,
30+
terminal: !!terminal,
31+
useGlobal: !!useGlobal,
32+
});
33+
}
34+
bench.end(n);
35+
36+
for (const server of servers) {
37+
server.close();
38+
}
39+
}

‎benchmark/repl/evaluate.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const repl = require('node:repl');
5+
const { PassThrough, Writable } = require('node:stream');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [2e4],
9+
code: [
10+
'1 + 1',
11+
'({ answer: 42 })',
12+
'Promise.resolve(42)',
13+
'await Promise.resolve(42)',
14+
],
15+
mode: ['sloppy', 'strict'],
16+
useGlobal: [0, 1],
17+
});
18+
19+
function main({ n, code, mode, useGlobal }) {
20+
const input = new PassThrough();
21+
const output = new Writable({ write(c, e, cb) { cb(); } });
22+
const server = new repl.REPLServer({
23+
input,
24+
output,
25+
replMode: mode === 'strict' ?
26+
repl.REPL_MODE_STRICT :
27+
repl.REPL_MODE_SLOPPY,
28+
terminal: false,
29+
useGlobal: !!useGlobal,
30+
});
31+
32+
// Inspector callbacks do not keep the event loop alive on their own.
33+
const keepAlive = setInterval(() => {}, 0x7fffffff);
34+
let remaining = n;
35+
36+
function evaluate() {
37+
server.eval(`${code}\n`, server.context, 'repl', onEvaluate);
38+
}
39+
40+
function onEvaluate(err) {
41+
if (err) {
42+
throw err;
43+
}
44+
45+
if (--remaining === 0) {
46+
bench.end(n);
47+
clearInterval(keepAlive);
48+
server.close();
49+
return;
50+
}
51+
52+
setImmediate(evaluate);
53+
}
54+
55+
bench.start();
56+
setImmediate(evaluate);
57+
}

‎benchmark/repl/process-lines.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const repl = require('node:repl');
5+
const { PassThrough, Writable } = require('node:stream');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e4],
9+
code: [
10+
'1 + 1\n',
11+
'Promise.resolve(42)\n',
12+
],
13+
mode: ['sloppy', 'strict'],
14+
terminal: [0, 1],
15+
useGlobal: [0, 1],
16+
});
17+
18+
function main({ n, code: inputCode, mode, terminal, useGlobal }) {
19+
const input = new PassThrough();
20+
const output = new Writable({ write(c, e, cb) { cb(); } });
21+
const server = new repl.REPLServer({
22+
input,
23+
output,
24+
replMode: mode === 'strict' ?
25+
repl.REPL_MODE_STRICT :
26+
repl.REPL_MODE_SLOPPY,
27+
terminal: !!terminal,
28+
useGlobal: !!useGlobal,
29+
});
30+
const originalEval = server.eval;
31+
// TTY input dispatch can briefly have no other active event loop handles.
32+
const keepAlive = setInterval(() => {}, 0x7fffffff);
33+
let remaining = n;
34+
35+
// eslint-disable-next-line node-core/func-name-matching
36+
server.eval = function REPLEval(code, context, file, callback) {
37+
originalEval(code, context, file, function onEvaluate() {
38+
const result = Reflect.apply(callback, this, arguments);
39+
if (--remaining === 0) {
40+
bench.end(n);
41+
clearInterval(keepAlive);
42+
server.close();
43+
} else {
44+
setImmediate(() => input.write(inputCode));
45+
}
46+
return result;
47+
});
48+
};
49+
50+
bench.start();
51+
input.write(inputCode);
52+
}

‎benchmark/repl/reset-context.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const repl = require('node:repl');
5+
const { PassThrough, Writable } = require('node:stream');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e3],
9+
});
10+
11+
function main({ n }) {
12+
const input = new PassThrough();
13+
const output = new Writable({ write(c, e, cb) { cb(); } });
14+
const server = new repl.REPLServer({
15+
input,
16+
output,
17+
terminal: false,
18+
});
19+
20+
bench.start();
21+
for (let i = 0; i < n; i++) {
22+
server.resetContext();
23+
}
24+
bench.end(n);
25+
server.close();
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const runBenchmark = require('../common/benchmark');
5+
6+
common.skipIfInspectorDisabled();
7+
8+
runBenchmark('repl', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL