forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile-conversion.test.js
227 lines (201 loc) · 7.65 KB
/
profile-conversion.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
/* 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 { unserializeProfileOfArbitraryFormat } from '../../profile-logic/process-profile';
import { GECKO_PROFILE_VERSION } from '../../app-logic/constants';
import type {
TracingEventUnion,
CpuProfileEvent,
} from '../../profile-logic/import/chrome';
import type { Profile } from 'firefox-profiler/types';
import { TextDecoder } from 'util';
beforeAll(function () {
if ((window: any).TextDecoder) {
throw new Error('A TextDecoder was already on the window object.');
}
(window: any).TextDecoder = TextDecoder;
});
afterAll(async function () {
delete (window: any).TextDecoder;
});
function checkProfileContainsUniqueTid(profile: Profile) {
const foundTids = new Set();
for (const thread of profile.threads) {
const { tid } = thread;
if (tid === undefined) {
throw new Error('Found an undefined tid!');
}
if (foundTids.has(tid)) {
console.error(`Found a duplicate tid ${tid}!`);
}
foundTids.add(tid);
}
}
describe('converting Linux perf profile', function () {
async function loadProfile(filename: string): Promise<Profile> {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync(filename);
const decompressedArrayBuffer = zlib.gunzipSync(buffer);
const text = decompressedArrayBuffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
return profile;
}
it('should import a perf profile', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/test.perf.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('should import a simple perf profile', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/simple-perf.txt.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('should import a perf profile of gzip', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/gzip.perf.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('should import a perf profile of graphviz with a header', async function () {
const profile = await loadProfile(
'src/test/fixtures/upgrades/graphviz.perf.gz'
);
expect(profile.meta.version).toEqual(GECKO_PROFILE_VERSION);
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});
describe('converting dhat profiles', function () {
it('should import a dhat profile', async function () {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync('src/test/fixtures/upgrades/dhat.json.gz');
const decompressedArrayBuffer = zlib.gunzipSync(buffer);
const text = decompressedArrayBuffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});
describe('converting Google Chrome profile', function () {
it('successfully imports a chunked profile (one that uses Profile + ProfileChunk trace events)', async function () {
// Mock out image loading behavior as the screenshots rely on the Image loading
// behavior.
jest
.spyOn(Image.prototype, 'addEventListener')
.mockImplementation((name: string, callback) => {
if (name === 'load') {
callback();
}
});
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync('src/test/fixtures/upgrades/test.chrome.gz');
const decompressedArrayBuffer = zlib.gunzipSync(buffer);
const text = decompressedArrayBuffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a non-chunked profile (one that uses a CpuProfile trace event)', async function () {
// As in the previous test, mock out image loading behavior as the screenshots
// rely on the Image loading behavior.
jest
.spyOn(Image.prototype, 'addEventListener')
.mockImplementation((name: string, callback) => {
if (name === 'load') {
callback();
}
});
const fs = require('fs');
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/test.chrome-unchunked.json'
);
const text = buffer.toString('utf8');
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a single CpuProfile, e.g. from node', async function () {
const fs = require('fs');
let cpuProfile: CpuProfileEvent | void;
{
// Load the existing saved profile, but then find a single CpuProfile entry in it.
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/test.chrome-unchunked.json'
);
const events: TracingEventUnion[] = JSON.parse(buffer.toString('utf8'));
// Use a for loop to look this up, as Flow is happier than a .find().
for (const event of events) {
if (event.name === 'CpuProfile') {
cpuProfile = event;
break;
}
}
if (!cpuProfile) {
throw new Error('Could not find a CPU profile');
}
}
// Now use the single CpuProfile to test that we can convert a node-like export
// format.
const text = JSON.stringify(cpuProfile.args.data.cpuProfile);
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});
describe('converting ART trace', function () {
it('successfully imports a non-streaming ART trace', async function () {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/art-trace-regular.trace.gz'
);
const arrayBuffer = zlib.gunzipSync(buffer).buffer;
const profile = await unserializeProfileOfArbitraryFormat(arrayBuffer);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
it('successfully imports a streaming ART trace', async function () {
const fs = require('fs');
const zlib = require('zlib');
const buffer = fs.readFileSync(
'src/test/fixtures/upgrades/art-trace-streaming.trace.gz'
);
const arrayBuffer = zlib.gunzipSync(buffer).buffer;
const profile = await unserializeProfileOfArbitraryFormat(arrayBuffer);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
checkProfileContainsUniqueTid(profile);
expect(profile).toMatchSnapshot();
});
});