From 8468dbd6b92997ab10cd298f98d1ca482b227b2a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 30 Dec 2018 00:26:36 +0100 Subject: [PATCH] benchmark: refactor path benchmarks So far the benchmarks created a highly specialized function which would inline exactly to the input. This changes it to provide a more realistic view to actual input by changing the input on each iteration. That prevents the function to be to specific. It also reduces the number of iterations the benchmarks are run to reduce the overall runtime. A microbenchmark should already show a significant difference with lower iterations, otherwise the significance for real world applications is only limited. --- benchmark/path/basename-posix.js | 4 ++-- benchmark/path/basename-win32.js | 4 ++-- benchmark/path/dirname-posix.js | 4 ++-- benchmark/path/dirname-win32.js | 4 ++-- benchmark/path/extname-posix.js | 4 ++-- benchmark/path/extname-win32.js | 4 ++-- benchmark/path/format-posix.js | 8 +++++--- benchmark/path/format-win32.js | 8 +++++--- benchmark/path/isAbsolute-posix.js | 4 ++-- benchmark/path/isAbsolute-win32.js | 4 ++-- benchmark/path/join-posix.js | 11 +++++++++-- benchmark/path/join-win32.js | 11 +++++++++-- benchmark/path/makeLong-win32.js | 4 ++-- benchmark/path/normalize-posix.js | 4 ++-- benchmark/path/normalize-win32.js | 4 ++-- benchmark/path/parse-posix.js | 9 +++------ benchmark/path/parse-win32.js | 9 +++------ benchmark/path/relative-posix.js | 12 ++++++------ benchmark/path/relative-win32.js | 14 ++++++-------- benchmark/path/resolve-posix.js | 11 +++++++++-- benchmark/path/resolve-win32.js | 11 +++++++++-- 21 files changed, 86 insertions(+), 62 deletions(-) diff --git a/benchmark/path/basename-posix.js b/benchmark/path/basename-posix.js index 024687cef0d2..45cad1e25660 100644 --- a/benchmark/path/basename-posix.js +++ b/benchmark/path/basename-posix.js @@ -15,7 +15,7 @@ const bench = common.createBenchmark(main, { '/foo/bar/baz/asdf/quux.html', ['/foo/bar/baz/asdf/quux.html', '.html'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, pathext }) { @@ -28,7 +28,7 @@ function main({ n, pathext }) { bench.start(); for (var i = 0; i < n; i++) { - posix.basename(pathext, ext); + posix.basename(i % 3 === 0 ? `${pathext}${i}` : pathext, ext); } bench.end(n); } diff --git a/benchmark/path/basename-win32.js b/benchmark/path/basename-win32.js index a68bf0c12a8e..30d65f3ac6a4 100644 --- a/benchmark/path/basename-win32.js +++ b/benchmark/path/basename-win32.js @@ -15,7 +15,7 @@ const bench = common.createBenchmark(main, { '\\foo\\bar\\baz\\asdf\\quux.html', ['\\foo\\bar\\baz\\asdf\\quux.html', '.html'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, pathext }) { @@ -28,7 +28,7 @@ function main({ n, pathext }) { bench.start(); for (var i = 0; i < n; i++) { - win32.basename(pathext, ext); + win32.basename(i % 3 === 0 ? `${pathext}${i}` : pathext, ext); } bench.end(n); } diff --git a/benchmark/path/dirname-posix.js b/benchmark/path/dirname-posix.js index b99bb99b3b0d..93f2f32c0126 100644 --- a/benchmark/path/dirname-posix.js +++ b/benchmark/path/dirname-posix.js @@ -12,13 +12,13 @@ const bench = common.createBenchmark(main, { 'foo/bar', '/foo/bar/baz/asdf/quux', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - posix.dirname(path); + posix.dirname(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/dirname-win32.js b/benchmark/path/dirname-win32.js index 551956efaf77..510595d721ac 100644 --- a/benchmark/path/dirname-win32.js +++ b/benchmark/path/dirname-win32.js @@ -12,13 +12,13 @@ const bench = common.createBenchmark(main, { 'foo\\bar', 'D:\\foo\\bar\\baz\\asdf\\quux', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - win32.dirname(path); + win32.dirname(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/extname-posix.js b/benchmark/path/extname-posix.js index 1c7d9e94495c..ee1ea07eebc0 100644 --- a/benchmark/path/extname-posix.js +++ b/benchmark/path/extname-posix.js @@ -15,13 +15,13 @@ const bench = common.createBenchmark(main, { '/foo/bar/baz/asdf/quux', '/foo/bar/baz/asdf/quux.foobarbazasdfquux', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - posix.extname(path); + posix.extname(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/extname-win32.js b/benchmark/path/extname-win32.js index 4378f8119af3..1de4bca28a29 100644 --- a/benchmark/path/extname-win32.js +++ b/benchmark/path/extname-win32.js @@ -15,13 +15,13 @@ const bench = common.createBenchmark(main, { 'D:\\foo\\bar\\baz\\asdf\\quux', '\\foo\\bar\\baz\\asdf\\quux.foobarbazasdfquux', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - win32.extname(path); + win32.extname(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/format-posix.js b/benchmark/path/format-posix.js index bae1ae1e2371..9c555f232c00 100644 --- a/benchmark/path/format-posix.js +++ b/benchmark/path/format-posix.js @@ -6,7 +6,7 @@ const bench = common.createBenchmark(main, { props: [ ['/', '/home/user/dir', 'index.html', '.html', 'index'].join('|'), ], - n: [1e7] + n: [1e6] }); function main({ n, props }) { @@ -14,13 +14,15 @@ function main({ n, props }) { const obj = { root: props[0] || '', dir: props[1] || '', - base: props[2] || '', + base: '', ext: props[3] || '', - name: props[4] || '', + name: '', }; bench.start(); for (var i = 0; i < n; i++) { + obj.base = `a${i}${props[2] || ''}`; + obj.name = `a${i}${props[4] || ''}`; posix.format(obj); } bench.end(n); diff --git a/benchmark/path/format-win32.js b/benchmark/path/format-win32.js index efb3fcd2c807..65315c4dd638 100644 --- a/benchmark/path/format-win32.js +++ b/benchmark/path/format-win32.js @@ -6,7 +6,7 @@ const bench = common.createBenchmark(main, { props: [ ['C:\\', 'C:\\path\\dir', 'index.html', '.html', 'index'].join('|'), ], - n: [1e7] + n: [1e6] }); function main({ n, props }) { @@ -14,13 +14,15 @@ function main({ n, props }) { const obj = { root: props[0] || '', dir: props[1] || '', - base: props[2] || '', + base: '', ext: props[3] || '', - name: props[4] || '', + name: '', }; bench.start(); for (var i = 0; i < n; i++) { + obj.base = `a${i}${props[2] || ''}`; + obj.name = `a${i}${props[4] || ''}`; win32.format(obj); } bench.end(n); diff --git a/benchmark/path/isAbsolute-posix.js b/benchmark/path/isAbsolute-posix.js index 96da0e01c640..dd0dfd1964e0 100644 --- a/benchmark/path/isAbsolute-posix.js +++ b/benchmark/path/isAbsolute-posix.js @@ -10,13 +10,13 @@ const bench = common.createBenchmark(main, { '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/baz/..', 'bar/baz', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - posix.isAbsolute(path); + posix.isAbsolute(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/isAbsolute-win32.js b/benchmark/path/isAbsolute-win32.js index edb84e80776f..ff03f2628a32 100644 --- a/benchmark/path/isAbsolute-win32.js +++ b/benchmark/path/isAbsolute-win32.js @@ -11,13 +11,13 @@ const bench = common.createBenchmark(main, { 'C:baz\\..', 'bar\\baz', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - win32.isAbsolute(path); + win32.isAbsolute(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/join-posix.js b/benchmark/path/join-posix.js index 2ba1a4299fcf..e573166d7ae0 100644 --- a/benchmark/path/join-posix.js +++ b/benchmark/path/join-posix.js @@ -6,15 +6,22 @@ const bench = common.createBenchmark(main, { paths: [ ['/foo', 'bar', '', 'baz/asdf', 'quux', '..'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, paths }) { const args = paths.split('|'); + const copy = [...args]; + const orig = copy[1]; bench.start(); for (var i = 0; i < n; i++) { - posix.join.apply(null, args); + if (i % 3 === 0) { + copy[1] = `${orig}${i}`; + posix.join(...copy); + } else { + posix.join(...args); + } } bench.end(n); } diff --git a/benchmark/path/join-win32.js b/benchmark/path/join-win32.js index 42449542aa31..cd69836c006e 100644 --- a/benchmark/path/join-win32.js +++ b/benchmark/path/join-win32.js @@ -6,15 +6,22 @@ const bench = common.createBenchmark(main, { paths: [ ['C:\\foo', 'bar', '', 'baz\\asdf', 'quux', '..'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, paths }) { const args = paths.split('|'); + const copy = [...args]; + const orig = copy[1]; bench.start(); for (var i = 0; i < n; i++) { - win32.join.apply(null, args); + if (i % 3 === 0) { + copy[1] = `${orig}${i}`; + win32.join(...copy); + } else { + win32.join(...args); + } } bench.end(n); } diff --git a/benchmark/path/makeLong-win32.js b/benchmark/path/makeLong-win32.js index f300f47cf53e..45d0d8de60d7 100644 --- a/benchmark/path/makeLong-win32.js +++ b/benchmark/path/makeLong-win32.js @@ -9,13 +9,13 @@ const bench = common.createBenchmark(main, { '\\\\foo\\bar', '\\\\?\\foo', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - win32._makeLong(path); + win32._makeLong(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/normalize-posix.js b/benchmark/path/normalize-posix.js index 7b5c2b1cf900..4383cff4a588 100644 --- a/benchmark/path/normalize-posix.js +++ b/benchmark/path/normalize-posix.js @@ -11,13 +11,13 @@ const bench = common.createBenchmark(main, { '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/foo/bar', '/foo/bar//baz/asdf/quux/..', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - posix.normalize(path); + posix.normalize(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/normalize-win32.js b/benchmark/path/normalize-win32.js index 749523daa818..319c391d17a7 100644 --- a/benchmark/path/normalize-win32.js +++ b/benchmark/path/normalize-win32.js @@ -11,13 +11,13 @@ const bench = common.createBenchmark(main, { 'C:\\foo\\bar', 'C:\\foo\\bar\\\\baz\\asdf\\quux\\..', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { bench.start(); for (var i = 0; i < n; i++) { - win32.normalize(path); + win32.normalize(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/parse-posix.js b/benchmark/path/parse-posix.js index 00fdc6a3ad7c..7fb1d55099f7 100644 --- a/benchmark/path/parse-posix.js +++ b/benchmark/path/parse-posix.js @@ -12,16 +12,13 @@ const bench = common.createBenchmark(main, { 'foo/bar', '/foo/bar/baz/asdf/.quux', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { - for (var i = 0; i < n; i++) { - posix.parse(path); - } bench.start(); - for (i = 0; i < n; i++) { - posix.parse(path); + for (let i = 0; i < n; i++) { + posix.parse(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/parse-win32.js b/benchmark/path/parse-win32.js index 5bcc150d1b70..ea4bc34a8492 100644 --- a/benchmark/path/parse-win32.js +++ b/benchmark/path/parse-win32.js @@ -13,16 +13,13 @@ const bench = common.createBenchmark(main, { 'foo\\bar', '\\foo\\bar\\baz\\asdf\\.quux', ], - n: [1e6] + n: [1e5] }); function main({ n, path }) { - for (var i = 0; i < n; i++) { - win32.parse(path); - } bench.start(); - for (i = 0; i < n; i++) { - win32.parse(path); + for (let i = 0; i < n; i++) { + win32.parse(i % 3 === 0 ? `${path}${i}` : path); } bench.end(n); } diff --git a/benchmark/path/relative-posix.js b/benchmark/path/relative-posix.js index caf199613555..2c4dd31d2778 100644 --- a/benchmark/path/relative-posix.js +++ b/benchmark/path/relative-posix.js @@ -12,7 +12,7 @@ const bench = common.createBenchmark(main, { ['/foo/bar/baz/quux', '/foo/bar/baz/quux'].join('|'), ['/foo/bar/baz/quux', '/var/log'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, paths }) { @@ -22,13 +22,13 @@ function main({ n, paths }) { to = paths.slice(delimIdx + 1); paths = paths.slice(0, delimIdx); } - for (var i = 0; i < n; i++) { - posix.relative(paths, to); - } bench.start(); - for (i = 0; i < n; i++) { - posix.relative(paths, to); + for (let i = 0; i < n; i++) { + if (i % 3 === 0) + posix.relative(`${paths}${i}`, `${to}${i}`); + else + posix.relative(paths, to); } bench.end(n); } diff --git a/benchmark/path/relative-win32.js b/benchmark/path/relative-win32.js index 81fd10b46c22..5f34fdf8fd58 100644 --- a/benchmark/path/relative-win32.js +++ b/benchmark/path/relative-win32.js @@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, { ['C:\\foo\\BAR\\BAZ', 'C:\\foo\\bar\\baz'].join('|'), ['C:\\foo\\bar\\baz\\quux', 'C:\\'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, paths }) { @@ -21,14 +21,12 @@ function main({ n, paths }) { paths = paths.slice(0, delimIdx); } - // Warmup - for (var i = 0; i < n; i++) { - win32.relative(paths, to); - } - bench.start(); - for (i = 0; i < n; i++) { - win32.relative(paths, to); + for (let i = 0; i < n; i++) { + if (i % 3 === 0) + win32.relative(`${paths}${i}`, `${to}${i}`); + else + win32.relative(paths, to); } bench.end(n); } diff --git a/benchmark/path/resolve-posix.js b/benchmark/path/resolve-posix.js index 14b7fd109625..3cdf1cd49a07 100644 --- a/benchmark/path/resolve-posix.js +++ b/benchmark/path/resolve-posix.js @@ -9,15 +9,22 @@ const bench = common.createBenchmark(main, { ['foo/bar', '/tmp/file/', '..', 'a/../subfile'].join('|'), ['a/b/c/', '../../..'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, paths }) { const args = paths.split('|'); + const copy = [...args]; + const orig = copy[0]; bench.start(); for (var i = 0; i < n; i++) { - posix.resolve.apply(null, args); + if (i % 3 === 0) { + copy[0] = `${orig}${i}`; + posix.resolve(...copy); + } else { + posix.resolve(...args); + } } bench.end(n); } diff --git a/benchmark/path/resolve-win32.js b/benchmark/path/resolve-win32.js index 83e10042b4a6..cf8144ef2c57 100644 --- a/benchmark/path/resolve-win32.js +++ b/benchmark/path/resolve-win32.js @@ -9,15 +9,22 @@ const bench = common.createBenchmark(main, { ['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'].join('|'), ['c:/blah\\blah', 'd:/games', 'c:../a'].join('|'), ], - n: [1e6] + n: [1e5] }); function main({ n, paths }) { const args = paths.split('|'); + const copy = [...args]; + const orig = copy[0]; bench.start(); for (var i = 0; i < n; i++) { - win32.resolve.apply(null, args); + if (i % 3 === 0) { + copy[0] = `${orig}${i}`; + win32.resolve(...copy); + } else { + win32.resolve(...args); + } } bench.end(n); }