-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathperf.ts
More file actions
147 lines (127 loc) · 3.87 KB
/
Copy pathperf.ts
File metadata and controls
147 lines (127 loc) · 3.87 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
import type { PerfEvent } from 'edge-login-ui-rn'
import performance, {
type PerformanceEntry,
PerformanceObserver
} from 'react-native-performance'
//
// Logging
//
/**
* Log the 'measure' performance entries observed throughout the app.
*
* This is a single logging location for all performance measurements, so that
* way the app can just use the performance API and not worry about logging, and
* the destination for these logs can be trivially changed and managed for all
* performance related logging needs.
*/
new PerformanceObserver(list => {
const entries = list.getEntries()
const measureEntries = entries.filter(entry => entry.entryType === 'measure')
// Log the measure entries:
measureEntries.forEach(entry => {
const detail = 'detail' in entry && entry.detail != null ? entry.detail : ''
console.log(`PERF:`, entry.name, `${entry.duration}ms`, detail)
})
// Clear measure entries:
const names = measureEntries.map(entries => entries.name)
for (const name of names) {
performance.clearMeasures(name)
}
}).observe({ entryTypes: ['measure'] })
//
// Native Measurements
//
// Launch:
new PerformanceObserver(list => {
const entries = list.getEntries()
const hasLaunchFinished = entries.some(
entry => entry.name === 'nativeLaunchEnd'
)
const hasBundleFinished = entries.some(
entry => entry.name === 'runJsBundleEnd'
)
if (hasLaunchFinished && hasBundleFinished) {
performance.measure('nativeLaunch', 'nativeLaunchStart', 'nativeLaunchEnd')
performance.measure('runJsBundle', 'runJsBundleStart', 'runJsBundleEnd')
performance.measure('fullLaunch', 'nativeLaunchStart', 'runJsBundleEnd')
// Clear mark entries:
performance.clearMarks('nativeLaunchStart')
performance.clearMarks('nativeLaunchEnd')
performance.clearMarks('runJsBundleStart')
performance.clearMarks('runJsBundleEnd')
}
}).observe({ type: 'react-native-mark', buffered: true })
//
// App Tracking Measurements
//
const measurements = [
// GUI Login (end-to-end):
markerMeasurement('loginFull', 'loginBegin', 'loginEnd'),
// Pin Login:
markerMeasurement('pinLogin', 'pinLoginBegin', 'pinLoginEnd'),
// Password Login:
markerMeasurement('passwordLogin', 'passwordLoginBegin', 'passwordLoginEnd')
]
// Observe for all markers and measure them:
new PerformanceObserver(list => {
const entries = list.getEntries()
measurements.forEach(fn => {
fn(entries)
})
}).observe({ type: 'mark', buffered: true })
//
// Login UI Performance Markers
//
export function performanceMarkersFromLoginUiPerfEvents(
event: PerfEvent
): void {
switch (event.name) {
case 'passwordLoginBegin':
performance.mark('loginBegin')
performance.mark('passwordLoginBegin')
break
case 'passwordLoginEnd':
performance.mark('passwordLoginEnd', {
detail: { isSuccessful: event.error == null }
})
break
case 'pinLoginBegin':
performance.mark('loginBegin')
performance.mark('pinLoginBegin')
break
case 'pinLoginEnd':
performance.mark('pinLoginEnd', {
detail: { isSuccessful: event.error == null }
})
break
default:
// Do nothing
}
}
//
// Utilities
//
function markerMeasurement(
measureName: string,
startName: string,
endName: string
) {
return (entries: PerformanceEntry[]) => {
const endMark = entries.find(entry => entry.name === endName)
if (endMark != null) {
const endMarkDetail = 'detail' in endMark ? endMark.detail : {}
const [startMark] = performance.getEntriesByName(startName)
// Measure the start and end markers if they exist:
if (startMark != null) {
performance.measure(measureName, {
start: startName,
end: endName,
detail: endMarkDetail
})
}
// Clear the markers:
performance.clearMarks(startName)
performance.clearMarks(endName)
}
}
}