Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
suite.on('complete', function () {
if(bench.profile) {
const profile = profiler.stopProfiling(profileId);
profile.export((error, result) => {
if (error) {
return;
}
writeFile(`${dirname(bench.file)}/${profileId}`, result, () => {
profile.delete();
});
});
}
});
// add listeners
it(basename(testCaseDirectoryPath), function () {
const testCase = loadInputFiles(testCaseDirectoryPath, options);
if(options.shouldSkip && options.shouldSkip(testCase, basename(testCaseDirectoryPath))) {
return this.skip();
}
if(options.shouldError && options.shouldError(testCase)) {
expect(testFunction.bind(null, testCase, basename(testCaseDirectoryPath))).to.throw;
} else {
const profileId = `${name}-${Date.now()}.profile`;
const profilingDirectory = process.env.GEN_PROFILE_DIR;
if (profilingDirectory) {
profiler.startProfiling(profileId);
}
const result = testFunction(testCase, basename(testCaseDirectoryPath));
if (profilingDirectory) {
const profile = profiler.stopProfiling(profileId);
generateProfileReport(profile, profilingDirectory, profileId);
}
const expected = options.getExpected(testCase);
options.expectFunc(testCase, expected, result, basename(testCaseDirectoryPath));
}
});
}
it(description, function () {
const inputs = getInput(testCase);
if (shouldError(testCase, index)) {
expect(testFunc.bind(null, ...inputs)).to.throw();
} else {
const profileId = `${description}-${Date.now()}.profile`;
if (env.GEN_PROFILE_DIR) {
profiler.startProfiling(profileId);
}
const result = testFunc(...inputs);
if (env.GEN_PROFILE_DIR) {
const profile = profiler.stopProfiling(profileId);
const directory = env.GEN_PROFILE_DIR || __dirname;
profile.export((error, result) => {
if (error) {
return;
}
writeFile(`${directory}/${profileId}`, result, () => {
profile.delete();
});
});
}
const actual = getActual(result);
const expected = getExpected(testCase);
it(description, function () {
const inputs = getInput(testCase);
if (shouldError(testCase, index)) {
expect(testFunc.bind(null, ...inputs)).to.throw();
} else {
const profileId = `${description}-${Date.now()}.profile`;
if (env.GEN_PROFILE_DIR) {
profiler.startProfiling(profileId);
}
const result = testFunc(...inputs);
if (env.GEN_PROFILE_DIR) {
const profile = profiler.stopProfiling(profileId);
const directory = env.GEN_PROFILE_DIR || __dirname;
profile.export((error, result) => {
if (error) {
return;
}
writeFile(`${directory}/${profileId}`, result, () => {
profile.delete();
});
});
}
const actual = getActual(result);
const expected = getExpected(testCase);
expectFunc(testCase, expect, expected, actual);
}
});
it(basename(testCaseDirectoryPath), function () {
const testCase = loadInputFiles(testCaseDirectoryPath, options);
if(options.shouldSkip && options.shouldSkip(testCase, basename(testCaseDirectoryPath))) {
return this.skip();
}
if(options.shouldError && options.shouldError(testCase)) {
expect(testFunction.bind(null, testCase, basename(testCaseDirectoryPath))).to.throw;
} else {
const profileId = `${name}-${Date.now()}.profile`;
const profilingDirectory = process.env.GEN_PROFILE_DIR;
if (profilingDirectory) {
profiler.startProfiling(profileId);
}
const result = testFunction(testCase, basename(testCaseDirectoryPath));
if (profilingDirectory) {
const profile = profiler.stopProfiling(profileId);
generateProfileReport(profile, profilingDirectory, profileId);
}
const expected = options.getExpected(testCase);
options.expectFunc(testCase, expected, result, basename(testCaseDirectoryPath));
}
});
}
export function profile(name: string) {
profiler.startProfiling(name, true);
return () => {
let profile = profiler.stopProfiling();
new Promise(resolve =>
profile
.export()
.pipe(fs.createWriteStream(name + ".cpuprofile"))
.on("finish", function() {
profile.delete();
resolve();
})
);
};
}
return () => {
let profile = profiler.stopProfiling();
new Promise(resolve =>
profile
.export()
.pipe(fs.createWriteStream(name + ".cpuprofile"))
.on("finish", function() {
profile.delete();
resolve();
})
);
};
}
router.get('/start-profiling', (ctx, next) => {
v8Profiler.startProfiling('p1')
ctx.body = 'started'
})
suite.on('start', function () {
if(bench.profile) {
profiler.startProfiling(profileId);
}
});
suite.on('complete', function () {
Profiler.prototype.createProfile = function(timeout, callback) {
if (typeof timeout === 'function') {
callback = timeout;
timeout = 10000;
}
const path = `${this.heapshotDir}/${this.serviceName}-profile-${moment().unix().toString()}.cpuprofile`;
process.send(JSON.stringify({ msg: 'pause_monitoring' }));
v8Profiler.startProfiling(path, true);
setTimeout(() => {
const profile = v8Profiler.stopProfiling(path);
profile.export((err, result) => {
if (err) {
return callback(err);
}
fs.writeFileSync(path, result);
profile.delete();
setTimeout(function () {
process.send(JSON.stringify({ msg: 'resume_monitoring' }));
}, ms('5s'));
return callback(null, path);
});
}, timeout);
};