UNPKG

1.31 kBJavaScriptView Raw
1'use strict'
2
3const typeMap = new Map([
4 ['S', 'V8.GCScavenger'],
5 ['M', 'V8.GCIncrementalMarking'],
6 ['F', 'V8.GCIncrementalMarkingFinalize'],
7 ['C', 'V8.GCFinalizeMC'],
8 [' ', 0],
9 ['.', 10],
10 ['-', 100]
11])
12
13function isBreak (type) {
14 return typeof type === 'number'
15}
16
17function isGCEvent (type) {
18 return typeof type === 'string'
19}
20
21function generateTraceEvent (chars, timeStretching) {
22 timeStretching = timeStretching || 1
23
24 const output = []
25
26 let lastType = 0
27 let startTimestamp = 0
28 let endTimestamp = 0
29 for (let i = 0; i <= chars.length; i++) {
30 const type = i === chars.length ? 0 : typeMap.get(chars[i])
31
32 if (lastType !== type && isGCEvent(lastType)) {
33 output.push({
34 pid: 0,
35 tid: 0,
36 ts: startTimestamp * 1e3,
37 ph: 'X',
38 cat: 'v8',
39 name: lastType,
40 dur: (endTimestamp - startTimestamp) * 1e3,
41 args: {
42 startTimestamp: startTimestamp,
43 endTimestamp: endTimestamp
44 }
45 })
46
47 startTimestamp = endTimestamp
48 } else if (isBreak(lastType)) {
49 startTimestamp = endTimestamp
50 }
51
52 // update the end timestamp in each iteration
53 endTimestamp += (isBreak(type) ? type : 10) * timeStretching
54 lastType = type
55 }
56
57 return output
58}
59
60module.exports = generateTraceEvent