forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-tracer.test.js
198 lines (186 loc) · 5.84 KB
/
js-tracer.test.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { storeWithProfile } from '../fixtures/stores';
import { selectedThreadSelectors } from '../../selectors/per-thread';
import { ensureExists } from '../../utils/flow';
import { changeShowJsTracerSummary } from '../../actions/profile-view';
import {
getProfileFromTextSamples,
getProfileWithJsTracerEvents,
type TestDefinedJsTracerEvent,
} from '../fixtures/profiles/processed-profile';
describe('selectors/getJsTracerTiming', function() {
describe('full stack-based view', function() {
it('has no JS tracer timing if no js tracer info is present', function() {
const { profile } = getProfileFromTextSamples('A');
const { getState } = storeWithProfile(profile);
expect(profile.threads[0].jsTracer).toBe(undefined);
expect(
selectedThreadSelectors.getExpensiveJsTracerTiming(getState())
).toEqual(null);
});
it('has empty JS tracer timing if no events are in the js tracer table', function() {
expect(
getHumanReadableJsTracerTiming({ useSelfTime: false, events: [] })
).toEqual([]);
});
it('can generate some simple nested timing', function() {
expect(
getHumanReadableJsTracerTiming({
useSelfTime: false,
events: [
['https://mozilla.org', 0, 20],
['Interpreter', 1, 19],
['IonMonkey', 2, 18],
],
})
).toEqual([
'https://mozilla.org (0:20)',
'Interpreter (1:19)',
'IonMonkey (2:18)',
]);
});
it('can generate sibling timing', function() {
expect(
getHumanReadableJsTracerTiming({
useSelfTime: false,
events: [
['https://mozilla.org', 0, 20],
['Interpreter', 1, 5],
['Interpreter', 2, 4],
['IonMonkey', 5, 19],
['IonMonkey', 6, 18],
],
})
).toEqual([
'https://mozilla.org (0:20)',
'Interpreter (1:5) | IonMonkey (5:19)',
'Interpreter (2:4) | IonMonkey (6:18)',
]);
});
});
describe('self time', function() {
it('has empty JS tracer timing if no events are in the js tracer table', function() {
expect(
getHumanReadableJsTracerTiming({ useSelfTime: true, events: [] })
).toEqual([]);
});
it('can generate some simple nested timing', function() {
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
['https://mozilla.org', 0, 20],
['Interpreter', 1, 19],
['IonMonkey', 2, 18],
],
})
).toEqual([
'Interpreter (1:2) | Interpreter (18:19)',
'IonMonkey (2:18)',
'https://mozilla.org (0:1) | https://mozilla.org (19:20)',
]);
});
it('can "drain off" prefixes the first if branch of the algorithm', function() {
// This test is checking a specific if branch of the timing function:
//
// xxxxxxxxxxxxxxxx[================]
// xxxxxxxx[======] [current]
// [prefix]
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
// This comment makes the formatting "prettier".
['A', 0, 2],
['B', 0, 2],
['C', 0, 1],
['A', 2, 3],
],
})
).toEqual([
// This comment makes the formatting "prettier".
'A (2:3)',
'B (1:2)',
'C (0:1)',
]);
});
it('handles float precision errors where the child event outlasts the parent', function() {
// 0 1 2 3 4 5
// [prefix--]
// [current-]
// 0 1 2 3 4 5
// [A-------]
// [B-------]
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
// This comment makes the formatting "prettier".
['A', 0, 3],
['B', 1, 4],
],
})
).toEqual([
// This comment makes the formatting "prettier".
'A (0:1)',
'B (1:3)',
]);
});
it('can split a prefix stack if the ending time matches', function() {
// This test is checking a specific if branch of the timing function:
//
// [prefix]xxxxxxxxx
// [current]
expect(
getHumanReadableJsTracerTiming({
useSelfTime: true,
events: [
// This comment makes the formatting "prettier".
['A', 0, 2],
['B', 1, 2],
],
})
).toEqual([
// This comment makes the formatting "prettier".
'A (0:1)',
'B (1:2)',
]);
});
});
});
/**
* Create JS tracing information from a list of events, and then compute a human
* readable representation of the timing.
*
* "EventName1 (StartTime:EndTime) | EventName2 (StartTime:EndTime)"
*/
function getHumanReadableJsTracerTiming({
useSelfTime,
events,
}: {|
useSelfTime: boolean,
events: TestDefinedJsTracerEvent[],
|}): string[] {
const profile = getProfileWithJsTracerEvents(events);
const { dispatch, getState } = storeWithProfile(profile);
const computeTiming = useSelfTime
? selectedThreadSelectors.getExpensiveJsTracerLeafTiming
: selectedThreadSelectors.getExpensiveJsTracerTiming;
dispatch(changeShowJsTracerSummary(useSelfTime));
return ensureExists(computeTiming(getState())).map(
({ start, end, label, length }) => {
const lines = [];
for (let i = 0; i < length; i++) {
lines.push(
`${label[i]} (${parseFloat(start[i].toFixed(2))}:${parseFloat(
end[i].toFixed(2)
)})`
);
}
return lines.join(' | ');
}
);
}