-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathline-timings.test.ts
More file actions
326 lines (289 loc) · 12.6 KB
/
line-timings.test.ts
File metadata and controls
326 lines (289 loc) · 12.6 KB
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
/* 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/. */
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import {
getStackLineInfo,
getLineTimings,
getTotalLineTimingsForCallNode,
} from 'firefox-profiler/profile-logic/line-timings';
import {
getCallNodeFramePerStack,
getCallNodeInfo,
getInvertedCallNodeInfo,
} from '../../profile-logic/profile-data';
import { ensureExists } from 'firefox-profiler/utils/types';
import type {
CallNodePath,
Thread,
IndexIntoCategoryList,
LineNumber,
} from 'firefox-profiler/types';
describe('getStackLineInfo', function () {
it('computes results for all stacks', function () {
const { derivedThreads } = getProfileFromTextSamples(`
A[file:one.js][line:20] A[file:one.js][line:21] A[file:one.js][line:20]
B[file:one.js][line:30] B[file:one.js][line:30] B[file:one.js][line:30]
C[file:two.js][line:10] C[file:two.js][line:11] D[file:two.js][line:40]
B[file:one.js][line:30] D[file:two.js][line:40]
`);
const [thread] = derivedThreads;
const { stackTable, frameTable, funcTable, stringTable } = thread;
const fileOneStringIndex = stringTable.indexForString('one.js');
const fileOneSourceIndex =
thread.sources.filename.indexOf(fileOneStringIndex);
const stackLineInfoOne = getStackLineInfo(
stackTable,
frameTable,
funcTable,
fileOneSourceIndex
);
// Expect the returned stackIndexToLineSetIndex array to have the same length as the stackTable.
expect(stackTable.length).toBe(9);
expect(stackLineInfoOne.stackIndexToLineSetIndex.length).toBe(9);
expect(stackLineInfoOne.lineSetTable.length).toBe(7);
});
});
describe('getLineTimings for getStackLineInfo', function () {
function getTimings(thread: Thread, file: string) {
const { stackTable, frameTable, funcTable, samples, stringTable } = thread;
const fileStringIndex = stringTable.indexForString(file);
const fileSourceIndex = thread.sources.filename.indexOf(fileStringIndex);
const stackLineInfo = getStackLineInfo(
stackTable,
frameTable,
funcTable,
fileSourceIndex
);
return getLineTimings(stackLineInfo, samples);
}
it('passes a basic test', function () {
// In this example, there's one self line hit in line 30.
// Both line 20 and line 30 have one total time hit.
const { derivedThreads } = getProfileFromTextSamples(`
A[file:file.js][line:20]
B[file:file.js][line:30]
`);
const [thread] = derivedThreads;
const lineTimings = getTimings(thread, 'file.js');
expect(lineTimings.totalLineHits.get(20)).toBe(1);
expect(lineTimings.totalLineHits.get(30)).toBe(1);
expect(lineTimings.totalLineHits.size).toBe(2); // no other hits
expect(lineTimings.selfLineHits.get(20)).toBe(undefined);
expect(lineTimings.selfLineHits.get(30)).toBe(1);
expect(lineTimings.selfLineHits.size).toBe(1); // no other hits
});
it('passes a test with two files and recursion', function () {
const { derivedThreads } = getProfileFromTextSamples(`
A[file:one.js][line:20] A[file:one.js][line:21] A[file:one.js][line:20]
B[file:one.js][line:30] B[file:one.js][line:30] B[file:one.js][line:30]
C[file:two.js][line:10] C[file:two.js][line:11] D[file:two.js][line:40]
B[file:one.js][line:30] D[file:two.js][line:40]
`);
const [thread] = derivedThreads;
const lineTimingsOne = getTimings(thread, 'one.js');
expect(lineTimingsOne.totalLineHits.get(20)).toBe(2);
expect(lineTimingsOne.totalLineHits.get(21)).toBe(1);
// one.js line 30 was hit in every sample, twice in the first sample
// (due to recursion) but that still only counts as one sample
expect(lineTimingsOne.totalLineHits.get(30)).toBe(3);
expect(lineTimingsOne.totalLineHits.size).toBe(3); // no other hits
// The only self line hit in one.js is in the first sample.
// The other two samples have tehir self line hit in two.js.
expect(lineTimingsOne.selfLineHits.get(20)).toBe(undefined);
expect(lineTimingsOne.selfLineHits.get(21)).toBe(undefined);
expect(lineTimingsOne.selfLineHits.get(30)).toBe(1);
expect(lineTimingsOne.selfLineHits.size).toBe(1); // no other hits
const lineTimingsTwo = getTimings(thread, 'two.js');
expect(lineTimingsTwo.totalLineHits.get(10)).toBe(1);
expect(lineTimingsTwo.totalLineHits.get(11)).toBe(1);
expect(lineTimingsTwo.totalLineHits.get(40)).toBe(1);
expect(lineTimingsTwo.totalLineHits.size).toBe(3); // no other hits
expect(lineTimingsTwo.selfLineHits.get(10)).toBe(undefined);
expect(lineTimingsTwo.selfLineHits.get(11)).toBe(1);
// two.js line 40 recursed but should only be counted as 1 sample
expect(lineTimingsTwo.selfLineHits.get(40)).toBe(1);
expect(lineTimingsTwo.selfLineHits.size).toBe(2); // no other hits
});
it('falls back to funcTable.lineNumber when frameTable.line is null', function () {
// Create a profile with frames that have null line numbers
const { derivedThreads } = getProfileFromTextSamples(`
A[file:file.js][line:20]
B[file:file.js][line:30]
`);
const [thread] = derivedThreads;
const { stackTable, frameTable, funcTable, samples, stringTable } = thread;
// Manually set frameTable.line to null for the leaf frame
// to simulate a case where frame line info is missing
const leafFrame = stackTable.frame[stackTable.length - 1];
frameTable.line[leafFrame] = null;
// Set funcTable.lineNumber to a value for the func of that frame
const func = frameTable.func[leafFrame];
funcTable.lineNumber[func] = 35;
const fileStringIndex = stringTable.indexForString('file.js');
const fileSourceIndex = thread.sources.filename.indexOf(fileStringIndex);
const stackLineInfo = getStackLineInfo(
stackTable,
frameTable,
funcTable,
fileSourceIndex
);
const lineTimings = getLineTimings(stackLineInfo, samples);
// The fallback should use funcTable.lineNumber[func] = 35
expect(lineTimings.selfLineHits.get(35)).toBe(1);
expect(lineTimings.totalLineHits.get(20)).toBe(1);
expect(lineTimings.totalLineHits.get(35)).toBe(1);
});
});
describe('getTotalLineTimingsForCallNode', function () {
function getTimings(
thread: Thread,
callNodePath: CallNodePath,
defaultCategory: IndexIntoCategoryList,
isInverted: boolean
): Map<LineNumber, number> {
const { stackTable, frameTable, funcTable, samples } = thread;
const nonInvertedCallNodeInfo = getCallNodeInfo(
stackTable,
frameTable,
defaultCategory
);
const callNodeInfo = isInverted
? getInvertedCallNodeInfo(
nonInvertedCallNodeInfo,
defaultCategory,
funcTable.length
)
: nonInvertedCallNodeInfo;
const callNodeIndex = ensureExists(
callNodeInfo.getCallNodeIndexFromPath(callNodePath),
'invalid call node path'
);
const callNodeFramePerStack = getCallNodeFramePerStack(
callNodeIndex,
callNodeInfo,
stackTable
);
const funcLine =
funcTable.lineNumber[callNodeInfo.funcForNode(callNodeIndex)];
return getTotalLineTimingsForCallNode(
samples,
callNodeFramePerStack,
frameTable,
funcLine
);
}
it('passes a basic test', function () {
const { derivedThreads, funcNamesDictPerThread, defaultCategory } =
getProfileFromTextSamples(`
A[file:file.js][line:20]
B[file:file.js][line:30]
`);
const [{ A, B }] = funcNamesDictPerThread;
const [thread] = derivedThreads;
// Compute the line timings for the root call node.
// One total line hit in line 20.
const lineTimingsRoot = getTimings(thread, [A], defaultCategory, false);
expect(lineTimingsRoot.get(20)).toBe(1);
expect(lineTimingsRoot.size).toBe(1); // no other hits
// Compute the line timings for the child call node.
// One total line hit in line 30.
const lineTimingsChild = getTimings(thread, [A, B], defaultCategory, false);
expect(lineTimingsChild.get(30)).toBe(1);
expect(lineTimingsChild.size).toBe(1); // no other hits
});
it('passes a basic test with recursion', function () {
const { derivedThreads, funcNamesDictPerThread, defaultCategory } =
getProfileFromTextSamples(`
A[file:file.js][line:20]
B[file:file.js][line:30]
A[file:file.js][line:21]
`);
const [{ A, B }] = funcNamesDictPerThread;
const [thread] = derivedThreads;
// Compute the line timings for the root call node.
// One total line hit in line 20.
const lineTimingsRoot = getTimings(thread, [A], defaultCategory, false);
expect(lineTimingsRoot.get(20)).toBe(1);
expect(lineTimingsRoot.size).toBe(1); // no other hits
// Compute the line timings for the leaf call node.
// One total line hit in line 21.
// In particular, we shouldn't record a hit for line 20, even though
// the hit at line 20 is also in A. But it's in the wrong call node.
const lineTimingsChild = getTimings(
thread,
[A, B, A],
defaultCategory,
false
);
expect(lineTimingsChild.get(21)).toBe(1);
expect(lineTimingsChild.size).toBe(1); // no other hits
});
it('passes a test where the same function is called via different call paths', function () {
const { derivedThreads, funcNamesDictPerThread, defaultCategory } =
getProfileFromTextSamples(`
A[file:one.js][line:20] A[file:one.js][line:21] A[file:one.js][line:20]
B[file:one.js][line:30] D[file:one.js][line:50] B[file:one.js][line:31]
C[file:two.js][line:10] C[file:two.js][line:11] C[file:two.js][line:12]
D[file:one.js][line:51]
`);
const [{ A, B, C }] = funcNamesDictPerThread;
const [thread] = derivedThreads;
const lineTimingsABC = getTimings(
thread,
[A, B, C],
defaultCategory,
false
);
expect(lineTimingsABC.get(10)).toBe(1);
expect(lineTimingsABC.get(12)).toBe(1);
expect(lineTimingsABC.size).toBe(2); // no other hits
});
it('passes a test with an inverted thread', function () {
const { derivedThreads, funcNamesDictPerThread, defaultCategory } =
getProfileFromTextSamples(`
A[file:one.js][line:20] A[file:one.js][line:21] A[file:one.js][line:20]
B[file:one.js][line:30] D[file:one.js][line:50] B[file:one.js][line:31]
D[file:one.js][line:51] D[file:one.js][line:52] C[file:two.js][line:12]
D[file:one.js][line:51]
`);
const [{ A, B, C, D }] = funcNamesDictPerThread;
const [thread] = derivedThreads;
// For the root D of the inverted tree, we have 3 total line hits.
const lineTimingsD = getTimings(thread, [D], defaultCategory, true);
expect(lineTimingsD.get(51)).toBe(2);
expect(lineTimingsD.get(52)).toBe(1);
expect(lineTimingsD.size).toBe(2); // no other hits
// For the C call node which is a child (direct caller) of D, we have
// one hit in line 12.
const lineTimingsDC = getTimings(thread, [D, C], defaultCategory, true);
expect(lineTimingsDC.get(12)).toBe(1);
expect(lineTimingsDC.size).toBe(1); // no other hits
// For the D <- B <- A call node, we have one total hit in line 20.
const lineTimingsDBA = getTimings(thread, [D, B, A], defaultCategory, true);
expect(lineTimingsDBA.get(20)).toBe(1);
expect(lineTimingsDBA.size).toBe(1); // no other hits
});
it('falls back to funcTable.lineNumber when frameTable.line is null', function () {
const { derivedThreads, funcNamesDictPerThread, defaultCategory } =
getProfileFromTextSamples(`
A[file:file.js][line:20]
B[file:file.js][line:30]
`);
const [{ A, B }] = funcNamesDictPerThread;
const [thread] = derivedThreads;
const { stackTable, frameTable, funcTable } = thread;
// Manually set frameTable.line to null for the leaf frame
// to simulate a case where frame line info is missing
const leafFrame = stackTable.frame[stackTable.length - 1];
frameTable.line[leafFrame] = null;
// Set funcTable.lineNumber to a value for the func of that frame
const func = frameTable.func[leafFrame];
funcTable.lineNumber[func] = 35;
// Compute the line timings for the child call node.
// The fallback should use funcTable.lineNumber[func] = 35
const lineTimingsChild = getTimings(thread, [A, B], defaultCategory, false);
expect(lineTimingsChild.get(35)).toBe(1);
expect(lineTimingsChild.size).toBe(1); // no other hits
});
});