-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathmeasurePerformance.test.ts
More file actions
163 lines (144 loc) 路 4 KB
/
Copy pathmeasurePerformance.test.ts
File metadata and controls
163 lines (144 loc) 路 4 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
import os from "os";
import fs from "fs";
import { measurePerformance } from "..";
import { PerformancePollingMock } from "../utils/test/PerformancePollingMock";
import { Logger, LogLevel } from "@perf-profiler/logger";
const mockPerformancePolling = new PerformancePollingMock();
jest.mock("@perf-profiler/profiler", () => {
const mockedProfiler = jest.requireActual("@perf-profiler/profiler").profiler;
mockedProfiler.installProfilerOnDevice = jest.fn();
mockedProfiler.getPidId = jest.fn(() => 123);
mockedProfiler.pollPerformanceMeasures = jest.fn((pid, { onMeasure, onStartMeasuring }) => {
mockPerformancePolling.setCallback(onMeasure);
onStartMeasuring();
});
mockedProfiler.detectDeviceRefreshRate = jest.fn(() => 120);
return {
...jest.requireActual("@perf-profiler/profiler"),
profiler: mockedProfiler,
};
});
Logger.setLogLevel(LogLevel.SILENT);
jest.setTimeout(10000);
// Mock test time to be always 1000ms
jest.mock("perf_hooks", () => {
let isStart = false;
return {
performance: {
now: () => {
isStart = !isStart;
return isStart ? 0 : 1000;
},
},
};
});
const runTest = jest.fn();
describe("measurePerformance", () => {
it("adds a score if a getScore function is passed", async () => {
const PATH = `${os.tmpdir()}/results.json`;
const TITLE = "TITLE";
const { writeResults } = await measurePerformance(
"com.example",
{
run: runTest,
getScore: (result) => result.iterations.length,
},
{
iterationCount: 3,
maxRetries: 3,
recordOptions: { record: false },
resultsFileOptions: {
path: PATH,
title: TITLE,
},
}
);
expect(runTest).toHaveBeenCalledTimes(3);
writeResults();
expect(JSON.parse(fs.readFileSync(PATH).toString())).toMatchInlineSnapshot(`
{
"iterations": [
{
"measures": [],
"startTime": 0,
"status": "SUCCESS",
"time": 1000,
},
{
"measures": [],
"startTime": 0,
"status": "SUCCESS",
"time": 1000,
},
{
"measures": [],
"startTime": 0,
"status": "SUCCESS",
"time": 1000,
},
],
"name": "TITLE",
"score": 3,
"specs": {
"refreshRate": 120,
},
"status": "SUCCESS",
}
`);
});
it("waits for a certain duration", async () => {
const DURATION = 1500;
const interval = setInterval(() => mockPerformancePolling.emit({}), 10);
const { measures } = await measurePerformance(
"com.example",
{ run: runTest, duration: DURATION },
{ iterationCount: 1 }
);
// DURATION is 1500
// So wait to have points 0 / 500 / 1000 and 1500 so 4 measures
expect(measures[0].measures.length).toEqual(4);
clearInterval(interval);
});
it("retries tests if they fail", async () => {
const mockFailingTest = (failureCount: number) => {
for (let i = 0; i < failureCount; i++) {
runTest.mockImplementationOnce(async () => {
throw new Error("Failure");
});
}
};
const MAX_RETRIES = 2;
mockFailingTest(2);
await measurePerformance(
"com.example",
{ run: runTest },
{
iterationCount: 3,
maxRetries: MAX_RETRIES,
}
);
mockFailingTest(3);
await expect(
measurePerformance(
"com.example",
{ run: runTest },
{
iterationCount: 3,
maxRetries: MAX_RETRIES,
}
)
).rejects.toThrowError("Max number of retries reached.");
});
it("throws an error if no measures are returned", async () => {
runTest.mockImplementationOnce(async () => Promise.resolve());
await expect(
measurePerformance(
"com.example",
{ run: runTest },
{
iterationCount: 0,
}
)
).rejects.toThrowError("No measure returned");
});
});