@universal-packages/time-measurer 1.0.0 → 1.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/Measurement.js +6 -6
- package/Measurement.js.map +1 -1
- package/README.md +128 -0
- package/package.json +2 -2
package/Measurement.js
CHANGED
@@ -42,13 +42,13 @@ class Measurement {
|
|
42
42
|
}
|
43
43
|
getHuman() {
|
44
44
|
if (this.hours !== 0) {
|
45
|
-
return `${this.
|
45
|
+
return `${this.hours}hrs ${this.minutes}min ${this.seconds}.${this.pad(this.milliseconds, 3)}sec`;
|
46
46
|
}
|
47
47
|
else if (this.minutes !== 0) {
|
48
|
-
return `${this.
|
48
|
+
return `${this.minutes}min ${this.seconds}.${this.pad(this.milliseconds, 3)}sec`;
|
49
49
|
}
|
50
50
|
else if (this.seconds !== 0) {
|
51
|
-
return `${this.
|
51
|
+
return `${this.seconds}.${this.pad(this.milliseconds, 3)}sec`;
|
52
52
|
}
|
53
53
|
else {
|
54
54
|
return `${this.pad(this.milliseconds, 3, 2)}ms`;
|
@@ -56,13 +56,13 @@ class Measurement {
|
|
56
56
|
}
|
57
57
|
gerExpressive() {
|
58
58
|
if (this.hours !== 0) {
|
59
|
-
return `${this.
|
59
|
+
return `${this.hours} Hours, ${this.minutes} Minutes, and ${this.seconds}.${this.pad(this.milliseconds, 3)} Seconds`;
|
60
60
|
}
|
61
61
|
else if (this.minutes !== 0) {
|
62
|
-
return `${this.
|
62
|
+
return `${this.minutes} Minutes, and ${this.seconds}.${this.pad(this.milliseconds, 3)} Seconds`;
|
63
63
|
}
|
64
64
|
else if (this.seconds !== 0) {
|
65
|
-
return `${this.
|
65
|
+
return `${this.seconds}.${this.pad(this.milliseconds, 3)} Seconds`;
|
66
66
|
}
|
67
67
|
else {
|
68
68
|
return `${this.pad(this.milliseconds, 3, 2)} Milliseconds`;
|
package/Measurement.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Measurement.js","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
|
+
[](https://github.com/universal-packages/universal-time-measurer/actions/workflows/testing.yml)
|
4
|
+
[](https://codecov.io/gh/universal-packages/universal-time-measurer)
|
5
|
+
|
6
|
+
Time Measurer is a simple wrap for `process.hrtime` 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.
|
7
|
+
|
8
|
+
## Install
|
9
|
+
|
10
|
+
```shell
|
11
|
+
npm install @universal-packages/time-measurer
|
12
|
+
```
|
13
|
+
|
14
|
+
## TimeMeasurer
|
15
|
+
|
16
|
+
Class `TimeMeasurer` provides an instantiable interface to start measuring time from any part of your code.
|
17
|
+
|
18
|
+
```js
|
19
|
+
import TimeMeasurer from '@universal-packages/time-measurer'
|
20
|
+
|
21
|
+
async function getAll() {
|
22
|
+
const measurer = new TimeMeasurer()
|
23
|
+
|
24
|
+
measurer.start()
|
25
|
+
|
26
|
+
const data = await myDB.getAllRecords()
|
27
|
+
const measurement = measurer.finish()
|
28
|
+
|
29
|
+
console.log('All records - ', measurement.toString())
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
getAll()
|
34
|
+
// > All records - 2.23ms
|
35
|
+
```
|
36
|
+
|
37
|
+
## Measurement
|
38
|
+
|
39
|
+
A `Measurement` object is the time representation after a measure, it provides the interface to express time as a formatted string or even as a date object.
|
40
|
+
|
41
|
+
### .toString()
|
42
|
+
|
43
|
+
Get the time representation as a string, this function takes one param `TimeFormat`, that can be one of `Condensed`, `Human`, `Expressive`, default: `Human`.
|
44
|
+
|
45
|
+
```js
|
46
|
+
measurement.toString()
|
47
|
+
measurement.toString('Condensed')
|
48
|
+
measurement.toString('Human')
|
49
|
+
measurement.toString('Expressive')
|
50
|
+
```
|
51
|
+
|
52
|
+
You will get someting like
|
53
|
+
|
54
|
+
```
|
55
|
+
02hrs 35min 51.235sec
|
56
|
+
02:35:51.235
|
57
|
+
02hrs 35min 51.235sec
|
58
|
+
02 Hours, 35 Minutes, and 51.235 Seconds
|
59
|
+
```
|
60
|
+
|
61
|
+
It will take into account parts of the representation that are not contributing to the time, like if the measurement only took seconds, minutes and hours will not be included.
|
62
|
+
|
63
|
+
```
|
64
|
+
51.235sec
|
65
|
+
51.235
|
66
|
+
51.235sec
|
67
|
+
51.235 Seconds
|
68
|
+
```
|
69
|
+
|
70
|
+
### .toString()
|
71
|
+
|
72
|
+
Get the time representation as a date object this can be helpful if you want to use the `Date` api to format or do whatever with the date.
|
73
|
+
|
74
|
+
```js
|
75
|
+
measurement.toDate()
|
76
|
+
```
|
77
|
+
|
78
|
+
## Functional
|
79
|
+
|
80
|
+
A more simple way to use the time measurer API is by importing just the `start` and `finish` functions, the only disadvantage here is that you can only measure one thing at a time, unlike `TimeMeasurer` that can be instantiated multiple times and measurer several things useful when you have a lot of async tasks running.
|
81
|
+
|
82
|
+
```ts
|
83
|
+
import { start, finish } from '@universal-packages/time-measurer'
|
84
|
+
|
85
|
+
async function getAll() {
|
86
|
+
start()
|
87
|
+
const data = await myDB.getAllRecords()
|
88
|
+
const measurement = finish()
|
89
|
+
|
90
|
+
console.log('All records - ', measurement.toString())
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
getAll()
|
95
|
+
// > All records - 2.23ms
|
96
|
+
```
|
97
|
+
|
98
|
+
## Sleep
|
99
|
+
|
100
|
+
Time measurer ships with a convenient sleep function that takes a single parameter `time` in milliseconds, internally it is just a promise with a timeout that resolves it.
|
101
|
+
|
102
|
+
|
103
|
+
```js
|
104
|
+
import TimeMeasurer, { sleep } from '@universal-packages/time-measurer'
|
105
|
+
|
106
|
+
async function waitable() {
|
107
|
+
TimeMeasurer.sleep(1000)
|
108
|
+
|
109
|
+
sleep(2000)
|
110
|
+
}
|
111
|
+
|
112
|
+
```
|
113
|
+
|
114
|
+
## Typescript
|
115
|
+
|
116
|
+
This library is developed in TypeScript and shipped fully typed.
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
|
121
|
+
|
122
|
+
- [Code of Conduct](./CODE_OF_CONDUCT.md)
|
123
|
+
- [Contributing Guide](./CONTRIBUTING.md)
|
124
|
+
|
125
|
+
### License
|
126
|
+
|
127
|
+
[MIT licensed](./LICENSE).
|
128
|
+
|
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@universal-packages/time-measurer",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.1.0",
|
4
4
|
"description": "Utility to measure routines times with precision",
|
5
5
|
"author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)",
|
6
6
|
"license": "MIT",
|
7
7
|
"main": "index.js",
|
8
8
|
"types": "index.d.ts",
|
9
|
-
"repository": "git://git@github.com/universal-
|
9
|
+
"repository": "git://git@github.com/universal-packages/universal-time-measurer.git",
|
10
10
|
"scripts": {
|
11
11
|
"build": "tsc --p tsconfig.dis.json",
|
12
12
|
"test": "jest --watch",
|