1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Timers = exports.Timer = void 0;
|
4 |
|
5 |
|
6 |
|
7 | class Timer {
|
8 | constructor(label) {
|
9 | this.label = label;
|
10 | this.startTime = Date.now();
|
11 | }
|
12 | start() {
|
13 | this.startTime = Date.now();
|
14 | }
|
15 | end() {
|
16 | this.timeMs = (Date.now() - this.startTime) / 1000;
|
17 | }
|
18 | isSet() {
|
19 | return this.timeMs !== undefined;
|
20 | }
|
21 | humanTime() {
|
22 | if (!this.timeMs) {
|
23 | return '???';
|
24 | }
|
25 | const parts = [];
|
26 | let time = this.timeMs;
|
27 | if (time > 60) {
|
28 | const mins = Math.floor(time / 60);
|
29 | parts.push(`${mins}m`);
|
30 | time -= mins * 60;
|
31 | }
|
32 | parts.push(`${time.toFixed(1)}s`);
|
33 | return parts.join('');
|
34 | }
|
35 | }
|
36 | exports.Timer = Timer;
|
37 |
|
38 |
|
39 |
|
40 | class Timers {
|
41 | constructor() {
|
42 | this.timers = [];
|
43 | }
|
44 | record(label, operation) {
|
45 | const timer = this.start(label);
|
46 | try {
|
47 | const x = operation();
|
48 | timer.end();
|
49 | return x;
|
50 | }
|
51 | catch (e) {
|
52 | timer.end();
|
53 | throw e;
|
54 | }
|
55 | }
|
56 | async recordAsync(label, operation) {
|
57 | const timer = this.start(label);
|
58 | return operation().finally(() => timer.end());
|
59 | }
|
60 | start(label) {
|
61 | const timer = new Timer(label);
|
62 | this.timers.push(timer);
|
63 | return timer;
|
64 | }
|
65 | display() {
|
66 | const timers = this.timers.filter((t) => t.isSet());
|
67 | timers.sort((a, b) => b.timeMs - a.timeMs);
|
68 | return timers.map((t) => `${t.label} (${t.humanTime()})`).join(' | ');
|
69 | }
|
70 | }
|
71 | exports.Timers = Timers;
|
72 |
|
\ | No newline at end of file |