@universal-packages/time-measurer 1.3.0 → 1.4.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/LICENSE.md +9 -0
- package/Measurement.d.ts +3 -3
- package/Measurement.js +4 -4
- package/Measurement.js.map +1 -1
- package/README.md +8 -11
- package/TimeMeasurer.d.ts +5 -4
- package/TimeMeasurer.js +8 -4
- package/TimeMeasurer.js.map +1 -1
- package/package.json +3 -2
- package/sleep.d.ts +1 -1
- package/sleep.js +1 -1
- package/sleep.js.map +1 -1
- package/startMeasurement.js +3 -1
- package/startMeasurement.js.map +1 -1
package/LICENSE.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022-present David De Anda
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/Measurement.d.ts
CHANGED
@@ -5,11 +5,11 @@ export default class Measurement {
|
|
5
5
|
readonly seconds: number;
|
6
6
|
readonly milliseconds: number;
|
7
7
|
constructor(nanoseconds: bigint);
|
8
|
-
/** Returns the measured time in a
|
8
|
+
/** Returns the measured time in a formatted string */
|
9
9
|
toString(format?: TimeFormat): string;
|
10
|
-
/** Returns the measured time in a
|
10
|
+
/** Returns the measured time in a formatted string */
|
11
11
|
toDate(): Date;
|
12
|
-
private
|
12
|
+
private getCondensed;
|
13
13
|
private getHuman;
|
14
14
|
private gerExpressive;
|
15
15
|
private pad;
|
package/Measurement.js
CHANGED
@@ -11,22 +11,22 @@ class Measurement {
|
|
11
11
|
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.seconds)) * 1000000000n;
|
12
12
|
this.milliseconds = Number(currentNanoseconds) / 1000000;
|
13
13
|
}
|
14
|
-
/** Returns the measured time in a
|
14
|
+
/** Returns the measured time in a formatted string */
|
15
15
|
toString(format = 'Human') {
|
16
16
|
switch (format) {
|
17
17
|
case 'Condensed':
|
18
|
-
return this.
|
18
|
+
return this.getCondensed();
|
19
19
|
case 'Human':
|
20
20
|
return this.getHuman();
|
21
21
|
case 'Expressive':
|
22
22
|
return this.gerExpressive();
|
23
23
|
}
|
24
24
|
}
|
25
|
-
/** Returns the measured time in a
|
25
|
+
/** Returns the measured time in a formatted string */
|
26
26
|
toDate() {
|
27
27
|
return new Date(0, 0, 0, this.hours, this.minutes, this.seconds, this.milliseconds);
|
28
28
|
}
|
29
|
-
|
29
|
+
getCondensed() {
|
30
30
|
if (this.hours !== 0) {
|
31
31
|
return `${this.pad(this.hours)}:${this.pad(this.minutes)}:${this.pad(this.seconds)}.${this.pad(this.milliseconds, 3)}`;
|
32
32
|
}
|
package/Measurement.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Measurement.js","sourceRoot":"","sources":["../src/Measurement.ts"],"names":[],"mappings":";;AAEA,MAAqB,WAAW;IAM9B,YAAmB,WAAmB;QACpC,IAAI,kBAAkB,GAAG,WAAW,CAAA;QAEpC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,kBAAk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C,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,UAAU,CAAA;SACrH;aAAM,IAAI,IAAI,CAAC,OAAO,KAAK,CAAC,EAAE;YAC7B,OAAO,GAAG,IAAI,CAAC,OAAO,iBAAiB,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,UAAU,CAAA;SAChG;aAAM,IAAI,IAAI,CAAC,OAAO,KAAK,CAAC,EAAE;YAC7B,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,UAAU,CAAA;SACnE;aAAM;YACL,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,EAAE,CAAC,CAAC,eAAe,CAAA;SAC3D;IACH,CAAC;IAEO,GAAG,CAAC,GAAW,EAAE,MAAM,GAAG,CAAC,EAAE,YAAY,GAAG,CAAC;QACnD,OAAO,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACxD,CAAC;CACF;AA7ED,8BA6EC"}
|
package/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Time Measurer
|
2
|
+
|
2
3
|
[](https://www.npmjs.com/package/@universal-packages/time-measurer)
|
3
4
|
[](https://github.com/universal-packages/universal-time-measurer/actions/workflows/testing.yml)
|
4
5
|
[](https://codecov.io/gh/universal-packages/universal-time-measurer)
|
@@ -13,7 +14,7 @@ npm install @universal-packages/time-measurer
|
|
13
14
|
|
14
15
|
## startMeasurement()
|
15
16
|
|
16
|
-
Creates a new TimeMeasurer instance to start a
|
17
|
+
Creates a new TimeMeasurer instance to start a measurement.
|
17
18
|
|
18
19
|
```ts
|
19
20
|
import { startMeasurement } from '@universal-packages/time-measurer'
|
@@ -26,7 +27,6 @@ async function getAll() {
|
|
26
27
|
console.log('All records - ', measurement.toString())
|
27
28
|
}
|
28
29
|
|
29
|
-
|
30
30
|
getAll()
|
31
31
|
// > All records - 2.23ms
|
32
32
|
```
|
@@ -41,6 +41,8 @@ import { TimeMeasurer } from '@universal-packages/time-measurer'
|
|
41
41
|
async function getAll() {
|
42
42
|
const measurer = new TimeMeasurer()
|
43
43
|
|
44
|
+
measurer.start()
|
45
|
+
|
44
46
|
const data = await myDB.getAllRecords()
|
45
47
|
const measurement = measurer.finish()
|
46
48
|
|
@@ -66,7 +68,7 @@ measurement.toString('Human')
|
|
66
68
|
measurement.toString('Expressive')
|
67
69
|
```
|
68
70
|
|
69
|
-
You will get
|
71
|
+
You will get something like
|
70
72
|
|
71
73
|
```
|
72
74
|
2hrs 35min 51.235sec
|
@@ -96,16 +98,12 @@ measurement.toDate()
|
|
96
98
|
|
97
99
|
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.
|
98
100
|
|
99
|
-
|
100
101
|
```js
|
101
|
-
import
|
102
|
+
import { sleep } from '@universal-packages/time-measurer'
|
102
103
|
|
103
|
-
async function
|
104
|
-
|
105
|
-
|
106
|
-
sleep(2000)
|
104
|
+
async function awaitable() {
|
105
|
+
await sleep(2000)
|
107
106
|
}
|
108
|
-
|
109
107
|
```
|
110
108
|
|
111
109
|
## Typescript
|
@@ -122,4 +120,3 @@ The development of this library happens in the open on GitHub, and we are gratef
|
|
122
120
|
### License
|
123
121
|
|
124
122
|
[MIT licensed](./LICENSE).
|
125
|
-
|
package/TimeMeasurer.d.ts
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
import Measurement from './Measurement';
|
2
2
|
/**
|
3
3
|
*
|
4
|
-
* It
|
4
|
+
* It measures the time a process takes from the time start is called
|
5
5
|
* to when the finish method is called
|
6
6
|
*
|
7
7
|
*/
|
8
8
|
export default class TimeMeasurer {
|
9
|
-
private
|
10
|
-
|
11
|
-
|
9
|
+
private hrStart;
|
10
|
+
/** Resets the initial time */
|
11
|
+
start(): void;
|
12
|
+
/** Returns a measurement representing the time passed from when start was called */
|
12
13
|
finish(): Measurement;
|
13
14
|
}
|
package/TimeMeasurer.js
CHANGED
@@ -6,17 +6,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const Measurement_1 = __importDefault(require("./Measurement"));
|
7
7
|
/**
|
8
8
|
*
|
9
|
-
* It
|
9
|
+
* It measures the time a process takes from the time start is called
|
10
10
|
* to when the finish method is called
|
11
11
|
*
|
12
12
|
*/
|
13
13
|
class TimeMeasurer {
|
14
14
|
constructor() {
|
15
|
-
this.
|
15
|
+
this.hrStart = 0n;
|
16
16
|
}
|
17
|
-
/**
|
17
|
+
/** Resets the initial time */
|
18
|
+
start() {
|
19
|
+
this.hrStart = process.hrtime.bigint();
|
20
|
+
}
|
21
|
+
/** Returns a measurement representing the time passed from when start was called */
|
18
22
|
finish() {
|
19
|
-
const nanoseconds = process.hrtime.bigint() - this.
|
23
|
+
const nanoseconds = process.hrtime.bigint() - this.hrStart;
|
20
24
|
return new Measurement_1.default(nanoseconds);
|
21
25
|
}
|
22
26
|
}
|
package/TimeMeasurer.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"TimeMeasurer.js","sourceRoot":"","sources":["../src/TimeMeasurer.ts"],"names":[],"mappings":";;;;;AAAA,gEAAuC;AAEvC;;;;;GAKG;AACH,MAAqB,YAAY;
|
1
|
+
{"version":3,"file":"TimeMeasurer.js","sourceRoot":"","sources":["../src/TimeMeasurer.ts"],"names":[],"mappings":";;;;;AAAA,gEAAuC;AAEvC;;;;;GAKG;AACH,MAAqB,YAAY;IAAjC;QACU,YAAO,GAAW,EAAE,CAAA;IAa9B,CAAC;IAXC,8BAA8B;IACvB,KAAK;QACV,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAA;IACxC,CAAC;IAED,oFAAoF;IAC7E,MAAM;QACX,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAE1D,OAAO,IAAI,qBAAW,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;CACF;AAdD,+BAcC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@universal-packages/time-measurer",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.4.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",
|
@@ -10,7 +10,8 @@
|
|
10
10
|
"scripts": {
|
11
11
|
"build": "tsc --p tsconfig.dis.json",
|
12
12
|
"test": "jest --watch",
|
13
|
-
"test:full": "jest --coverage --verbose"
|
13
|
+
"test:full": "jest --coverage --verbose",
|
14
|
+
"test:clear": "jest --clearCache"
|
14
15
|
},
|
15
16
|
"dependencies": {},
|
16
17
|
"devDependencies": {
|
package/sleep.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
/** Simple
|
1
|
+
/** Simple awaitable timeout to sleep the process */
|
2
2
|
export default function sleep(time: number): Promise<void>;
|
package/sleep.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
/** Simple
|
3
|
+
/** Simple awaitable timeout to sleep the process */
|
4
4
|
async function sleep(time) {
|
5
5
|
return new Promise((resolve) => setTimeout(() => resolve(), time));
|
6
6
|
}
|
package/sleep.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../src/sleep.ts"],"names":[],"mappings":";;AAAA,
|
1
|
+
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../src/sleep.ts"],"names":[],"mappings":";;AAAA,oDAAoD;AACrC,KAAK,UAAU,KAAK,CAAC,IAAY;IAC9C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAkB,EAAE,CAAC,UAAU,CAAC,GAAS,EAAE,CAAC,OAAO,EAAE,EAAE,IAAI,CAAC,CAAC,CAAA;AAC1F,CAAC;AAFD,wBAEC"}
|
package/startMeasurement.js
CHANGED
@@ -6,7 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.startMeasurement = void 0;
|
7
7
|
const TimeMeasurer_1 = __importDefault(require("./TimeMeasurer"));
|
8
8
|
function startMeasurement() {
|
9
|
-
|
9
|
+
const measurer = new TimeMeasurer_1.default();
|
10
|
+
measurer.start();
|
11
|
+
return measurer;
|
10
12
|
}
|
11
13
|
exports.startMeasurement = startMeasurement;
|
12
14
|
//# sourceMappingURL=startMeasurement.js.map
|
package/startMeasurement.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"startMeasurement.js","sourceRoot":"","sources":["../src/startMeasurement.ts"],"names":[],"mappings":";;;;;;AAAA,kEAAyC;AAEzC,SAAgB,gBAAgB;IAC9B,
|
1
|
+
{"version":3,"file":"startMeasurement.js","sourceRoot":"","sources":["../src/startMeasurement.ts"],"names":[],"mappings":";;;;;;AAAA,kEAAyC;AAEzC,SAAgB,gBAAgB;IAC9B,MAAM,QAAQ,GAAG,IAAI,sBAAY,EAAE,CAAA;IAEnC,QAAQ,CAAC,KAAK,EAAE,CAAA;IAEhB,OAAO,QAAQ,CAAA;AACjB,CAAC;AAND,4CAMC"}
|