mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
repl: add benchmarks
Signed-off-by: avivkeller <me@aviv.sh> PR-URL: https://github.com/nodejs/node/pull/64590 Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
6ff69baea8
commit
cd1e6ce29b
6 files changed
+235
No files matched your search
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const repl = require('node:repl');
|
||||
const { PassThrough, Writable } = require('node:stream');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [5e3],
|
||||
query: [
|
||||
'cons',
|
||||
'console.lo',
|
||||
'Buffer.prototype.wri',
|
||||
"require('f",
|
||||
],
|
||||
useGlobal: [0, 1],
|
||||
});
|
||||
|
||||
function main({ n, query, useGlobal }) {
|
||||
const input = new PassThrough();
|
||||
const output = new Writable({ write(c, e, cb) { cb(); } });
|
||||
const server = new repl.REPLServer({
|
||||
input,
|
||||
output,
|
||||
terminal: false,
|
||||
useGlobal: !!useGlobal,
|
||||
});
|
||||
|
||||
// Inspector callbacks do not keep the event loop alive on their own.
|
||||
const keepAlive = setInterval(() => {}, 0x7fffffff);
|
||||
let remaining = n;
|
||||
|
||||
function complete() {
|
||||
server.complete(query, onComplete);
|
||||
}
|
||||
|
||||
function onComplete(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (--remaining === 0) {
|
||||
bench.end(n);
|
||||
clearInterval(keepAlive);
|
||||
server.close();
|
||||
return;
|
||||
}
|
||||
|
||||
setImmediate(complete);
|
||||
}
|
||||
|
||||
bench.start();
|
||||
setImmediate(complete);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const repl = require('node:repl');
|
||||
const { PassThrough, Writable } = require('node:stream');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [500],
|
||||
preview: [0, 1],
|
||||
terminal: [0, 1],
|
||||
useGlobal: [0, 1],
|
||||
}, {
|
||||
combinationFilter: ({ preview, terminal }) => !!terminal || !preview,
|
||||
});
|
||||
|
||||
function main({ n, preview, terminal, useGlobal }) {
|
||||
const inputs = Array.from({ length: n }, () => new PassThrough());
|
||||
const outputs = Array.from(
|
||||
{ length: n },
|
||||
() => new Writable({ write(c, e, cb) { cb(); } }),
|
||||
);
|
||||
const servers = new Array(n);
|
||||
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
servers[i] = new repl.REPLServer({
|
||||
input: inputs[i],
|
||||
output: outputs[i],
|
||||
preview: !!preview,
|
||||
terminal: !!terminal,
|
||||
useGlobal: !!useGlobal,
|
||||
});
|
||||
}
|
||||
bench.end(n);
|
||||
|
||||
for (const server of servers) {
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const repl = require('node:repl');
|
||||
const { PassThrough, Writable } = require('node:stream');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [2e4],
|
||||
code: [
|
||||
'1 + 1',
|
||||
'({ answer: 42 })',
|
||||
'Promise.resolve(42)',
|
||||
'await Promise.resolve(42)',
|
||||
],
|
||||
mode: ['sloppy', 'strict'],
|
||||
useGlobal: [0, 1],
|
||||
});
|
||||
|
||||
function main({ n, code, mode, useGlobal }) {
|
||||
const input = new PassThrough();
|
||||
const output = new Writable({ write(c, e, cb) { cb(); } });
|
||||
const server = new repl.REPLServer({
|
||||
input,
|
||||
output,
|
||||
replMode: mode === 'strict' ?
|
||||
repl.REPL_MODE_STRICT :
|
||||
repl.REPL_MODE_SLOPPY,
|
||||
terminal: false,
|
||||
useGlobal: !!useGlobal,
|
||||
});
|
||||
|
||||
// Inspector callbacks do not keep the event loop alive on their own.
|
||||
const keepAlive = setInterval(() => {}, 0x7fffffff);
|
||||
let remaining = n;
|
||||
|
||||
function evaluate() {
|
||||
server.eval(`${code}\n`, server.context, 'repl', onEvaluate);
|
||||
}
|
||||
|
||||
function onEvaluate(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (--remaining === 0) {
|
||||
bench.end(n);
|
||||
clearInterval(keepAlive);
|
||||
server.close();
|
||||
return;
|
||||
}
|
||||
|
||||
setImmediate(evaluate);
|
||||
}
|
||||
|
||||
bench.start();
|
||||
setImmediate(evaluate);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const repl = require('node:repl');
|
||||
const { PassThrough, Writable } = require('node:stream');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [1e4],
|
||||
code: [
|
||||
'1 + 1\n',
|
||||
'Promise.resolve(42)\n',
|
||||
],
|
||||
mode: ['sloppy', 'strict'],
|
||||
terminal: [0, 1],
|
||||
useGlobal: [0, 1],
|
||||
});
|
||||
|
||||
function main({ n, code: inputCode, mode, terminal, useGlobal }) {
|
||||
const input = new PassThrough();
|
||||
const output = new Writable({ write(c, e, cb) { cb(); } });
|
||||
const server = new repl.REPLServer({
|
||||
input,
|
||||
output,
|
||||
replMode: mode === 'strict' ?
|
||||
repl.REPL_MODE_STRICT :
|
||||
repl.REPL_MODE_SLOPPY,
|
||||
terminal: !!terminal,
|
||||
useGlobal: !!useGlobal,
|
||||
});
|
||||
const originalEval = server.eval;
|
||||
// TTY input dispatch can briefly have no other active event loop handles.
|
||||
const keepAlive = setInterval(() => {}, 0x7fffffff);
|
||||
let remaining = n;
|
||||
|
||||
// eslint-disable-next-line node-core/func-name-matching
|
||||
server.eval = function REPLEval(code, context, file, callback) {
|
||||
originalEval(code, context, file, function onEvaluate() {
|
||||
const result = Reflect.apply(callback, this, arguments);
|
||||
if (--remaining === 0) {
|
||||
bench.end(n);
|
||||
clearInterval(keepAlive);
|
||||
server.close();
|
||||
} else {
|
||||
setImmediate(() => input.write(inputCode));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
};
|
||||
|
||||
bench.start();
|
||||
input.write(inputCode);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const repl = require('node:repl');
|
||||
const { PassThrough, Writable } = require('node:stream');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [1e3],
|
||||
});
|
||||
|
||||
function main({ n }) {
|
||||
const input = new PassThrough();
|
||||
const output = new Writable({ write(c, e, cb) { cb(); } });
|
||||
const server = new repl.REPLServer({
|
||||
input,
|
||||
output,
|
||||
terminal: false,
|
||||
});
|
||||
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
server.resetContext();
|
||||
}
|
||||
bench.end(n);
|
||||
server.close();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const runBenchmark = require('../common/benchmark');
|
||||
|
||||
common.skipIfInspectorDisabled();
|
||||
|
||||
runBenchmark('repl', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
|
||||
Reference in new issue
Block a user