From be6ad3f375b39e3f679295be80900009b78807d6 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 18 Nov 2023 01:46:42 +0100 Subject: [PATCH] benchmark: rewrite import.meta benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a ESM benchmark, rewrite it so that we are directly benchmarking the ESM import.meta paths and using number of loads for op/s calculation, instead of doing it in startup benchmarks and nesting number of process/workers spawn for op/s calculation. PR-URL: https://github.com/nodejs/node/pull/50683 Reviewed-By: James M Snell Reviewed-By: Vinícius Lourenço Claro Cardoso --- benchmark/esm/import-meta.js | 32 +++++++++++++++++++++++++ benchmark/fixtures/esm-dir-file.mjs | 5 ++-- benchmark/fixtures/load-esm-dir-file.js | 5 ---- benchmark/misc/startup.js | 1 - 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 benchmark/esm/import-meta.js delete mode 100644 benchmark/fixtures/load-esm-dir-file.js diff --git a/benchmark/esm/import-meta.js b/benchmark/esm/import-meta.js new file mode 100644 index 00000000000000..fd371cf60ed6a3 --- /dev/null +++ b/benchmark/esm/import-meta.js @@ -0,0 +1,32 @@ +'use strict'; + +const path = require('path'); +const { pathToFileURL, fileURLToPath } = require('url'); +const common = require('../common'); +const assert = require('assert'); +const bench = common.createBenchmark(main, { + n: [1000], +}); + +const file = pathToFileURL( + path.resolve(__filename, '../../fixtures/esm-dir-file.mjs'), +); +async function load(array, n) { + for (let i = 0; i < n; i++) { + array[i] = await import(`${file}?i=${i}`); + } + return array; +} + +function main({ n }) { + const array = []; + for (let i = 0; i < n; ++i) { + array.push({ dirname: '', filename: '', i: 0 }); + } + + bench.start(); + load(array, n).then((arr) => { + bench.end(n); + assert.strictEqual(arr[n - 1].filename, fileURLToPath(file)); + }); +} diff --git a/benchmark/fixtures/esm-dir-file.mjs b/benchmark/fixtures/esm-dir-file.mjs index d5dafc5c46697e..a1028d57afd56b 100644 --- a/benchmark/fixtures/esm-dir-file.mjs +++ b/benchmark/fixtures/esm-dir-file.mjs @@ -1,3 +1,2 @@ -import assert from 'assert'; -assert.ok(import.meta.dirname); -assert.ok(import.meta.filename); +export const dirname = import.meta.dirname; +export const filename = import.meta.filename; diff --git a/benchmark/fixtures/load-esm-dir-file.js b/benchmark/fixtures/load-esm-dir-file.js deleted file mode 100644 index a4115dd39e0489..00000000000000 --- a/benchmark/fixtures/load-esm-dir-file.js +++ /dev/null @@ -1,5 +0,0 @@ -(async function () { - for (let i = 0; i < 1000; i += 1) { - await import(`./esm-dir-file.mjs?i=${i}`); - } -}()); diff --git a/benchmark/misc/startup.js b/benchmark/misc/startup.js index f55be1a7902e4f..07c0701d128899 100644 --- a/benchmark/misc/startup.js +++ b/benchmark/misc/startup.js @@ -9,7 +9,6 @@ const bench = common.createBenchmark(main, { script: [ 'benchmark/fixtures/require-builtins', 'test/fixtures/semicolon', - 'benchmark/fixtures/load-esm-dir-file', ], mode: ['process', 'worker'], count: [30],