Skip to content

Commit

Permalink
benchmark: for esm when require.main is undefined, probe filename fro…
Browse files Browse the repository at this point in the history
…m error stack
  • Loading branch information
bumblehead committed Sep 20, 2024
1 parent 4f70132 commit 99999cc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ class Benchmark {
this._time = 0n;

// Use the file name as the name of the benchmark
this.name = require.main.filename.slice(__dirname.length + 1);
// For esm, require.main is undefined so probe filename from error stack
this.mainfilename = require.main
? require.main.filename
: (new Error).stack.split('\n')[3]
.match(/^.*(\(|at )(.*):[\d]*:[\d]*.*$/)[2]
.replace(/^file:\/\//, '')

this.name = require.main
? require.main.filename.slice(__dirname.length + 1)
: this.mainfilename.match(/[^\/]*$/)[0]

// Execution arguments i.e. flags used to run the jobs
this.flags = process.env.NODE_BENCHMARK_FLAGS?.split(/\s+/) ?? [];
Expand Down Expand Up @@ -224,7 +233,7 @@ class Benchmark {
childArgs.push(`${key}=${value}`);
}

const child = child_process.fork(require.main.filename, childArgs, {
const child = child_process.fork(this.mainfilename, childArgs, {
env: childEnv,
execArgv: this.flags.concat(process.execArgv),
});
Expand Down

0 comments on commit 99999cc

Please sign in to comment.