Skip to content

Commit 45cdbcf

Browse files
maclover7jasnell
authored andcommittedSep 20, 2017
test: create shared runBenchmark function
Mostly shared/duplicated logic between all benchmark test files, so creating a new common module to store it. PR-URL: #15004 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 91e96d8 commit 45cdbcf

15 files changed

+131
-257
lines changed
 

‎test/common/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ This directory contains modules used to test the Node.js implementation.
44

55
## Table of Contents
66

7+
* [Benchmark module](#benchmark-module)
78
* [Common module API](#common-module-api)
89
* [WPT module](#wpt-module)
910

11+
## Benchmark Module
12+
13+
The `benchmark` module is used by tests to run benchmarks.
14+
15+
### runBenchmark(name, args, env)
16+
17+
* `name` [&lt;String>] Name of benchmark suite to be run.
18+
* `args` [&lt;Array>] Array of environment variable key/value pairs (ex:
19+
`n=1`) to be applied via `--set`.
20+
* `env` [&lt;Object>] Environment variables to be applied during the run.
21+
1022
## Common Module API
1123

1224
The `common` module is used by tests for consistency across repeated

‎test/common/benchmark.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable required-modules */
2+
3+
'use strict';
4+
5+
const assert = require('assert');
6+
const fork = require('child_process').fork;
7+
const path = require('path');
8+
9+
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
10+
11+
function runBenchmark(name, args, env) {
12+
const argv = [];
13+
14+
for (let i = 0; i < args.length; i++) {
15+
argv.push('--set');
16+
argv.push(args[i]);
17+
}
18+
19+
argv.push(name);
20+
21+
const mergedEnv = Object.assign({}, process.env, env);
22+
23+
const child = fork(runjs, argv, { env: mergedEnv });
24+
child.on('exit', (code, signal) => {
25+
assert.strictEqual(code, 0);
26+
assert.strictEqual(signal, null);
27+
});
28+
}
29+
30+
module.exports = runBenchmark;

‎test/parallel/test-benchmark-arrays.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
require('../common');
44

5-
// Minimal test for arrays benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
5+
const runBenchmark = require('../common/benchmark');
76

8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'n=1',
14-
'--set', 'type=Array',
15-
'arrays'];
16-
17-
const child = fork(runjs, argv);
18-
child.on('exit', (code, signal) => {
19-
assert.strictEqual(code, 0);
20-
assert.strictEqual(signal, null);
21-
});
7+
runBenchmark('arrays', ['n=1', 'type=Array']);

‎test/parallel/test-benchmark-cluster.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22

33
require('../common');
44

5-
// Minimal test for cluster benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
5+
const runBenchmark = require('../common/benchmark');
76

8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'n=1',
14-
'--set', 'payload=string',
15-
'--set', 'sendsPerBroadcast=1',
16-
'cluster'];
17-
18-
const child = fork(runjs, argv);
19-
child.on('exit', (code, signal) => {
20-
assert.strictEqual(code, 0);
21-
assert.strictEqual(signal, null);
22-
});
7+
runBenchmark('cluster', ['n=1', 'payload=string', 'sendsPerBroadcast=1']);

‎test/parallel/test-benchmark-crypto.js

+15-26
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,18 @@ if (!common.hasCrypto)
88
if (common.hasFipsCrypto)
99
common.skip('some benchmarks are FIPS-incompatible');
1010

11-
// Minimal test for crypto benchmarks. This makes sure the benchmarks aren't
12-
// horribly broken but nothing more than that.
13-
14-
const assert = require('assert');
15-
const fork = require('child_process').fork;
16-
const path = require('path');
17-
18-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
19-
const argv = ['--set', 'algo=sha256',
20-
'--set', 'api=stream',
21-
'--set', 'keylen=1024',
22-
'--set', 'len=1',
23-
'--set', 'n=1',
24-
'--set', 'out=buffer',
25-
'--set', 'type=buf',
26-
'--set', 'v=crypto',
27-
'--set', 'writes=1',
28-
'crypto'];
29-
30-
const child = fork(runjs, argv, { env: Object.assign({}, process.env, {
31-
NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }) });
32-
33-
child.on('exit', (code, signal) => {
34-
assert.strictEqual(code, 0);
35-
assert.strictEqual(signal, null);
36-
});
11+
const runBenchmark = require('../common/benchmark');
12+
13+
runBenchmark('crypto',
14+
[
15+
'n=1',
16+
'algo=sha256',
17+
'api=stream',
18+
'keylen=1024',
19+
'len=1',
20+
'out=buffer',
21+
'type=buf',
22+
'v=crypto',
23+
'writes=1'
24+
],
25+
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

‎test/parallel/test-benchmark-dns.js

+2-19
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,9 @@
22

33
require('../common');
44

5-
// Minimal test for dns benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
7-
8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
5+
const runBenchmark = require('../common/benchmark');
136

147
const env = Object.assign({}, process.env,
158
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
169

17-
const child = fork(runjs,
18-
['--set', 'n=1',
19-
'--set', 'all=false',
20-
'--set', 'name=127.0.0.1',
21-
'dns'],
22-
{ env });
23-
24-
child.on('exit', (code, signal) => {
25-
assert.strictEqual(code, 0);
26-
assert.strictEqual(signal, null);
27-
});
10+
runBenchmark('dns', ['n=1', 'all=false', 'name=127.0.0.1'], env);

‎test/parallel/test-benchmark-domain.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
require('../common');
44

5-
// Minimal test for domain benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
5+
const runBenchmark = require('../common/benchmark');
76

8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'arguments=0',
14-
'--set', 'n=1',
15-
'domain'];
16-
17-
const child = fork(runjs, argv);
18-
child.on('exit', (code, signal) => {
19-
assert.strictEqual(code, 0);
20-
assert.strictEqual(signal, null);
21-
});
7+
runBenchmark('domain', ['n=1', 'arguments=0']);

‎test/parallel/test-benchmark-events.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22

33
require('../common');
44

5-
// Minimal test for events benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
5+
const runBenchmark = require('../common/benchmark');
76

8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'n=1',
14-
'events'];
15-
16-
const child = fork(runjs, argv);
17-
child.on('exit', (code, signal) => {
18-
assert.strictEqual(code, 0);
19-
assert.strictEqual(signal, null);
20-
});
7+
runBenchmark('events', ['n=1']);

‎test/parallel/test-benchmark-os.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22

33
require('../common');
44

5-
// Minimal test for os benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
5+
const runBenchmark = require('../common/benchmark');
76

8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'n=1',
14-
'os'];
15-
16-
const child = fork(runjs, argv);
17-
child.on('exit', (code, signal) => {
18-
assert.strictEqual(code, 0);
19-
assert.strictEqual(signal, null);
20-
});
7+
runBenchmark('os', ['n=1']);

‎test/parallel/test-benchmark-path.js

+10-20
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@
22

33
require('../common');
44

5-
// Minimal test for path benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
7-
8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'n=1',
14-
'--set', 'path=',
15-
'--set', 'pathext=',
16-
'--set', 'paths=',
17-
'--set', 'props=',
18-
'path'];
19-
20-
const child = fork(runjs, argv);
21-
child.on('exit', (code, signal) => {
22-
assert.strictEqual(code, 0);
23-
assert.strictEqual(signal, null);
24-
});
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('path',
8+
[
9+
'n=1',
10+
'path=',
11+
'pathext=',
12+
'paths=',
13+
'props='
14+
]);

‎test/parallel/test-benchmark-process.js

+8-18
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22

33
require('../common');
44

5-
// Minimal test for process benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
7-
8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'millions=0.000001',
14-
'--set', 'n=1',
15-
'--set', 'type=raw',
16-
'process'];
17-
18-
const child = fork(runjs, argv);
19-
child.on('exit', (code, signal) => {
20-
assert.strictEqual(code, 0);
21-
assert.strictEqual(signal, null);
22-
});
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('process',
8+
[
9+
'millions=0.000001',
10+
'n=1',
11+
'type=raw'
12+
]);

‎test/parallel/test-benchmark-timers.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,12 @@
22

33
require('../common');
44

5-
// Minimal test for timers benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
7-
8-
const assert = require('assert');
9-
const fork = require('child_process').fork;
10-
const path = require('path');
11-
12-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
13-
const argv = ['--set', 'type=depth',
14-
'--set', 'millions=0.000001',
15-
'--set', 'thousands=0.001',
16-
'timers'];
17-
18-
const env = Object.assign({}, process.env,
19-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
20-
21-
const child = fork(runjs, argv, { env });
22-
child.on('exit', (code, signal) => {
23-
assert.strictEqual(code, 0);
24-
assert.strictEqual(signal, null);
25-
});
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('timers',
8+
[
9+
'type=depth',
10+
'millions=0.000001',
11+
'thousands=0.001'
12+
],
13+
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

‎test/sequential/test-benchmark-child-process.js

+11-26
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,14 @@
22

33
require('../common');
44

5-
const assert = require('assert');
6-
const fork = require('child_process').fork;
7-
const path = require('path');
8-
9-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
10-
11-
const env = Object.assign({}, process.env,
12-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
13-
14-
const child = fork(
15-
runjs,
16-
[
17-
'--set', 'dur=0',
18-
'--set', 'n=1',
19-
'--set', 'len=1',
20-
'--set', 'params=1',
21-
'--set', 'methodName=execSync',
22-
'child_process'
23-
],
24-
{ env }
25-
);
26-
27-
child.on('exit', (code, signal) => {
28-
assert.strictEqual(code, 0);
29-
assert.strictEqual(signal, null);
30-
});
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('child_process',
8+
[
9+
'dur=0',
10+
'n=1',
11+
'len=1',
12+
'params=1',
13+
'methodName=execSync',
14+
],
15+
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

‎test/sequential/test-benchmark-http.js

+16-28
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,23 @@ const common = require('../common');
55
if (!common.enoughTestMem)
66
common.skip('Insufficient memory for HTTP benchmark test');
77

8-
// Minimal test for http benchmarks. This makes sure the benchmarks aren't
9-
// horribly broken but nothing more than that.
10-
118
// Because the http benchmarks use hardcoded ports, this should be in sequential
129
// rather than parallel to make sure it does not conflict with tests that choose
1310
// random available ports.
1411

15-
const assert = require('assert');
16-
const fork = require('child_process').fork;
17-
const path = require('path');
18-
19-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
20-
21-
const env = Object.assign({}, process.env,
22-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
23-
24-
const child = fork(runjs, ['--set', 'benchmarker=test-double',
25-
'--set', 'c=1',
26-
'--set', 'chunkedEnc=true',
27-
'--set', 'chunks=0',
28-
'--set', 'dur=0.1',
29-
'--set', 'key=""',
30-
'--set', 'len=1',
31-
'--set', 'method=write',
32-
'--set', 'n=1',
33-
'--set', 'res=normal',
34-
'http'],
35-
{ env });
36-
child.on('exit', (code, signal) => {
37-
assert.strictEqual(code, 0);
38-
assert.strictEqual(signal, null);
39-
});
12+
const runBenchmark = require('../common/benchmark');
13+
14+
runBenchmark('http',
15+
[
16+
'benchmarker=test-double',
17+
'c=1',
18+
'chunkedEnc=true',
19+
'chunks=0',
20+
'dur=0.1',
21+
'key=""',
22+
'len=1',
23+
'method=write',
24+
'n=1',
25+
'res=normal'
26+
],
27+
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

‎test/sequential/test-benchmark-net.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@
22

33
require('../common');
44

5-
// Minimal test for net benchmarks. This makes sure the benchmarks aren't
6-
// horribly broken but nothing more than that.
7-
85
// Because the net benchmarks use hardcoded ports, this should be in sequential
96
// rather than parallel to make sure it does not conflict with tests that choose
107
// random available ports.
118

12-
const assert = require('assert');
13-
const fork = require('child_process').fork;
14-
const path = require('path');
15-
16-
const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js');
9+
const runBenchmark = require('../common/benchmark');
1710

18-
const env = Object.assign({}, process.env,
19-
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
20-
const child = fork(runjs,
21-
['--set', 'dur=0',
22-
'--set', 'len=1024',
23-
'--set', 'type=buf',
24-
'net'],
25-
{ env });
26-
child.on('exit', (code, signal) => {
27-
assert.strictEqual(code, 0);
28-
assert.strictEqual(signal, null);
29-
});
11+
runBenchmark('net',
12+
[
13+
'dur=0',
14+
'len=1024',
15+
'type=buf'
16+
],
17+
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)
Please sign in to comment.