forked from clinicjs/node-clinic-flame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (139 loc) · 5.17 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
'use strict'
const events = require('events')
const x = require('0x')
const path = require('path')
const fs = require('fs')
const rimraf = require('rimraf')
const getLoggingPaths = require('./collect/get-logging-paths.js')
const systemInfo = require('./collect/system-info.js')
const inlinedFunctions = require('./collect/inlined-functions.js')
const analyse = require('./analysis/index.js')
const pump = require('pump')
const buildJs = require('@nearform/clinic-common/scripts/build-js')
const buildCss = require('@nearform/clinic-common/scripts/build-css')
const mainTemplate = require('@nearform/clinic-common/templates/main')
class ClinicFlame extends events.EventEmitter {
constructor (settings = {}) {
super()
const {
detectPort = false,
debug = false,
dest = null
} = settings
this.detectPort = detectPort
this.debug = debug
this.path = dest
}
collect (args, cb) {
const argv = args.slice(1)
const self = this
const paths = getLoggingPaths({
path: this.path,
identifier: '{pid}' // replaced with actual pid by 0x
})
callbackify(x({
argv,
onPort: this.detectPort ? onPort : undefined,
onProcessExit: () => {
this.emit('analysing')
},
pathToNodeBinary: args[0],
collectOnly: true,
writeTicks: true,
outputDir: paths['/0x-data/'],
workingDir: '.' // 0x temporary working files, doesn't support placeholders like {pid}
}), done)
function done (err, dir) {
/* istanbul ignore if: currently hard to cause, we can cover this when 0x returns an error on SIGKILL */
if (err) return cb(err)
const pidMatch = dir.match(/(?:\/|\\)(\d+)\.clinic-flame-0x-data/)
const pid = pidMatch ? pidMatch[1]
/* istanbul ignore next: can't reliably cause 0x to detect no pid without hacking 0x */
: 'UNKNOWN_PID' // 0x's fallback if, somehow, no PID was detected
let count = 0
function next (err) {
// istanbul ignore if
if (err) return cb(err)
count += 1
if (count < 3) return
rimraf(paths['/0x-data/'], (err) => {
// istanbul ignore if
if (err) return cb(err)
cb(null, paths['/'])
})
}
const paths = getLoggingPaths({ path: self.path, identifier: pid })
fs.writeFile(paths['/systeminfo'], JSON.stringify(systemInfo(paths['/0x-data/']), null, 2), next)
fs.writeFile(paths['/inlinedfunctions'], JSON.stringify(inlinedFunctions(paths['/0x-data/'])), next)
// TODO maybe gzip this? it can be very big and gzip can easily save 80~95%
fs.rename(path.join(paths['/0x-data/'], 'ticks.json'), paths['/samples'], next)
}
function onPort (port, cb) {
self.emit('port', Number(port.toString()), null, cb)
}
}
visualize (outputDir, outputFilename, callback) {
const paths = getLoggingPaths({ path: outputDir })
callbackify(analyse(paths), (err, data) => {
if (err) return callback(err)
// data.merged → call tree where optimized and unoptimized versions of the same function are in a single frame
// data.unmerged → call tree where optimized and unoptimized versions of the same function are in separate frames
this.writeHtml(data, outputFilename, callback)
})
}
writeHtml (data, outputFilename, callback) {
// TODO: migrate most of this to clinic-common
const fakeDataPath = path.join(__dirname, 'visualizer', 'data.json')
const stylePath = path.join(__dirname, 'visualizer', 'style.css')
const scriptPath = path.join(__dirname, 'visualizer', 'main.js')
const logoPath = path.join(__dirname, 'visualizer/assets', 'flame-logo.svg')
const nearFormLogoPath = path.join(__dirname, 'visualizer', 'nearform-logo.svg')
const clinicFaviconPath = path.join(__dirname, 'visualizer', 'clinic-favicon.png.b64')
// add logos
const logoFile = fs.createReadStream(logoPath)
const nearFormLogoFile = fs.createReadStream(nearFormLogoPath)
const clinicFaviconBase64 = fs.createReadStream(clinicFaviconPath)
// build JS
const scriptFile = buildJs({
basedir: __dirname,
debug: this.debug,
fakeDataPath,
scriptPath,
beforeBundle: b => b.require({
source: JSON.stringify(data),
file: fakeDataPath
}),
env: {
PRESENTATION_MODE: process.env.PRESENTATION_MODE
}
})
// build CSS
const styleFile = buildCss({
stylePath,
debug: this.debug
})
// generate HTML
const outputFile = mainTemplate({
favicon: clinicFaviconBase64,
title: 'Clinic Flame',
styles: styleFile,
script: scriptFile,
headerLogoUrl: 'https://github.com/nearform/node-clinic-flame',
headerLogoTitle: 'Clinic Flame on GitHub',
headerLogo: logoFile,
headerText: 'Flame',
nearFormLogo: nearFormLogoFile,
uploadId: outputFilename.split('/').pop().split('.html').shift(),
body: '<main></main>'
})
pump(
outputFile,
fs.createWriteStream(outputFilename),
callback
)
}
}
function callbackify (p, cb) {
p.then(val => process.nextTick(cb, null, val)).catch(err => process.nextTick(cb, err))
}
module.exports = ClinicFlame