-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtracing.ts
94 lines (82 loc) · 2.55 KB
/
tracing.ts
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
import Debug from 'debug'
const debug = Debug('axm:tracing')
import ProfilingType from '../profiling/profilingType'
import { ServiceManager } from '../serviceManager'
import { InspectorService } from '../services/inspector'
export interface TraceEvent {
pid: Number
tid: Number
ts: Number
tts: Number
name: String
cat: String
dur: Number
tdur: Number
args: Object
}
export interface TraceEventsCollection {
value: TraceEvent[]
}
export interface TraceEventsCollected {
method: String,
data: TraceEventsCollection
}
export default class Tracing implements ProfilingType {
private inspectorService: InspectorService
private traceConfig: Object = {
includedCategories: ['node', 'v8'],
recordContinuously: true
}
constructor () {
this.inspectorService = ServiceManager.get('inspector')
}
init () {
debug('init tracing feature')
if (!this.inspectorService) throw new Error(`Inspector service not initialized`)
this.inspectorService.createSession()
this.inspectorService.connect()
}
destroy () {
this.inspectorService.disconnect()
}
async start () {
debug('starting collection trace events data')
return await this.inspectorService.post('NodeTracing.start', {
traceConfig: this.traceConfig
})
}
async stop (): Promise<string> {
return this.getProfileInfo()
}
private getProfileInfo (): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const buffer: TraceEventsCollection[] = []
const onTracingData = (event: TraceEventsCollected) => {
debug('receiving trace events data')
buffer.push(event.data)
}
const onTracingEnd = _ => {
// cleanup listeners
debug('received end of trace events')
this.inspectorService.removeListener('dataCollected', onTracingData)
this.inspectorService.removeListener('tracingComplete', onTracingEnd)
const flattenEvents = buffer.reduce((agg: TraceEvent[], events: TraceEventsCollection) => {
agg = agg.concat(events.value)
return agg
}, [])
return resolve(JSON.stringify(flattenEvents))
}
this.inspectorService.on('NodeTracing.dataCollected', onTracingData)
this.inspectorService.on('NodeTracing.tracingComplete', onTracingEnd)
// stop tracing
await this.inspectorService.post('NodeTracing.stop', {
traceConfig: this.traceConfig
})
} catch (err) {
debug('tracing stopped !')
return reject(err)
}
})
}
}