Skip to content

Commit 2ca7aeb

Browse files
cjihrigjasnell
authored andcommitted
benchmark: add common.binding()
Recently, process.binding() was replaced with internalBinding(). However, internalBinding() is not available on older builds of Node, which are often used for benchmarking purposes. This commit adds a common.binding() to the benchmarks to work around the issue. Hopefully, this can be removed in the not too distant future. PR-URL: #23460 Fixes: #23436 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent d911bab commit 2ca7aeb

File tree

8 files changed

+33
-21
lines changed

8 files changed

+33
-21
lines changed

benchmark/common.js

+10
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,13 @@ Benchmark.prototype.report = function(rate, elapsed) {
242242
type: 'report'
243243
});
244244
};
245+
246+
exports.binding = function(bindingName) {
247+
try {
248+
const { internalBinding } = require('internal/test/binding');
249+
250+
return internalBinding(bindingName);
251+
} catch {
252+
return process.binding(bindingName);
253+
}
254+
};

benchmark/http/bench-parser.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ const bench = common.createBenchmark(main, {
1010
});
1111

1212
function main({ len, n }) {
13-
const { internalBinding } = require('internal/test/binding');
14-
const { HTTPParser } = internalBinding('http_parser');
13+
const { HTTPParser } = common.binding('http_parser');
1514
const REQUEST = HTTPParser.REQUEST;
1615
const kOnHeaders = HTTPParser.kOnHeaders | 0;
1716
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;

benchmark/misc/punycode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common.js');
44
let icu;
55
try {
6-
icu = process.binding('icu');
6+
icu = common.binding('icu');
77
} catch (err) {}
88
const punycode = require('punycode');
99

benchmark/misc/trace.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const bench = common.createBenchmark(main, {
1111

1212
const {
1313
TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN: kBeforeEvent
14-
} = process.binding('constants').trace;
14+
} = common.binding('constants').trace;
1515

1616
function doTrace(n, trace) {
1717
bench.start();
@@ -31,12 +31,10 @@ function doIsTraceCategoryEnabled(n, isTraceCategoryEnabled) {
3131
}
3232

3333
function main({ n, method }) {
34-
const { internalBinding } = require('internal/test/binding');
35-
3634
const {
3735
trace,
3836
isTraceCategoryEnabled
39-
} = internalBinding('trace_events');
37+
} = common.binding('trace_events');
4038

4139
switch (method) {
4240
case '':

benchmark/net/tcp-raw-c2s.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ const bench = common.createBenchmark(main, {
1515
}, { flags: [ '--expose-internals', '--no-warnings' ] });
1616

1717
function main({ dur, len, type }) {
18-
const { internalBinding } = require('internal/test/binding');
19-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
20-
const { TCPConnectWrap } = process.binding('tcp_wrap');
21-
const { WriteWrap } = internalBinding('stream_wrap');
18+
const {
19+
TCP,
20+
TCPConnectWrap,
21+
constants: TCPConstants
22+
} = common.binding('tcp_wrap');
23+
const { WriteWrap } = common.binding('stream_wrap');
2224
const PORT = common.PORT;
2325

2426
const serverHandle = new TCP(TCPConstants.SERVER);

benchmark/net/tcp-raw-pipe.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ const bench = common.createBenchmark(main, {
1717
});
1818

1919
function main({ dur, len, type }) {
20-
const { internalBinding } = require('internal/test/binding');
21-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
22-
const { TCPConnectWrap } = process.binding('tcp_wrap');
23-
const { WriteWrap } = internalBinding('stream_wrap');
20+
const {
21+
TCP,
22+
TCPConnectWrap,
23+
constants: TCPConstants
24+
} = common.binding('tcp_wrap');
25+
const { WriteWrap } = common.binding('stream_wrap');
2426
const PORT = common.PORT;
2527

2628
function fail(err, syscall) {

benchmark/net/tcp-raw-s2c.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ const bench = common.createBenchmark(main, {
1717
});
1818

1919
function main({ dur, len, type }) {
20-
const { internalBinding } = require('internal/test/binding');
21-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
22-
const { TCPConnectWrap } = process.binding('tcp_wrap');
23-
const { WriteWrap } = internalBinding('stream_wrap');
20+
const {
21+
TCP,
22+
TCPConnectWrap,
23+
constants: TCPConstants
24+
} = common.binding('tcp_wrap');
25+
const { WriteWrap } = common.binding('stream_wrap');
2426
const PORT = common.PORT;
2527

2628
const serverHandle = new TCP(TCPConstants.SERVER);

benchmark/util/type-check.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ function main({ type, argument, version, n }) {
3838
// For testing, if supplied with an empty type, default to ArrayBufferView.
3939
type = type || 'ArrayBufferView';
4040

41-
const { internalBinding } = require('internal/test/binding');
42-
const util = internalBinding('util');
41+
const util = common.binding('util');
4342
const types = require('internal/util/types');
4443

4544
const func = { native: util, js: types }[version][`is${type}`];

0 commit comments

Comments
 (0)