我有一个基于 Typescript 的 React 项目,我在其中运行笑话测试(也在 TS 中)。我可以很好地运行测试,但我正在尝试分析一些需要相当长时间才能运行的测试的性能。我尝试过使用 Chrome Devtools 附加到测试,确实如此,但由于它是 TS 而不是普通的 Js,所以失败了。有什么方法可以单独分析我的测试以查看性能问题发生在哪里?使用 VS 代码。
它对我来说只是一个常规的 TypeScript 库,而不是 React 项目,但我敢打赌这也适用于您的用例。我将其留在这里,以防万一它可用,或者供将来的我使用。
我发现唯一有效的解决方案是手动设置探查器v8-profiler-next。
import * as fs from 'fs';
import * as v8Profiler from 'v8-profiler-next';
v8Profiler.setGenerateType(1);
const title = 'good-name';
describe('Should be able to generate with inputs', () => {
v8Profiler.startProfiling(title, true);
afterAll(() => {
const profile = v8Profiler.stopProfiling(title);
profile.export(function (error, result: any) {
// if it doesn't have the extension .cpuprofile then
// chrome's profiler tool won't like it.
// examine the profile:
// Navigate to chrome://inspect
// Click Open dedicated DevTools for Node
// Select the profiler tab
// Load your file
fs.writeFileSync(`${title}.cpuprofile`, result);
profile.delete();
});
});
test('....', async () => {
// Add test
});
});
然后,这将为您提供 CPU 配置文件,它可以与 TypeScript 配合良好。