forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.ts
More file actions
72 lines (63 loc) · 2.32 KB
/
benchmark.ts
File metadata and controls
72 lines (63 loc) · 2.32 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
import _debug from '@codesandbox/common/lib/utils/debug';
import Manager from '../eval/manager';
import {
clearMeasurements,
measure,
endMeasure,
getMeasurements,
getCumulativeMeasure,
} from './metrics';
const debug = _debug('cs:compiler:benchmarks');
interface TranspilationTimes {
total: number;
transpile: number;
resolving: number;
esConverting: number;
}
const printTranspilationMeasurements = (results: TranspilationTimes) => {
debug(`Total ${results.total.toFixed(2)}ms`);
debug(` Transpiling ${results.transpile.toFixed(2)}ms`);
debug(` Resolving ${results.resolving.toFixed(2)}ms`);
debug(` Converting ES ${results.esConverting.toFixed(2)}ms`);
};
export function generateBenchmarkInterface(manager: Manager) {
return {
transpilation: async (n = 10, path = '/src/index.js') => {
const module = manager.resolveModule(path, '/');
const times: TranspilationTimes[] = [];
for (let i = 0; i < n; i++) {
manager.clearTranspilationCache();
manager.cachedPaths = {};
clearMeasurements();
measure('transpilation');
// eslint-disable-next-line
await manager.transpileModules(module);
const total = endMeasure('transpilation', { silent: true });
times.push({
total,
resolving: getCumulativeMeasure('resolve', { silent: true }),
esConverting: getCumulativeMeasure('esconvert', { silent: true }),
transpile: getCumulativeMeasure('transpile', { silent: true }),
});
}
const averageResults = times.reduce(
(result, entry) => ({
total: result.total + entry.total / n,
esConverting: result.esConverting + entry.esConverting / n,
resolving: result.resolving + entry.resolving / n,
transpile: result.transpile + entry.transpile / n,
}),
{ total: 0, esConverting: 0, resolving: 0, transpile: 0 }
);
printTranspilationMeasurements(averageResults);
},
getLastTranspilationMeasurements() {
printTranspilationMeasurements({
total: getMeasurements().transpilation,
resolving: getCumulativeMeasure('resolve', { silent: true }),
esConverting: getCumulativeMeasure('esconvert', { silent: true }),
transpile: getCumulativeMeasure('transpile', { silent: true }),
});
},
};
}