-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.mjs
64 lines (55 loc) · 1.62 KB
/
profile.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import inspector from 'node:inspector';
import fs from 'fs';
import path from 'path';
import readline from 'readline';
import { fileURLToPath } from 'url';
// 创建 inspector 会话
const session = new inspector.Session();
session.connect();
// 获取 __dirname 等效路径
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 启用性能分析
session.post('Profiler.enable', (err) => {
if (err) {
console.error('Error enabling profiler:', err);
return;
}
// 启动性能分析
session.post('Profiler.start', async (err) => {
if (err) {
console.error('Error starting profiler:', err);
return;
}
try {
// 动态导入并启动 web server
await import('@electron-forge/cli/dist/electron-forge-start.js');
console.log('Web server started. Press Ctrl+C to stop profiling and generate report.');
} catch (importErr) {
console.error('Error importing module:', importErr);
}
});
});
// 监听 Ctrl+C 事件以停止性能分析
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('SIGINT', () => {
session.post('Profiler.stop', (err, { profile }) => {
if (err) {
console.error('Error stopping profiler:', err);
return;
}
// 将性能数据保存到文件
const filePath = path.resolve(__dirname, 'profile.json');
fs.writeFile(filePath, JSON.stringify(profile), (err) => {
if (err) {
console.error('Error saving profile:', err);
return;
}
console.log(`Profile saved to ${filePath}`);
process.exit();
});
});
});