-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.js
executable file
·153 lines (139 loc) · 4.59 KB
/
bin.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const c2s = require('cpuprofile2stackcollapse');
const { spawn, execSync } = require('child_process');
const { Transform } = require('stream');
const path = require('path');
const opn = require('opn');
const os = require('os');
let args = process.argv.slice(2);
let icicleArgs = [];
if (args.includes('--icicle')) {
args = args.filter(arg => arg !== '--icicle');
icicleArgs = ['--inverted', '--reverse'];
}
if (args.length === 1) {
oneFile(...getStream(args[0]));
} else if (args.length === 2) {
diffFiles(...getStream(args[0]), ...getStream(args[1]));
}
function flamegraph(command, args = []) {
return spawn(path.join(__dirname, 'FlameGraph', `${command}.pl`), args);
}
function getStream(filename) {
const longname = normalizeFilename(filename);
const extname = path.extname(longname);
switch (extname) {
case '.cpuprofile':
return [fs.createReadStream(longname).pipe(c2s()), longname];
case '.data':
return [perfDataStream(longname), longname];
case '.log':
return [preprocessLog(longname).pipe(preprocessedToStacks()), longname];
default:
throw new Error('unrecognized file format');
}
}
function preprocessLog(filename) {
return spawn('node', ['--prof-process', '--preprocess', '--ignore-unknown', filename]).stdout;
}
function preprocessedToStacks() {
const output = new Transform({
transform(chunk, encoding, callback) {
if (!this._bufs) {
this._bufs = [];
}
this._bufs.push(chunk);
callback();
},
flush(callback) {
var data = JSON.parse(Buffer.concat(this._bufs).toString());
const stacks = {};
data.ticks.forEach(tick => {
const converted = tick.s.map(n => data.code[n]).reverse();
const collapsed = converted.map(n => n ? `(${n.type}) ${n.name}` : '(unknown)').join(';');
stacks[collapsed] = stacks[collapsed] ? stacks[collapsed] + 1 : 1;
});
for (const [stack, hits] of Object.entries(stacks)) {
this.push(`${stack} ${hits}\n`);
}
callback();
}
});
return output;
}
function perfDataStream(filename) {
const stackcollapseProc = flamegraph('stackcollapse-perf');
let needsRoot = false;
try {
fs.accessSync(filename, fs.constants.R_OK);
} catch (e) {
needsRoot = true;
}
const perfScriptProc = needsRoot ?
spawn('sudo', ['perf', 'script', '-i', filename]) :
spawn('perf', ['script', '-i', filename]);
perfScriptProc.stdout.pipe(stackcollapseProc.stdin);
return stackcollapseProc.stdout;
}
function normalizeFilename(filename) {
if (!path.isAbsolute(filename)) {
return path.resolve(filename);
}
return filename;
}
function openInBrowser(filename) {
if ('BROWSER' in process.env) {
execSync(`${process.env.BROWSER} file://${filename}`);
} else {
opn(`${filename}`);
}
}
function oneFile(strm, filename) {
const flameProc = flamegraph('flamegraph', icicleArgs);
strm.pipe(flameProc.stdin);
const svgBufs = [];
flameProc.stdout.on('data', d => svgBufs.push(d));
flameProc.stdout.on('end', () => {
const svg = Buffer.concat(svgBufs);
const svgFilename = path.join(process.cwd(), `${path.basename(filename)}.html`);
fs.writeFile(svgFilename, svg, () => {
console.log(`Flamechart saved as ${svgFilename}`);
openInBrowser(svgFilename);
});
});
}
function diffFiles(strm1, filename1, strm2, filename2) {
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'pflames-'));
const stackFile1 = path.join(tmpdir, '1.stacks');
const stackFile2 = path.join(tmpdir, '2.stacks');
let toBeDone = 2;
const done = () => {
if (--toBeDone !== 0) {
return;
}
const svgBufs = [];
const difffoldedProc = flamegraph('difffolded', [stackFile1, stackFile2]);
const flameProc = flamegraph('flamegraph', icicleArgs);
difffoldedProc.stdout.pipe(flameProc.stdin);
flameProc.stdout.on('data', d => svgBufs.push(d));
flameProc.stdout.on('end', () => {
const svg = Buffer.concat(svgBufs);
const svgFilename = path.join(process.cwd(), `diff-${path.basename(filename1)}-${path.basename(filename2)}.html`);
fs.writeFile(svgFilename, svg, () => {
console.log(`Flamechart saved as ${svgFilename}`);
openInBrowser(svgFilename);
fs.unlinkSync(stackFile1);
fs.unlinkSync(stackFile2);
fs.rmdirSync(tmpdir);
});
});
};
const writeStream1 = fs.createWriteStream(stackFile1);
writeStream1.on('close', done);
const writeStream2 = fs.createWriteStream(stackFile2);
writeStream2.on('close', done);
strm1.pipe(writeStream1);
strm2.pipe(writeStream2);
}