UNPKG

1.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var now = function () {
4 return typeof performance !== 'undefined' && !!performance.now ? performance.now() : Date.now();
5};
6var RESET_INTERVAL = 3 * 60 * 1000; // auto reset every 3 minutes
7/**
8 * Performance helper class for measuring things.
9 *
10 * @public
11 * {@docCategory FabricPerformance}
12 */
13var FabricPerformance = /** @class */ (function () {
14 function FabricPerformance() {
15 }
16 /**
17 * Measures execution time of the given syncronous function. If the same logic is executed multiple times,
18 * each individual measurement will be collected as well the overall numbers.
19 * @param name - The name of this measurement
20 * @param func - The logic to be measured for execution time
21 */
22 FabricPerformance.measure = function (name, func) {
23 if (FabricPerformance._timeoutId) {
24 FabricPerformance.setPeriodicReset();
25 }
26 var start = now();
27 func();
28 var end = now();
29 var measurement = FabricPerformance.summary[name] || {
30 totalDuration: 0,
31 count: 0,
32 all: [],
33 };
34 var duration = end - start;
35 measurement.totalDuration += duration;
36 measurement.count++;
37 measurement.all.push({
38 duration: duration,
39 timeStamp: end,
40 });
41 FabricPerformance.summary[name] = measurement;
42 };
43 FabricPerformance.reset = function () {
44 FabricPerformance.summary = {};
45 clearTimeout(FabricPerformance._timeoutId);
46 FabricPerformance._timeoutId = NaN;
47 };
48 FabricPerformance.setPeriodicReset = function () {
49 FabricPerformance._timeoutId = setTimeout(function () { return FabricPerformance.reset(); }, RESET_INTERVAL);
50 };
51 FabricPerformance.summary = {};
52 return FabricPerformance;
53}());
54exports.FabricPerformance = FabricPerformance;
55//# sourceMappingURL=FabricPerformance.js.map
\No newline at end of file