From b9c7c9002f8cde2ddb17192bc2e5639f9649c28a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 17 Aug 2019 23:01:10 +0200 Subject: [PATCH] benchmark: improve process.env benchmarks Benchmark different types of operations and make results comparable by normalizing process.env for enumeartion. PR-URL: https://github.com/nodejs/node/pull/29188 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- benchmark/process/bench-env.js | 54 +++++++++++++++++++++--- test/benchmark/test-benchmark-process.js | 3 +- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/benchmark/process/bench-env.js b/benchmark/process/bench-env.js index a332d3cbd61895..5df521cc958389 100644 --- a/benchmark/process/bench-env.js +++ b/benchmark/process/bench-env.js @@ -3,15 +3,55 @@ const common = require('../common'); const bench = common.createBenchmark(main, { - n: [1e5], + n: [1e6], + operation: ['get', 'set', 'enumerate', 'query', 'delete'] }); -function main({ n }) { - bench.start(); - for (var i = 0; i < n; i++) { - // Access every item in object to process values. - Object.keys(process.env); +function main({ n, operation }) { + switch (operation) { + case 'get': + bench.start(); + for (let i = 0; i < n; i++) { + process.env.PATH; + } + bench.end(n); + break; + case 'set': + bench.start(); + for (let i = 0; i < n; i++) { + process.env.DUMMY = 'hello, world'; + } + bench.end(n); + break; + case 'enumerate': + // First, normalize process.env so that benchmark results are comparable. + for (const key of Object.keys(process.env)) + delete process.env[key]; + for (let i = 0; i < 64; i++) + process.env[Math.random()] = Math.random(); + + n /= 10; // Enumeration is comparatively heavy. + bench.start(); + for (let i = 0; i < n; i++) { + // Access every item in object to process values. + Object.keys(process.env); + } + bench.end(n); + break; + case 'query': + bench.start(); + for (let i = 0; i < n; i++) { + 'PATH' in process.env; + } + bench.end(n); + break; + case 'delete': + bench.start(); + for (let i = 0; i < n; i++) { + delete process.env.DUMMY; + } + bench.end(n); + break; } - bench.end(n); } diff --git a/test/benchmark/test-benchmark-process.js b/test/benchmark/test-benchmark-process.js index 15cb678017e7bb..a73fc075bfcfa6 100644 --- a/test/benchmark/test-benchmark-process.js +++ b/test/benchmark/test-benchmark-process.js @@ -7,5 +7,6 @@ const runBenchmark = require('../common/benchmark'); runBenchmark('process', [ 'n=1', - 'type=raw' + 'type=raw', + 'operation=enumerate', ], { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });