@codspeed/tinybench-plugin
Version:
tinybench compatibility layer for CodSpeed
141 lines (127 loc) • 4.08 kB
JavaScript
;
var core = require('@codspeed/core');
var path = require('path');
var url = require('url');
function get(belowFn) {
const oldLimit = Error.stackTraceLimit;
Error.stackTraceLimit = Infinity;
const dummyObject = {};
const v8Handler = Error.prepareStackTrace;
Error.prepareStackTrace = function(dummyObject, v8StackTrace) {
return v8StackTrace;
};
Error.captureStackTrace(dummyObject, belowFn || get);
const v8StackTrace = dummyObject.stack;
Error.prepareStackTrace = v8Handler;
Error.stackTraceLimit = oldLimit;
return v8StackTrace;
}
function CallSite(properties) {
for (const property in properties) {
this[property] = properties[property];
}
}
const strProperties = [
'this',
'typeName',
'functionName',
'methodName',
'fileName',
'lineNumber',
'columnNumber',
'function',
'evalOrigin'
];
const boolProperties = [
'topLevel',
'eval',
'native',
'constructor'
];
strProperties.forEach(function (property) {
CallSite.prototype[property] = null;
CallSite.prototype['get' + property[0].toUpperCase() + property.substr(1)] = function () {
return this[property];
};
});
boolProperties.forEach(function (property) {
CallSite.prototype[property] = false;
CallSite.prototype['is' + property[0].toUpperCase() + property.substr(1)] = function () {
return this[property];
};
});
core.tryIntrospect();
function isCodSpeedBenchOptions(options) {
return "uri" in options;
}
function withCodSpeed(bench) {
if (!core.Measurement.isInstrumented()) {
const rawRun = bench.run;
bench.run = async () => {
console.warn(
`[CodSpeed] ${bench.tasks.length} benches detected but no instrumentation found, falling back to tinybench`
);
return await rawRun.bind(bench)();
};
return bench;
}
const rawAdd = bench.add;
bench.add = (name, fn, opts) => {
const callingFile = getCallingFile();
const uri = `${callingFile}::${name}`;
const options = Object.assign({}, opts ?? {}, { uri });
return rawAdd.bind(bench)(name, fn, options);
};
const rootCallingFile = getCallingFile();
bench.run = async () => {
console.log(`[CodSpeed] running with @codspeed/tinybench v${"4.0.1"}`);
core.setupCore();
for (const task of bench.tasks) {
const uri = isCodSpeedBenchOptions(task.opts) ? task.opts.uri : `${rootCallingFile}::${task.name}`;
await task.opts.beforeAll?.call(task);
await core.optimizeFunction(async () => {
await task.opts.beforeEach?.call(task);
await task.fn();
await task.opts.afterEach?.call(task);
});
await task.opts.beforeEach?.call(task);
await core.mongoMeasurement.start(uri);
global.gc?.();
await async function __codspeed_root_frame__() {
core.Measurement.startInstrumentation();
await task.fn();
core.Measurement.stopInstrumentation(uri);
}();
await core.mongoMeasurement.stop(uri);
await task.opts.afterEach?.call(task);
await task.opts.afterAll?.call(task);
console.log(` \u2714 Measured ${uri}`);
}
core.teardownCore();
console.log(`[CodSpeed] Done running ${bench.tasks.length} benches.`);
return bench.tasks;
};
return bench;
}
function getCallingFile() {
const stack = get();
let callingFile = stack[2].getFileName();
const gitDir = core.getGitDir(callingFile);
if (gitDir === void 0) {
throw new Error("Could not find a git repository");
}
if (callingFile.startsWith("file://")) {
callingFile = url.fileURLToPath(callingFile);
}
return path.relative(gitDir, callingFile);
}
async function setupInstruments(body) {
if (!core.Measurement.isInstrumented()) {
console.warn("[CodSpeed] No instrumentation found, using default mongoUrl");
return { remoteAddr: body.mongoUrl };
}
return await core.mongoMeasurement.setupInstruments(body);
}
exports.setupInstruments = setupInstruments;
exports.withCodSpeed = withCodSpeed;
//# sourceMappingURL=index.cjs.js.map