@codspeed/tinybench-plugin
Version:
tinybench compatibility layer for CodSpeed
138 lines (125 loc) • 4.07 kB
JavaScript
import { tryIntrospect, Measurement, setupCore, optimizeFunction, mongoMeasurement, teardownCore, getGitDir } from '@codspeed/core';
import path from 'path';
import { fileURLToPath } from '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];
};
});
tryIntrospect();
function isCodSpeedBenchOptions(options) {
return "uri" in options;
}
function withCodSpeed(bench) {
if (!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"}`);
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 optimizeFunction(async () => {
await task.opts.beforeEach?.call(task);
await task.fn();
await task.opts.afterEach?.call(task);
});
await task.opts.beforeEach?.call(task);
await mongoMeasurement.start(uri);
global.gc?.();
await async function __codspeed_root_frame__() {
Measurement.startInstrumentation();
await task.fn();
Measurement.stopInstrumentation(uri);
}();
await mongoMeasurement.stop(uri);
await task.opts.afterEach?.call(task);
await task.opts.afterAll?.call(task);
console.log(` \u2714 Measured ${uri}`);
}
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 = getGitDir(callingFile);
if (gitDir === void 0) {
throw new Error("Could not find a git repository");
}
if (callingFile.startsWith("file://")) {
callingFile = fileURLToPath(callingFile);
}
return path.relative(gitDir, callingFile);
}
async function setupInstruments(body) {
if (!Measurement.isInstrumented()) {
console.warn("[CodSpeed] No instrumentation found, using default mongoUrl");
return { remoteAddr: body.mongoUrl };
}
return await mongoMeasurement.setupInstruments(body);
}
export { setupInstruments, withCodSpeed };
//# sourceMappingURL=index.es5.js.map