From 45cdbcfee6a79f521f2c0afee780c8afa1c5fab4 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Thu, 24 Aug 2017 03:15:55 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/15004 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung Reviewed-By: Rich Trott Reviewed-By: Refael Ackermann --- test/common/README.md | 12 +++++ test/common/benchmark.js | 30 +++++++++++++ test/parallel/test-benchmark-arrays.js | 18 +------- test/parallel/test-benchmark-cluster.js | 19 +------- test/parallel/test-benchmark-crypto.js | 41 +++++++---------- test/parallel/test-benchmark-dns.js | 21 +-------- test/parallel/test-benchmark-domain.js | 18 +------- test/parallel/test-benchmark-events.js | 17 +------ test/parallel/test-benchmark-os.js | 17 +------ test/parallel/test-benchmark-path.js | 30 +++++-------- test/parallel/test-benchmark-process.js | 26 ++++------- test/parallel/test-benchmark-timers.js | 30 ++++--------- .../test-benchmark-child-process.js | 37 +++++----------- test/sequential/test-benchmark-http.js | 44 +++++++------------ test/sequential/test-benchmark-net.js | 28 ++++-------- 15 files changed, 131 insertions(+), 257 deletions(-) create mode 100644 test/common/benchmark.js diff --git a/test/common/README.md b/test/common/README.md index b8d9af2fcf70f5..c7d4e780f026ab 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -4,9 +4,21 @@ This directory contains modules used to test the Node.js implementation. ## Table of Contents +* [Benchmark module](#benchmark-module) * [Common module API](#common-module-api) * [WPT module](#wpt-module) +## Benchmark Module + +The `benchmark` module is used by tests to run benchmarks. + +### runBenchmark(name, args, env) + +* `name` [<String>] Name of benchmark suite to be run. +* `args` [<Array>] Array of environment variable key/value pairs (ex: + `n=1`) to be applied via `--set`. +* `env` [<Object>] Environment variables to be applied during the run. + ## Common Module API The `common` module is used by tests for consistency across repeated diff --git a/test/common/benchmark.js b/test/common/benchmark.js new file mode 100644 index 00000000000000..6496da1cfb9fe5 --- /dev/null +++ b/test/common/benchmark.js @@ -0,0 +1,30 @@ +/* eslint-disable required-modules */ + +'use strict'; + +const assert = require('assert'); +const fork = require('child_process').fork; +const path = require('path'); + +const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); + +function runBenchmark(name, args, env) { + const argv = []; + + for (let i = 0; i < args.length; i++) { + argv.push('--set'); + argv.push(args[i]); + } + + argv.push(name); + + const mergedEnv = Object.assign({}, process.env, env); + + const child = fork(runjs, argv, { env: mergedEnv }); + child.on('exit', (code, signal) => { + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + }); +} + +module.exports = runBenchmark; diff --git a/test/parallel/test-benchmark-arrays.js b/test/parallel/test-benchmark-arrays.js index 2ffdc52c03a592..6e11d9743e0dac 100644 --- a/test/parallel/test-benchmark-arrays.js +++ b/test/parallel/test-benchmark-arrays.js @@ -2,20 +2,6 @@ require('../common'); -// Minimal test for arrays benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. +const runBenchmark = require('../common/benchmark'); -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'n=1', - '--set', 'type=Array', - 'arrays']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('arrays', ['n=1', 'type=Array']); diff --git a/test/parallel/test-benchmark-cluster.js b/test/parallel/test-benchmark-cluster.js index 51a1f31ef3013b..d6e3b27ee89f81 100644 --- a/test/parallel/test-benchmark-cluster.js +++ b/test/parallel/test-benchmark-cluster.js @@ -2,21 +2,6 @@ require('../common'); -// Minimal test for cluster benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. +const runBenchmark = require('../common/benchmark'); -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'n=1', - '--set', 'payload=string', - '--set', 'sendsPerBroadcast=1', - 'cluster']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('cluster', ['n=1', 'payload=string', 'sendsPerBroadcast=1']); diff --git a/test/parallel/test-benchmark-crypto.js b/test/parallel/test-benchmark-crypto.js index 3675c38b9ea280..2e78d78bc93985 100644 --- a/test/parallel/test-benchmark-crypto.js +++ b/test/parallel/test-benchmark-crypto.js @@ -8,29 +8,18 @@ if (!common.hasCrypto) if (common.hasFipsCrypto) common.skip('some benchmarks are FIPS-incompatible'); -// Minimal test for crypto benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'algo=sha256', - '--set', 'api=stream', - '--set', 'keylen=1024', - '--set', 'len=1', - '--set', 'n=1', - '--set', 'out=buffer', - '--set', 'type=buf', - '--set', 'v=crypto', - '--set', 'writes=1', - 'crypto']; - -const child = fork(runjs, argv, { env: Object.assign({}, process.env, { - NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }) }); - -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +const runBenchmark = require('../common/benchmark'); + +runBenchmark('crypto', + [ + 'n=1', + 'algo=sha256', + 'api=stream', + 'keylen=1024', + 'len=1', + 'out=buffer', + 'type=buf', + 'v=crypto', + 'writes=1' + ], + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); diff --git a/test/parallel/test-benchmark-dns.js b/test/parallel/test-benchmark-dns.js index ba15ad8c0d2fa9..27c3271c74d86d 100644 --- a/test/parallel/test-benchmark-dns.js +++ b/test/parallel/test-benchmark-dns.js @@ -2,26 +2,9 @@ require('../common'); -// Minimal test for dns benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); +const runBenchmark = require('../common/benchmark'); const env = Object.assign({}, process.env, { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); -const child = fork(runjs, - ['--set', 'n=1', - '--set', 'all=false', - '--set', 'name=127.0.0.1', - 'dns'], - { env }); - -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('dns', ['n=1', 'all=false', 'name=127.0.0.1'], env); diff --git a/test/parallel/test-benchmark-domain.js b/test/parallel/test-benchmark-domain.js index cacd45f6dad8c7..b1b56d2b7f5fec 100644 --- a/test/parallel/test-benchmark-domain.js +++ b/test/parallel/test-benchmark-domain.js @@ -2,20 +2,6 @@ require('../common'); -// Minimal test for domain benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. +const runBenchmark = require('../common/benchmark'); -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'arguments=0', - '--set', 'n=1', - 'domain']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('domain', ['n=1', 'arguments=0']); diff --git a/test/parallel/test-benchmark-events.js b/test/parallel/test-benchmark-events.js index 6b7d25c61f007f..a82444c78d766d 100644 --- a/test/parallel/test-benchmark-events.js +++ b/test/parallel/test-benchmark-events.js @@ -2,19 +2,6 @@ require('../common'); -// Minimal test for events benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. +const runBenchmark = require('../common/benchmark'); -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'n=1', - 'events']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('events', ['n=1']); diff --git a/test/parallel/test-benchmark-os.js b/test/parallel/test-benchmark-os.js index 4ad179063afce8..836e0e650483f1 100644 --- a/test/parallel/test-benchmark-os.js +++ b/test/parallel/test-benchmark-os.js @@ -2,19 +2,6 @@ require('../common'); -// Minimal test for os benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. +const runBenchmark = require('../common/benchmark'); -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'n=1', - 'os']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('os', ['n=1']); diff --git a/test/parallel/test-benchmark-path.js b/test/parallel/test-benchmark-path.js index 922a59f03cde8d..9b73b92100880d 100644 --- a/test/parallel/test-benchmark-path.js +++ b/test/parallel/test-benchmark-path.js @@ -2,23 +2,13 @@ require('../common'); -// Minimal test for path benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'n=1', - '--set', 'path=', - '--set', 'pathext=', - '--set', 'paths=', - '--set', 'props=', - 'path']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +const runBenchmark = require('../common/benchmark'); + +runBenchmark('path', + [ + 'n=1', + 'path=', + 'pathext=', + 'paths=', + 'props=' + ]); diff --git a/test/parallel/test-benchmark-process.js b/test/parallel/test-benchmark-process.js index 6abd584379d146..08b28269159a16 100644 --- a/test/parallel/test-benchmark-process.js +++ b/test/parallel/test-benchmark-process.js @@ -2,21 +2,11 @@ require('../common'); -// Minimal test for process benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'millions=0.000001', - '--set', 'n=1', - '--set', 'type=raw', - 'process']; - -const child = fork(runjs, argv); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +const runBenchmark = require('../common/benchmark'); + +runBenchmark('process', + [ + 'millions=0.000001', + 'n=1', + 'type=raw' + ]); diff --git a/test/parallel/test-benchmark-timers.js b/test/parallel/test-benchmark-timers.js index 991ffda7186e72..cca9ede3a01c41 100644 --- a/test/parallel/test-benchmark-timers.js +++ b/test/parallel/test-benchmark-timers.js @@ -2,24 +2,12 @@ require('../common'); -// Minimal test for timers benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); -const argv = ['--set', 'type=depth', - '--set', 'millions=0.000001', - '--set', 'thousands=0.001', - 'timers']; - -const env = Object.assign({}, process.env, - { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); - -const child = fork(runjs, argv, { env }); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +const runBenchmark = require('../common/benchmark'); + +runBenchmark('timers', + [ + 'type=depth', + 'millions=0.000001', + 'thousands=0.001' + ], + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); diff --git a/test/sequential/test-benchmark-child-process.js b/test/sequential/test-benchmark-child-process.js index f993238549fca4..365777069bc4cf 100644 --- a/test/sequential/test-benchmark-child-process.js +++ b/test/sequential/test-benchmark-child-process.js @@ -2,29 +2,14 @@ require('../common'); -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); - -const env = Object.assign({}, process.env, - { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); - -const child = fork( - runjs, - [ - '--set', 'dur=0', - '--set', 'n=1', - '--set', 'len=1', - '--set', 'params=1', - '--set', 'methodName=execSync', - 'child_process' - ], - { env } -); - -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +const runBenchmark = require('../common/benchmark'); + +runBenchmark('child_process', + [ + 'dur=0', + 'n=1', + 'len=1', + 'params=1', + 'methodName=execSync', + ], + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); diff --git a/test/sequential/test-benchmark-http.js b/test/sequential/test-benchmark-http.js index b8d47fb8980db1..2989198b77c1d1 100644 --- a/test/sequential/test-benchmark-http.js +++ b/test/sequential/test-benchmark-http.js @@ -5,35 +5,23 @@ const common = require('../common'); if (!common.enoughTestMem) common.skip('Insufficient memory for HTTP benchmark test'); -// Minimal test for http benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - // Because the http benchmarks use hardcoded ports, this should be in sequential // rather than parallel to make sure it does not conflict with tests that choose // random available ports. -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); - -const env = Object.assign({}, process.env, - { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); - -const child = fork(runjs, ['--set', 'benchmarker=test-double', - '--set', 'c=1', - '--set', 'chunkedEnc=true', - '--set', 'chunks=0', - '--set', 'dur=0.1', - '--set', 'key=""', - '--set', 'len=1', - '--set', 'method=write', - '--set', 'n=1', - '--set', 'res=normal', - 'http'], - { env }); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +const runBenchmark = require('../common/benchmark'); + +runBenchmark('http', + [ + 'benchmarker=test-double', + 'c=1', + 'chunkedEnc=true', + 'chunks=0', + 'dur=0.1', + 'key=""', + 'len=1', + 'method=write', + 'n=1', + 'res=normal' + ], + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); diff --git a/test/sequential/test-benchmark-net.js b/test/sequential/test-benchmark-net.js index b2d360328504fb..5d3d283fc7903e 100644 --- a/test/sequential/test-benchmark-net.js +++ b/test/sequential/test-benchmark-net.js @@ -2,28 +2,16 @@ require('../common'); -// Minimal test for net benchmarks. This makes sure the benchmarks aren't -// horribly broken but nothing more than that. - // Because the net benchmarks use hardcoded ports, this should be in sequential // rather than parallel to make sure it does not conflict with tests that choose // random available ports. -const assert = require('assert'); -const fork = require('child_process').fork; -const path = require('path'); - -const runjs = path.join(__dirname, '..', '..', 'benchmark', 'run.js'); +const runBenchmark = require('../common/benchmark'); -const env = Object.assign({}, process.env, - { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); -const child = fork(runjs, - ['--set', 'dur=0', - '--set', 'len=1024', - '--set', 'type=buf', - 'net'], - { env }); -child.on('exit', (code, signal) => { - assert.strictEqual(code, 0); - assert.strictEqual(signal, null); -}); +runBenchmark('net', + [ + 'dur=0', + 'len=1024', + 'type=buf' + ], + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });