forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.ts
More file actions
153 lines (130 loc) · 3.8 KB
/
metrics.ts
File metadata and controls
153 lines (130 loc) · 3.8 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
import _debug from '@codesandbox/common/lib/utils/debug';
import { getGlobal } from '@codesandbox/common/lib/utils/global';
const debug = _debug('cs:compiler:measurements');
type MeasurementKey = string;
const runningMeasurements = new Map<string, number>();
let measurements: { [measurement: string]: number } = {};
const global = getGlobal();
if (typeof global.performance === 'undefined') {
global.performance = {
mark: () => {},
now: () => Date.now(),
measure: () => {},
};
}
export function measure(key: MeasurementKey) {
try {
performance.mark(`${key}_start`);
runningMeasurements.set(key, performance.now());
} catch (e) {
console.warn(`Something went wrong while adding measure: ${e.message}`);
}
}
export function endMeasure(
key: MeasurementKey,
options: {
displayName?: string;
lastTime?: number;
silent?: boolean;
} = {}
) {
try {
const { lastTime } = options;
performance.mark(`${key}_end`);
const lastMeasurement =
typeof lastTime === 'undefined' ? runningMeasurements.get(key) : lastTime;
if (typeof lastMeasurement === 'undefined') {
console.warn(
`Measurement for '${key}' was requested, but never was started`
);
return 0;
}
const nowMeasurement = performance.now();
measurements[key] = nowMeasurement - lastMeasurement;
if (!options.silent) {
debug(
`${options.displayName || key} Time: ${measurements[key].toFixed(2)}ms`
);
}
const hadKey = runningMeasurements.delete(key);
performance.measure(key, hadKey ? `${key}_start` : undefined, `${key}_end`);
return measurements[key];
} catch (e) {
console.warn(`Something went wrong while adding measure: ${e.message}`);
return 0;
}
}
/**
* Get the cumulative of a specific measurement by prefix. If you had for example these measurements:
* - transpile-index.js
* - transpile-test.js
*
* You can get the sum of these measurements with getCumulativeMeasure('transpile', 'Transpilation')
*/
export function getCumulativeMeasure(
prefix: string,
options: { displayName?: string; silent?: boolean } = {}
) {
const keys = Object.keys(measurements).filter(p =>
p.startsWith(prefix + '-')
);
const totalTime = keys.reduce((prev, key) => prev + measurements[key], 0);
if (!options.silent) {
debug(
`${options.displayName || prefix} Total Time: ${totalTime.toFixed(2)}ms`
);
debug(` Average Time: ${(totalTime / keys.length).toFixed(2)}ms`);
}
return totalTime;
}
export function clearMeasurements() {
measurements = {};
runningMeasurements.clear();
}
export function getMeasurements() {
return measurements;
}
getGlobal().measurements = {
getCumulativeMeasure,
getMeasurements,
};
const MEASUREMENT_API = `https://col.csbops.io/data/sandpack`;
export function persistMeasurements(data: {
sandboxId: string;
cacheUsed: boolean;
browser: string;
version: string;
}): Promise<Response | void> {
const body = [
{
measurement: 'load_times',
tags: {
browser: data.browser,
cache_used: data.cacheUsed,
version: data.version,
},
fields: {
transpilation: measurements.transpilation,
evaluation: measurements.evaluation,
external_resources: measurements['external-resources'],
compilation: measurements.compilation,
boot: measurements.boot,
total: measurements.total,
dependencies: measurements.dependencies,
},
},
];
if (process.env.NODE_ENV === 'development' || process.env.STAGING) {
// eslint-disable-next-line
console.log(body);
return Promise.resolve();
}
return fetch(MEASUREMENT_API, {
method: 'POST',
body: JSON.stringify(body),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
}