mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
benchmark: add source map and source map cache
PR-URL: https://github.com/nodejs/node/pull/58125 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
parent
839b25e371
commit
31252b9af1
3 changed files with 162 additions and 0 deletions
60
benchmark/source_map/source-map-cache.js
Normal file
60
benchmark/source_map/source-map-cache.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const assert = require('assert');
|
||||
const fixtures = require('../../test/common/fixtures');
|
||||
|
||||
const bench = common.createBenchmark(
|
||||
main,
|
||||
{
|
||||
operation: [
|
||||
'findSourceMap-valid',
|
||||
'findSourceMap-generated-source',
|
||||
],
|
||||
n: [1e5],
|
||||
},
|
||||
);
|
||||
|
||||
function main({ operation, n }) {
|
||||
const Module = require('node:module');
|
||||
|
||||
Module.setSourceMapsSupport(true, {
|
||||
generatedCode: true,
|
||||
});
|
||||
const validFileName = fixtures.path('test-runner/source-maps/line-lengths/index.js');
|
||||
|
||||
const fileNameKey = '/source-map/disk.js';
|
||||
const generatedFileName = fixtures.path(fileNameKey);
|
||||
const generatedFileContent = fixtures.readSync(fileNameKey, 'utf8');
|
||||
const sourceMapUrl = generatedFileName.replace(/\.js$/, '.map');
|
||||
const sourceWithGeneratedSourceMap =
|
||||
`${generatedFileContent}\n//# sourceMappingURL=${sourceMapUrl}\n//# sourceURL=${generatedFileName}`;
|
||||
const generatedExpectedUrl = `file://${generatedFileName}`;
|
||||
|
||||
let sourceMap;
|
||||
switch (operation) {
|
||||
case 'findSourceMap-valid':
|
||||
require(validFileName);
|
||||
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMap = Module.findSourceMap(validFileName);
|
||||
}
|
||||
bench.end(n);
|
||||
break;
|
||||
|
||||
case 'findSourceMap-generated-source':
|
||||
eval(sourceWithGeneratedSourceMap);
|
||||
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMap = Module.findSourceMap(generatedExpectedUrl);
|
||||
}
|
||||
bench.end(n);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown operation: ${operation}`);
|
||||
}
|
||||
assert.ok(sourceMap);
|
||||
}
|
95
benchmark/source_map/source-map.js
Normal file
95
benchmark/source_map/source-map.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const assert = require('assert');
|
||||
const fixtures = require('../../test/common/fixtures');
|
||||
|
||||
const bench = common.createBenchmark(
|
||||
main,
|
||||
{
|
||||
operation: [
|
||||
'parse',
|
||||
'parse-sectioned',
|
||||
'findEntry',
|
||||
'findEntry-sectioned',
|
||||
'findOrigin',
|
||||
'findOrigin-sectioned',
|
||||
],
|
||||
n: [1e5],
|
||||
},
|
||||
);
|
||||
|
||||
function main({ operation, n }) {
|
||||
const { SourceMap } = require('node:module');
|
||||
|
||||
const samplePayload = JSON.parse(
|
||||
fixtures.readSync('source-map/no-source.js.map', 'utf8'),
|
||||
);
|
||||
const sectionedPayload = JSON.parse(
|
||||
fixtures.readSync('source-map/disk-index.map', 'utf8'),
|
||||
);
|
||||
|
||||
let sourceMap;
|
||||
let sourceMapMethod;
|
||||
switch (operation) {
|
||||
case 'parse':
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMap = new SourceMap(samplePayload);
|
||||
}
|
||||
bench.end(n);
|
||||
break;
|
||||
|
||||
case 'parse-sectioned':
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMap = new SourceMap(sectionedPayload);
|
||||
}
|
||||
bench.end(n);
|
||||
break;
|
||||
|
||||
case 'findEntry':
|
||||
sourceMap = new SourceMap(samplePayload);
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMapMethod = sourceMap.findEntry(i, i);
|
||||
}
|
||||
bench.end(n);
|
||||
assert.ok(sourceMapMethod);
|
||||
break;
|
||||
|
||||
case 'findEntry-sectioned':
|
||||
sourceMap = new SourceMap(sectionedPayload);
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMapMethod = sourceMap.findEntry(i, i);
|
||||
}
|
||||
bench.end(n);
|
||||
assert.ok(sourceMapMethod);
|
||||
break;
|
||||
|
||||
case 'findOrigin':
|
||||
sourceMap = new SourceMap(samplePayload);
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMapMethod = sourceMap.findOrigin(i, i);
|
||||
}
|
||||
bench.end(n);
|
||||
assert.ok(sourceMapMethod);
|
||||
break;
|
||||
|
||||
case 'findOrigin-sectioned':
|
||||
sourceMap = new SourceMap(sectionedPayload);
|
||||
bench.start();
|
||||
for (let i = 0; i < n; i++) {
|
||||
sourceMapMethod = sourceMap.findOrigin(i, i);
|
||||
}
|
||||
bench.end(n);
|
||||
assert.ok(sourceMapMethod);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown operation: ${operation}`);
|
||||
}
|
||||
assert.ok(sourceMap);
|
||||
}
|
7
test/benchmark/test-benchmark-source-map.js
Normal file
7
test/benchmark/test-benchmark-source-map.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
require('../common');
|
||||
|
||||
const runBenchmark = require('../common/benchmark');
|
||||
|
||||
runBenchmark('source_map', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
|
Loading…
Add table
Add a link
Reference in a new issue