forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasure.ts
49 lines (42 loc) · 1.52 KB
/
measure.ts
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
import {profiler} from '@perf-profiler/profiler';
import {getAverageCpuUsage, getAverageCpuUsagePerProcess, getAverageFPSUsage, getAverageRAMUsage} from '@perf-profiler/reporter';
import {ThreadNames} from '@perf-profiler/types';
import type {Measure} from '@perf-profiler/types';
let measures: Measure[] = [];
const POLLING_STOPPED = {
stop: (): void => {
throw new Error('Cannot stop polling on a stopped profiler');
},
};
let polling = POLLING_STOPPED;
const start = (bundleId: string, {onAttachFailed}: {onAttachFailed: () => Promise<void>}) => {
// clear our measurements results
measures = [];
polling = profiler.pollPerformanceMeasures(bundleId, {
onMeasure: (measure: Measure) => {
measures.push(measure);
},
onPidChanged: () => {
onAttachFailed();
},
});
};
const stop = () => {
polling.stop();
polling = POLLING_STOPPED;
const average = getAverageCpuUsagePerProcess(measures);
const uiThread = average.find(({processName}) => processName === ThreadNames.ANDROID.UI)?.cpuUsage;
// most likely this line needs to be updated when we migrate to RN 0.74 with bridgeless mode
const jsThread = average.find(({processName}) => processName === ThreadNames.RN.JS_ANDROID)?.cpuUsage;
const cpu = getAverageCpuUsage(measures);
const fps = getAverageFPSUsage(measures);
const ram = getAverageRAMUsage(measures);
return {
uiThread,
jsThread,
cpu,
fps,
ram,
};
};
export {start, stop};