From 43f699d4d2799cfc17cbcad5770e1889075d5dbe Mon Sep 17 00:00:00 2001 From: Early Riser <80089617+EarlyRiser42@users.noreply.github.com> Date: Sun, 25 Aug 2024 09:41:13 -0400 Subject: [PATCH] benchmark: fix benchmark for file path and URL conversion PR-URL: https://github.com/nodejs/node/pull/54190 Reviewed-By: Daeyeon Jeong Reviewed-By: Antoine du Hamel --- benchmark/url/whatwg-url-to-and-from-path.js | 56 +++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/benchmark/url/whatwg-url-to-and-from-path.js b/benchmark/url/whatwg-url-to-and-from-path.js index 3b87c0670a8fee..366a8c98991f60 100644 --- a/benchmark/url/whatwg-url-to-and-from-path.js +++ b/benchmark/url/whatwg-url-to-and-from-path.js @@ -3,28 +3,46 @@ const common = require('../common.js'); const { fileURLToPath, pathToFileURL } = require('node:url'); const isWindows = process.platform === 'win32'; -const bench = common.createBenchmark(main, { - input: isWindows ? [ - 'file:///c/', - ] : [ - 'file:///dev/null', - 'file:///dev/null?key=param&bool', - 'file:///dev/null?key=param&bool#hash', - ], - method: isWindows ? [ - 'fileURLToPath', - ] : [ - 'fileURLToPath', - 'pathToFileURL', - ], - n: [5e6], -}); +const inputs = isWindows ? [ + 'C:\\foo', + 'C:\\Program Files\\Music\\Web Sys\\main.html?REQUEST=RADIO', + '\\\\nas\\My Docs\\File.doc', + '\\\\?\\UNC\\server\\share\\folder\\file.txt', + 'file:///C:/foo', + 'file:///C:/dir/foo?query=1', + 'file:///C:/dir/foo#fragment', +] : [ + '/dev/null', + '/dev/null?key=param&bool', + '/dev/null?key=param&bool#hash', + 'file:///dev/null', + 'file:///dev/null?key=param&bool', + 'file:///dev/null?key=param&bool#hash', +]; -function main({ n, input, method }) { - method = method === 'fileURLOrPath' ? fileURLToPath : pathToFileURL; +const bench = common.createBenchmark( + main, + { + method: ['pathToFileURL', 'fileURLToPath'], + input: Object.values(inputs), + n: [5e6], + }, + { + combinationFilter: (p) => ( + (isWindows ? + (!p.input.startsWith('file://') && p.method === 'pathToFileURL') : + p.method === 'pathToFileURL' + ) || + (p.input.startsWith('file://') && p.method === 'fileURLToPath') + ), + }, +); + +function main({ method, input, n }) { + const methodFunc = method === 'fileURLToPath' ? fileURLToPath : pathToFileURL; bench.start(); for (let i = 0; i < n; i++) { - method(input); + methodFunc(input); } bench.end(n); }