@universal-packages/time-measurer 1.6.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Benchmark.d.ts +24 -0
- package/Benchmark.d.ts.map +1 -0
- package/Benchmark.js +82 -0
- package/Benchmark.js.map +1 -0
- package/Benchmark.types.d.ts +28 -0
- package/Benchmark.types.d.ts.map +1 -0
- package/Benchmark.types.js +2 -0
- package/Benchmark.types.js.map +1 -0
- package/Measurement.d.ts +103 -7
- package/Measurement.d.ts.map +1 -0
- package/Measurement.js +148 -14
- package/Measurement.js.map +1 -1
- package/Measurement.types.d.ts +1 -0
- package/Measurement.types.d.ts.map +1 -0
- package/Measurement.types.js +1 -2
- package/README.md +557 -55
- package/Sleep.d.ts +5 -0
- package/Sleep.d.ts.map +1 -0
- package/Sleep.js +8 -0
- package/Sleep.js.map +1 -0
- package/TimeMeasurer.d.ts +15 -10
- package/TimeMeasurer.d.ts.map +1 -0
- package/TimeMeasurer.js +23 -20
- package/TimeMeasurer.js.map +1 -1
- package/TimeProfiler.d.ts +71 -0
- package/TimeProfiler.d.ts.map +1 -0
- package/TimeProfiler.js +171 -0
- package/TimeProfiler.js.map +1 -0
- package/TimeProfiler.types.d.ts +20 -0
- package/TimeProfiler.types.d.ts.map +1 -0
- package/TimeProfiler.types.js +2 -0
- package/TimeProfiler.types.js.map +1 -0
- package/index.d.ts +8 -4
- package/index.d.ts.map +1 -0
- package/index.js +8 -28
- package/index.js.map +1 -1
- package/package.json +22 -23
- package/sleep.d.ts +0 -2
- package/sleep.js +0 -8
- package/sleep.js.map +0 -1
- package/startMeasurement.d.ts +0 -2
- package/startMeasurement.js +0 -13
- package/startMeasurement.js.map +0 -1
package/README.md
CHANGED
@@ -6,108 +6,610 @@
|
|
6
6
|
|
7
7
|
Time Measurer is a simple wrap for `process.hrtime.bigint` to measure time with procession and express that time easily through formatted representations, anytime you want to express how much a query or a request took at code level you may want to give this a try.
|
8
8
|
|
9
|
-
|
9
|
+
# Getting Started
|
10
10
|
|
11
11
|
```shell
|
12
12
|
npm install @universal-packages/time-measurer
|
13
13
|
```
|
14
14
|
|
15
|
-
|
15
|
+
# Usage
|
16
16
|
|
17
|
-
|
17
|
+
## TimeMeasurer `class`
|
18
18
|
|
19
|
-
|
19
|
+
The `TimeMeasurer` class is a simple utility for measuring time with precision. It wraps `process.hrtime.bigint()` for Node.js environments and falls back to `performance.now()` in browser environments.
|
20
20
|
|
21
21
|
```ts
|
22
|
-
import {
|
22
|
+
import { TimeMeasurer } from '@universal-packages/time-measurer'
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
const measurement = measurer.finish()
|
24
|
+
const measurer = TimeMeasurer.start()
|
25
|
+
// ... some operation
|
26
|
+
const measurement = measurer.finish()
|
28
27
|
|
29
|
-
|
30
|
-
|
28
|
+
console.log(measurement.toString()) // "123.456ms"
|
29
|
+
```
|
30
|
+
|
31
|
+
### Constructor <small><small>`constructor`</small></small>
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
```ts
|
34
|
+
new TimeMeasurer()
|
34
35
|
```
|
35
36
|
|
36
|
-
|
37
|
+
Creates a new TimeMeasurer instance. The measurer is not started automatically.
|
37
38
|
|
38
|
-
|
39
|
+
### Instance Methods
|
39
40
|
|
40
|
-
|
41
|
-
import { sleep } from '@universal-packages/time-measurer'
|
41
|
+
#### start
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
}
|
43
|
+
```ts
|
44
|
+
start(): void
|
46
45
|
```
|
47
46
|
|
48
|
-
|
47
|
+
Starts the time measurement. This method will throw an error if the measurer has already been started.
|
48
|
+
|
49
|
+
```ts
|
50
|
+
const measurer = new TimeMeasurer()
|
51
|
+
measurer.start()
|
52
|
+
// ... some operation
|
53
|
+
const measurement = measurer.finish()
|
54
|
+
```
|
49
55
|
|
50
|
-
|
56
|
+
#### finish
|
51
57
|
|
52
|
-
```
|
53
|
-
|
58
|
+
```ts
|
59
|
+
finish(): Measurement
|
60
|
+
```
|
54
61
|
|
55
|
-
|
56
|
-
const measurer = new TimeMeasurer()
|
62
|
+
Finishes the time measurement and returns a `Measurement` instance containing the elapsed time.
|
57
63
|
|
58
|
-
|
64
|
+
```ts
|
65
|
+
const measurer = new TimeMeasurer()
|
66
|
+
measurer.start()
|
67
|
+
// ... some operation
|
68
|
+
const measurement = measurer.finish()
|
69
|
+
```
|
59
70
|
|
60
|
-
|
61
|
-
const measurement = measurer.finish()
|
71
|
+
### Static Methods
|
62
72
|
|
63
|
-
|
64
|
-
|
73
|
+
#### start
|
74
|
+
|
75
|
+
```ts
|
76
|
+
static start(): TimeMeasurer
|
77
|
+
```
|
78
|
+
|
79
|
+
Creates a new TimeMeasurer instance and immediately starts it. This is a convenience method for quick measurements.
|
80
|
+
|
81
|
+
```ts
|
82
|
+
const measurer = TimeMeasurer.start()
|
83
|
+
// ... some operation
|
84
|
+
const measurement = measurer.finish()
|
85
|
+
```
|
86
|
+
|
87
|
+
## Measurement `class`
|
88
|
+
|
89
|
+
The `Measurement` class represents a measured time duration with various formatting options, convenient accessors for different time units, arithmetic operations, and comparison capabilities.
|
90
|
+
|
91
|
+
```ts
|
92
|
+
import { Measurement, TimeMeasurer } from '@universal-packages/time-measurer'
|
93
|
+
|
94
|
+
const measurer = TimeMeasurer.start()
|
95
|
+
// ... some operation
|
96
|
+
const measurement = measurer.finish()
|
97
|
+
|
98
|
+
console.log(measurement.hours) // 0
|
99
|
+
console.log(measurement.minutes) // 0
|
100
|
+
console.log(measurement.seconds) // 1
|
101
|
+
console.log(measurement.milliseconds) // 234.567
|
102
|
+
|
103
|
+
// Arithmetic operations
|
104
|
+
const measurement1 = new Measurement(1000000000n) // 1 second
|
105
|
+
const measurement2 = new Measurement(2000000000n) // 2 seconds
|
106
|
+
|
107
|
+
const sum = measurement1.add(measurement2)
|
108
|
+
console.log(sum.toString()) // "3.000sec"
|
109
|
+
|
110
|
+
// Comparison operations
|
111
|
+
console.log(measurement1 < measurement2) // true
|
112
|
+
console.log(measurement1.lessThan(measurement2)) // true
|
113
|
+
```
|
114
|
+
|
115
|
+
### Constructor <small><small>`constructor`</small></small>
|
116
|
+
|
117
|
+
```ts
|
118
|
+
new Measurement(nanoseconds: bigint)
|
119
|
+
```
|
120
|
+
|
121
|
+
Creates a new Measurement instance from nanoseconds. This is typically called internally by TimeMeasurer.
|
122
|
+
|
123
|
+
### Properties
|
124
|
+
|
125
|
+
- **`hours`**: `number` - The hours component of the measurement
|
126
|
+
- **`minutes`**: `number` - The minutes component of the measurement
|
127
|
+
- **`seconds`**: `number` - The seconds component of the measurement
|
128
|
+
- **`milliseconds`**: `number` - The milliseconds component (including fractional part)
|
129
|
+
- **`nanoseconds`**: `bigint` - The total nanoseconds of the measurement
|
130
|
+
|
131
|
+
### Instance Methods
|
132
|
+
|
133
|
+
#### Arithmetic Operations
|
134
|
+
|
135
|
+
##### add
|
136
|
+
|
137
|
+
```ts
|
138
|
+
add(other: Measurement): Measurement
|
139
|
+
```
|
140
|
+
|
141
|
+
Adds another measurement to this one and returns a new measurement with the sum of both times.
|
142
|
+
|
143
|
+
```ts
|
144
|
+
const measurement1 = new Measurement(1000000000n) // 1 second
|
145
|
+
const measurement2 = new Measurement(500000000n) // 0.5 seconds
|
146
|
+
const sum = measurement1.add(measurement2)
|
147
|
+
console.log(sum.toString()) // "1.500sec"
|
148
|
+
```
|
149
|
+
|
150
|
+
##### subtract
|
151
|
+
|
152
|
+
```ts
|
153
|
+
subtract(other: Measurement): Measurement
|
154
|
+
```
|
155
|
+
|
156
|
+
Subtracts another measurement from this one and returns a new measurement with the difference. If the result would be negative, returns a zero measurement.
|
157
|
+
|
158
|
+
```ts
|
159
|
+
const measurement1 = new Measurement(2000000000n) // 2 seconds
|
160
|
+
const measurement2 = new Measurement(500000000n) // 0.5 seconds
|
161
|
+
const difference = measurement1.subtract(measurement2)
|
162
|
+
console.log(difference.toString()) // "1.500sec"
|
163
|
+
|
164
|
+
// Negative results are clamped to zero
|
165
|
+
const negative = measurement2.subtract(measurement1)
|
166
|
+
console.log(negative.toString()) // "0.00ms"
|
167
|
+
```
|
168
|
+
|
169
|
+
#### Comparison Methods
|
170
|
+
|
171
|
+
##### equals
|
172
|
+
|
173
|
+
```ts
|
174
|
+
equals(other: Measurement): boolean
|
175
|
+
```
|
176
|
+
|
177
|
+
Checks if this measurement is equal to another measurement.
|
178
|
+
|
179
|
+
```ts
|
180
|
+
const measurement1 = new Measurement(1000000000n)
|
181
|
+
const measurement2 = new Measurement(1000000000n)
|
182
|
+
console.log(measurement1.equals(measurement2)) // true
|
183
|
+
```
|
184
|
+
|
185
|
+
##### lessThan
|
186
|
+
|
187
|
+
```ts
|
188
|
+
lessThan(other: Measurement): boolean
|
189
|
+
```
|
190
|
+
|
191
|
+
Checks if this measurement is less than another measurement.
|
192
|
+
|
193
|
+
```ts
|
194
|
+
const measurement1 = new Measurement(500000000n) // 0.5 seconds
|
195
|
+
const measurement2 = new Measurement(1000000000n) // 1 second
|
196
|
+
console.log(measurement1.lessThan(measurement2)) // true
|
197
|
+
```
|
198
|
+
|
199
|
+
##### greaterThan
|
200
|
+
|
201
|
+
```ts
|
202
|
+
greaterThan(other: Measurement): boolean
|
203
|
+
```
|
204
|
+
|
205
|
+
Checks if this measurement is greater than another measurement.
|
206
|
+
|
207
|
+
```ts
|
208
|
+
console.log(measurement2.greaterThan(measurement1)) // true
|
209
|
+
```
|
210
|
+
|
211
|
+
##### lessThanOrEqual
|
212
|
+
|
213
|
+
```ts
|
214
|
+
lessThanOrEqual(other: Measurement): boolean
|
215
|
+
```
|
216
|
+
|
217
|
+
Checks if this measurement is less than or equal to another measurement.
|
218
|
+
|
219
|
+
##### greaterThanOrEqual
|
220
|
+
|
221
|
+
```ts
|
222
|
+
greaterThanOrEqual(other: Measurement): boolean
|
223
|
+
```
|
224
|
+
|
225
|
+
Checks if this measurement is greater than or equal to another measurement.
|
226
|
+
|
227
|
+
#### Operator Overloading
|
228
|
+
|
229
|
+
The `Measurement` class supports JavaScript's native comparison and arithmetic operators through `Symbol.toPrimitive`:
|
230
|
+
|
231
|
+
```ts
|
232
|
+
const measurement1 = new Measurement(1000000000n) // 1 second
|
233
|
+
const measurement2 = new Measurement(2000000000n) // 2 seconds
|
234
|
+
|
235
|
+
// Comparison operators (recommended)
|
236
|
+
console.log(measurement1 < measurement2) // true
|
237
|
+
console.log(measurement1 > measurement2) // false
|
238
|
+
console.log(measurement1 <= measurement2) // true
|
239
|
+
console.log(measurement1 >= measurement2) // false
|
240
|
+
console.log(measurement1 == measurement2) // false
|
241
|
+
console.log(measurement1 != measurement2) // true
|
242
|
+
|
243
|
+
// Arithmetic operators (returns numbers in nanoseconds)
|
244
|
+
console.log(measurement1 + measurement2) // 3000000000 (nanoseconds)
|
245
|
+
console.log(measurement2 - measurement1) // 1000000000 (nanoseconds)
|
246
|
+
|
247
|
+
// For arithmetic operations that return Measurement objects, use methods:
|
248
|
+
const sum = measurement1.add(measurement2) // Returns Measurement
|
249
|
+
const diff = measurement2.subtract(measurement1) // Returns Measurement
|
250
|
+
```
|
251
|
+
|
252
|
+
#### Method Chaining
|
253
|
+
|
254
|
+
All arithmetic methods return new `Measurement` instances, allowing for method chaining:
|
255
|
+
|
256
|
+
```ts
|
257
|
+
const base = new Measurement(1000000000n) // 1 second
|
258
|
+
const half = new Measurement(500000000n) // 0.5 seconds
|
259
|
+
const quarter = new Measurement(250000000n) // 0.25 seconds
|
260
|
+
|
261
|
+
const result = base.add(half).subtract(quarter)
|
262
|
+
console.log(result.toString()) // "1.250sec"
|
263
|
+
```
|
264
|
+
|
265
|
+
#### toString
|
266
|
+
|
267
|
+
```ts
|
268
|
+
toString(format?: TimeFormat): string
|
269
|
+
```
|
270
|
+
|
271
|
+
Converts the measurement to a formatted string representation.
|
272
|
+
|
273
|
+
```ts
|
274
|
+
const measurement = new Measurement(1234567890n)
|
275
|
+
|
276
|
+
console.log(measurement.toString('Human')) // "1.234sec"
|
277
|
+
console.log(measurement.toString('Condensed')) // "1.234"
|
278
|
+
console.log(measurement.toString('Expressive')) // "1.234 Seconds"
|
279
|
+
```
|
280
|
+
|
281
|
+
##### TimeFormat
|
282
|
+
|
283
|
+
- **`'Human'`** (default): User-friendly format like "1.234sec", "2min 30.500sec", "1hrs 15min 30.250sec"
|
284
|
+
- **`'Condensed'`**: Compact format like "1.234", "02:30.500", "01:15:30.250"
|
285
|
+
- **`'Expressive'`**: Verbose format like "1.234 Seconds", "2 Minutes, and 30.500 Seconds"
|
286
|
+
|
287
|
+
#### toDate
|
288
|
+
|
289
|
+
```ts
|
290
|
+
toDate(): Date
|
291
|
+
```
|
292
|
+
|
293
|
+
Converts the measurement to a JavaScript Date object with the time components.
|
294
|
+
|
295
|
+
```ts
|
296
|
+
const measurement = new Measurement(3661000000000n) // 1 hour, 1 minute, 1 second
|
297
|
+
const date = measurement.toDate()
|
298
|
+
console.log(date.getHours()) // 1
|
299
|
+
console.log(date.getMinutes()) // 1
|
300
|
+
console.log(date.getSeconds()) // 1
|
301
|
+
```
|
302
|
+
|
303
|
+
## Benchmark `class`
|
304
|
+
|
305
|
+
The `Benchmark` class provides functionality for running performance benchmarks with support for multiple iterations, warmup runs, and statistical analysis.
|
306
|
+
|
307
|
+
```ts
|
308
|
+
import { Benchmark } from '@universal-packages/time-measurer'
|
309
|
+
|
310
|
+
const benchmark = new Benchmark({
|
311
|
+
iterations: 1000,
|
312
|
+
warmupIterations: 100,
|
313
|
+
name: 'Array sorting benchmark'
|
314
|
+
})
|
315
|
+
|
316
|
+
const result = benchmark.run(() => {
|
317
|
+
const arr = Array.from({ length: 1000 }, () => Math.random())
|
318
|
+
arr.sort()
|
319
|
+
})
|
320
|
+
|
321
|
+
console.log(`Average: ${result.average.toString()}`)
|
322
|
+
console.log(`Min: ${result.min.toString()}`)
|
323
|
+
console.log(`Max: ${result.max.toString()}`)
|
324
|
+
```
|
325
|
+
|
326
|
+
### Constructor <small><small>`constructor`</small></small>
|
327
|
+
|
328
|
+
```ts
|
329
|
+
new Benchmark(options?: BenchmarkOptions)
|
330
|
+
```
|
331
|
+
|
332
|
+
Creates a new Benchmark instance with the specified options.
|
333
|
+
|
334
|
+
#### BenchmarkOptions
|
335
|
+
|
336
|
+
- **`iterations`**: `number` (default: `1`)
|
337
|
+
Number of iterations to run for the actual benchmark measurement.
|
338
|
+
- **`warmupIterations`**: `number` (default: `0`)
|
339
|
+
Number of warmup iterations to run before the actual measurement to stabilize performance.
|
340
|
+
- **`name`**: `string` (default: `'Unnamed Benchmark'`)
|
341
|
+
A descriptive name for the benchmark.
|
342
|
+
|
343
|
+
### Instance Methods
|
344
|
+
|
345
|
+
#### run
|
346
|
+
|
347
|
+
```ts
|
348
|
+
run(fn: () => void): BenchmarkResult
|
349
|
+
```
|
350
|
+
|
351
|
+
Runs a synchronous function benchmark and returns detailed results including statistics.
|
352
|
+
|
353
|
+
```ts
|
354
|
+
const benchmark = new Benchmark({ iterations: 100 })
|
355
|
+
|
356
|
+
const result = benchmark.run(() => {
|
357
|
+
// Your code to benchmark
|
358
|
+
heavyComputation()
|
359
|
+
})
|
360
|
+
|
361
|
+
console.log(`Completed ${result.iterations} iterations`)
|
362
|
+
console.log(`Average time: ${result.average.toString()}`)
|
363
|
+
```
|
364
|
+
|
365
|
+
#### runAsync
|
366
|
+
|
367
|
+
```ts
|
368
|
+
runAsync(fn: () => Promise<void>): Promise<BenchmarkResult>
|
369
|
+
```
|
370
|
+
|
371
|
+
Runs an asynchronous function benchmark and returns detailed results including statistics.
|
372
|
+
|
373
|
+
```ts
|
374
|
+
const benchmark = new Benchmark({ iterations: 50 })
|
375
|
+
|
376
|
+
const result = await benchmark.runAsync(async () => {
|
377
|
+
// Your async code to benchmark
|
378
|
+
await asyncOperation()
|
379
|
+
})
|
380
|
+
|
381
|
+
console.log(`Average time: ${result.average.toString()}`)
|
382
|
+
```
|
383
|
+
|
384
|
+
#### BenchmarkResult
|
385
|
+
|
386
|
+
The result object returned by `run()` and `runAsync()` contains:
|
387
|
+
|
388
|
+
- **`name`**: `string` - Name of the benchmark
|
389
|
+
- **`iterations`**: `number` - Number of iterations performed
|
390
|
+
- **`warmupIterations`**: `number` - Number of warmup iterations performed
|
391
|
+
- **`measurements`**: `Measurement[]` - Array of all individual measurements
|
392
|
+
- **`min`**: `Measurement` - Fastest measurement
|
393
|
+
- **`max`**: `Measurement` - Slowest measurement
|
394
|
+
- **`average`**: `Measurement` - Average of all measurements
|
395
|
+
- **`total`**: `Measurement` - Total time for all iterations
|
396
|
+
|
397
|
+
## TimeProfiler `class`
|
398
|
+
|
399
|
+
The `TimeProfiler` class provides advanced profiling capabilities with named checkpoints, memory tracking, and session management for detailed performance analysis.
|
400
|
+
|
401
|
+
```ts
|
402
|
+
import { TimeProfiler } from '@universal-packages/time-measurer'
|
403
|
+
|
404
|
+
const profiler = TimeProfiler.start('Database Operations')
|
405
|
+
|
406
|
+
// Start some operation
|
407
|
+
const users = await db.users.findMany()
|
408
|
+
profiler.checkpoint('Users loaded')
|
409
|
+
|
410
|
+
// Another operation
|
411
|
+
const posts = await db.posts.findMany()
|
412
|
+
profiler.checkpoint('Posts loaded')
|
413
|
+
|
414
|
+
// Finish profiling
|
415
|
+
const checkpoints = profiler.stop('Complete')
|
416
|
+
|
417
|
+
checkpoints.forEach((cp) => {
|
418
|
+
console.log(`${cp.name}: ${cp.measurement.toString()}`)
|
419
|
+
})
|
420
|
+
```
|
421
|
+
|
422
|
+
### Constructor <small><small>`constructor`</small></small>
|
423
|
+
|
424
|
+
```ts
|
425
|
+
new TimeProfiler(options?: ProfilerOptions)
|
426
|
+
```
|
427
|
+
|
428
|
+
Creates a new TimeProfiler instance with the specified options.
|
429
|
+
|
430
|
+
#### ProfilerOptions
|
431
|
+
|
432
|
+
- **`name`**: `string` (default: `'Profiler Session'`)
|
433
|
+
A descriptive name for the profiling session.
|
434
|
+
- **`trackMemory`**: `boolean` (default: `false`)
|
435
|
+
Whether to track memory usage and deltas between checkpoints.
|
436
|
+
|
437
|
+
### Properties
|
438
|
+
|
439
|
+
#### checkpoints
|
440
|
+
|
441
|
+
```ts
|
442
|
+
readonly checkpoints: ProfilerCheckpoint[]
|
443
|
+
```
|
444
|
+
|
445
|
+
Returns a copy of all checkpoints recorded so far.
|
446
|
+
|
447
|
+
#### lastCheckpoint
|
448
|
+
|
449
|
+
```ts
|
450
|
+
readonly lastCheckpoint: ProfilerCheckpoint | undefined
|
451
|
+
```
|
452
|
+
|
453
|
+
Returns the most recently recorded checkpoint, or undefined if none exist.
|
454
|
+
|
455
|
+
#### isRunning
|
456
|
+
|
457
|
+
```ts
|
458
|
+
readonly isRunning: boolean
|
459
|
+
```
|
460
|
+
|
461
|
+
Returns true if the profiler is currently active and measuring time.
|
462
|
+
|
463
|
+
#### elapsed
|
464
|
+
|
465
|
+
```ts
|
466
|
+
readonly elapsed: Measurement | undefined
|
467
|
+
```
|
468
|
+
|
469
|
+
Returns the current elapsed time since the profiler was started, or undefined if not started.
|
65
470
|
|
66
|
-
|
67
|
-
|
471
|
+
### Instance Methods
|
472
|
+
|
473
|
+
#### start
|
474
|
+
|
475
|
+
```ts
|
476
|
+
start(): void
|
477
|
+
```
|
478
|
+
|
479
|
+
Starts the profiling session. Throws an error if already started.
|
480
|
+
|
481
|
+
```ts
|
482
|
+
const profiler = new TimeProfiler()
|
483
|
+
profiler.start()
|
68
484
|
```
|
69
485
|
|
70
|
-
|
486
|
+
#### checkpoint
|
71
487
|
|
72
|
-
|
488
|
+
```ts
|
489
|
+
checkpoint(name: string): Measurement
|
490
|
+
```
|
73
491
|
|
74
|
-
|
492
|
+
Creates a named checkpoint and returns the measurement from the start time. Throws an error if not started.
|
75
493
|
|
76
|
-
|
494
|
+
```ts
|
495
|
+
profiler.checkpoint('Database connected')
|
496
|
+
// ... more operations
|
497
|
+
profiler.checkpoint('Data processed')
|
498
|
+
```
|
77
499
|
|
78
|
-
|
500
|
+
#### getCheckpoint
|
79
501
|
|
80
|
-
|
502
|
+
```ts
|
503
|
+
getCheckpoint(name: string): ProfilerCheckpoint | undefined
|
504
|
+
```
|
81
505
|
|
82
|
-
|
506
|
+
Retrieves a specific checkpoint by name.
|
83
507
|
|
84
|
-
|
508
|
+
```ts
|
509
|
+
const dbCheckpoint = profiler.getCheckpoint('Database connected')
|
510
|
+
if (dbCheckpoint) {
|
511
|
+
console.log(`DB connection took: ${dbCheckpoint.measurement.toString()}`)
|
512
|
+
}
|
513
|
+
```
|
85
514
|
|
86
|
-
####
|
515
|
+
#### stop
|
87
516
|
|
88
|
-
|
517
|
+
```ts
|
518
|
+
stop(finalCheckpointName?: string): ProfilerCheckpoint[]
|
519
|
+
```
|
89
520
|
|
90
|
-
|
521
|
+
Stops the profiler, creates a final checkpoint, and returns all checkpoints.
|
91
522
|
|
523
|
+
```ts
|
524
|
+
const allCheckpoints = profiler.stop('Session Complete')
|
92
525
|
```
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
526
|
+
|
527
|
+
#### reset
|
528
|
+
|
529
|
+
```ts
|
530
|
+
reset(): void
|
97
531
|
```
|
98
532
|
|
99
|
-
|
533
|
+
Resets the profiler state to start a new session.
|
100
534
|
|
535
|
+
```ts
|
536
|
+
profiler.reset()
|
537
|
+
profiler.start() // Ready for a new session
|
101
538
|
```
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
539
|
+
|
540
|
+
### Static Methods
|
541
|
+
|
542
|
+
#### start
|
543
|
+
|
544
|
+
```ts
|
545
|
+
static start(name?: string): TimeProfiler
|
546
|
+
```
|
547
|
+
|
548
|
+
Creates and immediately starts a new TimeProfiler instance.
|
549
|
+
|
550
|
+
```ts
|
551
|
+
const profiler = TimeProfiler.start('API Request Processing')
|
552
|
+
```
|
553
|
+
|
554
|
+
#### ProfilerCheckpoint
|
555
|
+
|
556
|
+
Each checkpoint contains:
|
557
|
+
|
558
|
+
- **`name`**: `string` - Name/label for the checkpoint
|
559
|
+
- **`measurement`**: `Measurement` - Time elapsed from start to this checkpoint
|
560
|
+
- **`timestamp`**: `Date` - When the checkpoint was created
|
561
|
+
- **`memoryUsage`**: `number` (optional) - Memory usage in bytes (if tracking enabled)
|
562
|
+
- **`memoryDelta`**: `number` (optional) - Memory change from previous checkpoint (if tracking enabled)
|
563
|
+
|
564
|
+
### Memory Tracking Example
|
565
|
+
|
566
|
+
```ts
|
567
|
+
const profiler = new TimeProfiler({ trackMemory: true })
|
568
|
+
profiler.start()
|
569
|
+
|
570
|
+
// Create some data
|
571
|
+
const largeArray = new Array(1000000).fill('data')
|
572
|
+
const checkpoint1 = profiler.checkpoint('Array created')
|
573
|
+
|
574
|
+
// Process data
|
575
|
+
largeArray.forEach((item) => item.toUpperCase())
|
576
|
+
const checkpoint2 = profiler.checkpoint('Array processed')
|
577
|
+
|
578
|
+
console.log(`Memory at checkpoint 1: ${checkpoint1.memoryUsage} bytes`)
|
579
|
+
console.log(`Memory delta: ${checkpoint2.memoryDelta} bytes`)
|
580
|
+
```
|
581
|
+
|
582
|
+
## Sleep `class`
|
583
|
+
|
584
|
+
The `Sleep` class provides a simple utility for creating delays in asynchronous code using human-readable time strings. It leverages the `ms` library for flexible time string parsing.
|
585
|
+
|
586
|
+
```ts
|
587
|
+
import { Sleep } from '@universal-packages/time-measurer'
|
588
|
+
|
589
|
+
// Wait for 2 seconds
|
590
|
+
await Sleep.for('2s')
|
591
|
+
|
592
|
+
// Wait for 1.5 seconds
|
593
|
+
await Sleep.for('1500ms')
|
594
|
+
|
595
|
+
// Wait for 2 minutes
|
596
|
+
await Sleep.for('2m')
|
597
|
+
|
598
|
+
// Wait for 1 hour
|
599
|
+
await Sleep.for('1h')
|
600
|
+
```
|
601
|
+
|
602
|
+
### Static Methods
|
603
|
+
|
604
|
+
#### for
|
605
|
+
|
606
|
+
```ts
|
607
|
+
static for(timeString: StringValue): Promise<void>
|
106
608
|
```
|
107
609
|
|
108
|
-
|
610
|
+
Creates a delay for the specified duration using a human-readable time string. Returns a Promise that resolves after the specified time has elapsed.
|
109
611
|
|
110
|
-
|
612
|
+
The `timeString` parameter accepts the same format as the `ms` library
|
111
613
|
|
112
614
|
## Typescript
|
113
615
|
|
package/Sleep.d.ts
ADDED
package/Sleep.d.ts.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Sleep.d.ts","sourceRoot":"","sources":["../src/Sleep.ts"],"names":[],"mappings":"AAAA,OAAW,EAAE,WAAW,EAAE,MAAM,IAAI,CAAA;AAEpC,qBAAa,KAAK;WACI,GAAG,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;CAIhE"}
|
package/Sleep.js
ADDED
package/Sleep.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Sleep.js","sourceRoot":"","sources":["../src/Sleep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,MAAM,IAAI,CAAA;AAEpC,MAAM,OAAO,KAAK;IACT,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAuB;QAC7C,MAAM,QAAQ,GAAG,EAAE,CAAC,UAAU,CAAC,CAAA;QAC/B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAA;IAChE,CAAC;CACF"}
|