forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.test.js
439 lines (396 loc) · 14.3 KB
/
actions.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* 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 * as ProfileViewSelectors from '../../reducers/profile-view';
import * as UrlStateSelectors from '../../reducers/url-state';
import {
changeCallTreeSearchString,
changeInvertCallstack,
updateProfileSelection,
changeImplementationFilter,
changeSelectedCallNode,
} from '../../actions/profile-view';
import { getProfileFromTextSamples } from '../fixtures/profiles/make-profile';
const { selectedThreadSelectors } = ProfileViewSelectors;
describe('selectors/getStackTimingByDepth', function() {
/**
* This table shows off how a stack chart gets filtered to JS only, where the number is
* the stack index, and P is platform code, and J javascript.
*
* Unfiltered -> JS Only
* 0-10-20-30-40-50-60-70-80-90-91 0-10-20-30-40-50-60-70-80-90-91 <- Timing (ms)
* ================================ ================================
* 0P 0P 0P 0P 0P 0P 0P 0P 0P 0P | 0P 0P 0P 0P 0P 0P 0P 0P 0P 0P |
* 1P 1P 1P 1P 1P 1P 1P 1P 1P | 1J 1J 1J 1J |
* 2P 2P 3P 4J 4J 4J 4J | 2J 2J |
* 5J 5J | 3P |
* 6P | 4J |
* 7P |
* 8J |
*/
it('computes unfiltered stack timing by depth', function() {
const store = storeWithProfile();
const stackTimingByDepth = selectedThreadSelectors.getStackTimingByDepth(
store.getState()
);
expect(stackTimingByDepth).toEqual([
{ start: [0], end: [91], stack: [0], length: 1 },
{ start: [0, 50], end: [40, 91], stack: [1, 1], length: 2 },
{ start: [10, 30, 60], end: [30, 40, 91], stack: [2, 3, 4], length: 3 },
{ start: [70], end: [90], stack: [5], length: 1 },
{ start: [80], end: [90], stack: [6], length: 1 },
{ start: [80], end: [90], stack: [7], length: 1 },
{ start: [80], end: [90], stack: [8], length: 1 },
]);
});
it('uses search strings', function() {
const store = storeWithProfile();
store.dispatch(changeCallTreeSearchString('javascript'));
const stackTimingByDepth = selectedThreadSelectors.getStackTimingByDepth(
store.getState()
);
expect(stackTimingByDepth).toEqual([
{ start: [60], end: [91], stack: [0], length: 1 },
{ start: [60], end: [91], stack: [1], length: 1 },
{ start: [60], end: [91], stack: [4], length: 1 },
{ start: [70], end: [90], stack: [5], length: 1 },
{ start: [80], end: [90], stack: [6], length: 1 },
{ start: [80], end: [90], stack: [7], length: 1 },
{ start: [80], end: [90], stack: [8], length: 1 },
]);
});
/**
* The inverted stack indices will not match this chart, as new indices will be
* generated by the function that inverts the profile information.
*
* Uninverted -> Inverted
* 0-10-20-30-40-50-60-70-80-90-91 0-10-20-30-40-50-60-70-80-90-91 <- Timing (ms)
* ================================ ================================
* 0P 0P 0P 0P 0P 0P 0P 0P 0P 0P | 1P 2P 2P 3P 0P 1P 4J 5P 8J 4J
* 1P 1P 1P 1P 1P 1P 1P 1P 1P | 0P 1P 1P 1P 0P 1P 4P 7P 1P
* 2P 2P 3P 4J 4J 4J 4J | 0P 0P 0P 0P 1J 6P 0P
* 5J 5J | 0P 5J
* 6P | 4J
* 7P | 1P
* 8J | 0P
*/
it('can handle inverted stacks', function() {
const store = storeWithProfile();
store.dispatch(changeInvertCallstack(true));
const stackTimingByDepth = selectedThreadSelectors.getStackTimingByDepth(
store.getState()
);
expect(stackTimingByDepth).toEqual([
{
start: [0, 10, 30, 40, 50, 60, 70, 80, 90],
end: [10, 30, 40, 50, 60, 70, 80, 90, 91],
stack: [0, 2, 5, 8, 0, 9, 12, 16, 9],
length: 9,
},
{
start: [0, 10, 30, 50, 60, 70, 80, 90],
end: [10, 30, 40, 60, 70, 80, 90, 91],
stack: [1, 3, 6, 1, 10, 13, 17, 10],
length: 8,
},
{
start: [10, 30, 60, 70, 80, 90],
end: [30, 40, 70, 80, 90, 91],
stack: [4, 7, 11, 14, 18, 11],
length: 6,
},
{
start: [70, 80],
end: [80, 90],
stack: [15, 19],
length: 2,
},
{ start: [80], end: [90], stack: [20], length: 1 },
{ start: [80], end: [90], stack: [21], length: 1 },
{ start: [80], end: [90], stack: [22], length: 1 },
]);
});
});
describe('selectors/getFlameGraphTiming', function() {
/**
* Map the flameGraphTiming data structure into a human readable format where
* each line takes the form:
*
* "FunctionName1 (StartTime:EndTime) | FunctionName2 (StartTime:EndTime)"
*/
function getHumanReadableFlameGraphRanges(store, funcNames) {
const { callNodeTable } = selectedThreadSelectors.getCallNodeInfo(
store.getState()
);
const flameGraphTiming = selectedThreadSelectors.getFlameGraphTiming(
store.getState()
);
return flameGraphTiming.map(({ callNode, end, length, start }) => {
const lines = [];
for (let i = 0; i < length; i++) {
const callNodeIndex = callNode[i];
const funcIndex = callNodeTable.func[callNodeIndex];
const funcName = funcNames[funcIndex];
lines.push(
`${funcName} (${parseFloat(start[i].toFixed(2))}:${parseFloat(
end[i].toFixed(2)
)})`
);
}
return lines.join(' | ');
});
}
/**
* Map the flameGraphTiming data structure into a human readable format where
* each line takes the form:
*
* "FunctionName1 (TotalTime:SelfTime:SelfTimeRelative) | ..."
*/
function getHumanReadableFlameGraphTimings(store, funcNames) {
const { callNodeTable } = selectedThreadSelectors.getCallNodeInfo(
store.getState()
);
const flameGraphTiming = selectedThreadSelectors.getFlameGraphTiming(
store.getState()
);
return flameGraphTiming.map(
({ selfTimeRelative, display, callNode, length }) => {
const lines = [];
for (let i = 0; i < length; i++) {
const callNodeIndex = callNode[i];
const funcIndex = callNodeTable.func[callNodeIndex];
const funcName = funcNames[funcIndex];
const { totalTime, selfTime } = display[i];
lines.push(
`${funcName} (${totalTime}:${selfTime}:${selfTimeRelative[i]})`
);
}
return lines.join(' | ');
}
);
}
it('computes a basic example', function() {
const {
profile,
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
A A A
B B B
C C H
D F I
E G
`);
const store = storeWithProfile(profile);
expect(getHumanReadableFlameGraphRanges(store, funcNames)).toEqual([
'A (0:1)',
'B (0:1)',
'C (0:0.67) | H (0.67:1)',
'D (0:0.33) | F (0.33:0.67) | I (0.67:1)',
'E (0:0.33) | G (0.33:0.67)',
]);
});
it('can handle null samples', function() {
const {
profile,
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
A A X A
B B B
C C H
D F I
E G
`);
// Remove the X sample by setting it's stack to null.
profile.threads[0].samples.stack[2] = null;
const store = storeWithProfile(profile);
expect(getHumanReadableFlameGraphRanges(store, funcNames)).toEqual([
'A (0:1)',
'B (0:1)',
'C (0:0.67) | H (0.67:1)',
'D (0:0.33) | F (0.33:0.67) | I (0.67:1)',
'E (0:0.33) | G (0.33:0.67)',
]);
});
it('sorts stacks in alphabetical order', function() {
const {
profile,
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
D D A D
E F B F
C G
`);
const store = storeWithProfile(profile);
expect(getHumanReadableFlameGraphRanges(store, funcNames)).toEqual([
'A (0:0.25) | D (0.25:1)',
'B (0:0.25) | E (0.25:0.5) | F (0.5:1)',
'C (0:0.25) | G (0.5:0.75)',
]);
});
it('contains totalTime, selfTime and selfTimeRelative', function() {
const {
profile,
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
A A A A
B
C
`);
const store = storeWithProfile(profile);
expect(getHumanReadableFlameGraphTimings(store, funcNames)).toEqual([
'A (4:3:0.75)',
'B (1:—:0)',
'C (1:1:0.25)',
]);
});
});
describe('selectors/getCallNodeMaxDepthForFlameGraph', function() {
it('calculates the max call node depth', function() {
const { profile } = getProfileFromTextSamples(`
A A A
B B B
C C
D
`);
const store = storeWithProfile(profile);
const allSamplesMaxDepth = selectedThreadSelectors.getCallNodeMaxDepthForFlameGraph(
store.getState()
);
expect(allSamplesMaxDepth).toEqual(4);
});
it('returns zero if there are no samples', function() {
const { profile } = getProfileFromTextSamples(` `);
const store = storeWithProfile(profile);
const allSamplesMaxDepth = selectedThreadSelectors.getCallNodeMaxDepthForFlameGraph(
store.getState()
);
expect(allSamplesMaxDepth).toEqual(0);
});
});
describe('actions/changeImplementationFilter', function() {
const store = storeWithProfile();
it('is initially set to filter to all', function() {
const filter = UrlStateSelectors.getImplementationFilter(store.getState());
expect(filter).toEqual('combined');
});
it('can be changed to cpp', function() {
store.dispatch(changeImplementationFilter('cpp'));
const filter = UrlStateSelectors.getImplementationFilter(store.getState());
expect(filter).toEqual('cpp');
});
});
describe('actions/updateProfileSelection', function() {
it('can update the selection with new values', function() {
const store = storeWithProfile();
const initialSelection = ProfileViewSelectors.getProfileViewOptions(
store.getState()
).selection;
expect(initialSelection).toEqual({
hasSelection: false,
isModifying: false,
});
store.dispatch(
updateProfileSelection({
hasSelection: true,
isModifying: false,
selectionStart: 100,
selectionEnd: 200,
})
);
const secondSelection = ProfileViewSelectors.getProfileViewOptions(
store.getState()
).selection;
expect(secondSelection).toEqual({
hasSelection: true,
isModifying: false,
selectionStart: 100,
selectionEnd: 200,
});
});
});
describe('actions/changeInvertCallstack', function() {
// This profile has a heavily weighted path of A, B, I, J that should be selected.
const {
profile,
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
A A A A A
B E B B B
C F I I I
D G J J J
H
`);
const toFuncIndex = funcName => funcNames.indexOf(funcName);
const threadIndex = 0;
// The assumptions in this tests is that we are going between these two call node
// paths, one uninverted, the other inverted:
const callNodePath = ['A', 'B'].map(toFuncIndex);
const invertedCallNodePath = ['J', 'I', 'B'].map(toFuncIndex);
// Make tests more readable by grabbing the relevant paths, and transforming
// them to their function names, rather than indexes.
const getPaths = state => ({
selectedCallNodePath: selectedThreadSelectors
.getSelectedCallNodePath(state)
.map(index => funcNames[index]),
expandedCallNodePaths: Array.from(
selectedThreadSelectors.getExpandedCallNodePaths(state)
).map(path => path.map(index => funcNames[index])),
});
describe('on a normal call tree', function() {
// Each test uses a normal call tree, with a selected call node.
const storeWithNormalCallTree = () => {
const store = storeWithProfile(profile);
store.dispatch(changeSelectedCallNode(threadIndex, callNodePath));
return store;
};
it('starts with a selectedCallNodePath', function() {
const { getState } = storeWithNormalCallTree();
const { selectedCallNodePath, expandedCallNodePaths } = getPaths(
getState()
);
expect(selectedCallNodePath).toEqual(['A', 'B']);
expect(expandedCallNodePaths).toEqual([['A']]);
});
it('inverts the selectedCallNodePath', function() {
const { dispatch, getState } = storeWithProfile(profile);
dispatch(changeSelectedCallNode(threadIndex, callNodePath));
dispatch(changeInvertCallstack(true));
const { selectedCallNodePath, expandedCallNodePaths } = getPaths(
getState()
);
// Do not select the first alphabetical path:
expect(selectedCallNodePath).not.toEqual(['D', 'C', 'B']);
// Pick the heaviest path:
expect(selectedCallNodePath).toEqual(['J', 'I', 'B']);
expect(expandedCallNodePaths).toEqual([['J'], ['J', 'I']]);
});
});
describe('on an inverted call tree', function() {
// Each test uses a store with an inverted profile, and a selected call node.
const storeWithInvertedCallTree = () => {
const store = storeWithProfile(profile);
store.dispatch(changeInvertCallstack(true));
store.dispatch(changeSelectedCallNode(threadIndex, invertedCallNodePath));
return store;
};
it('starts with a selectedCallNodePath', function() {
const { getState } = storeWithInvertedCallTree();
const { selectedCallNodePath, expandedCallNodePaths } = getPaths(
getState()
);
expect(selectedCallNodePath).toEqual(['J', 'I', 'B']);
expect(expandedCallNodePaths).toEqual([['J'], ['J', 'I']]);
});
it('uninverts the selectedCallNodePath', function() {
const { dispatch, getState } = storeWithInvertedCallTree();
dispatch(changeInvertCallstack(false));
const { selectedCallNodePath, expandedCallNodePaths } = getPaths(
getState()
);
expect(selectedCallNodePath).toEqual(['A', 'B']);
expect(expandedCallNodePaths).toEqual([['A']]);
});
});
});