Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions benchmark/repl/completion.js
Original file line number Diff line number Diff line change
@@ -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);
}
39 changes: 39 additions & 0 deletions benchmark/repl/creation.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
57 changes: 57 additions & 0 deletions benchmark/repl/evaluate.js
Original file line number Diff line number Diff line change
@@ -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);
}
52 changes: 52 additions & 0 deletions benchmark/repl/process-lines.js
Original file line number Diff line number Diff line change
@@ -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);
}
26 changes: 26 additions & 0 deletions benchmark/repl/reset-context.js
Original file line number Diff line number Diff line change
@@ -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();
}
7 changes: 7 additions & 0 deletions test/benchmark/test-benchmark-repl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('../common');

const runBenchmark = require('../common/benchmark');

runBenchmark('repl', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
Loading