@node-cli/perf 1.0.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 +21 -0
- package/README.md +78 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/performance.d.ts +15 -0
- package/dist/performance.js +51 -0
- package/dist/performance.js.map +1 -0
- package/dist/unique-id.d.ts +17 -0
- package/dist/unique-id.js +26 -0
- package/dist/unique-id.js.map +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Arno Versini
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Node CLI perf package
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
> Performance tools for nodejs command-line applications.
|
|
6
|
+
|
|
7
|
+
## Performance
|
|
8
|
+
|
|
9
|
+
The class Performance is a wrapper around [nodejs Performance measurement APIs](https://nodejs.org/api/perf_hooks.html).
|
|
10
|
+
|
|
11
|
+
It is intended for an extremely simple case:
|
|
12
|
+
|
|
13
|
+
- start performance monitoring
|
|
14
|
+
- do something that takes a while
|
|
15
|
+
- stop performance monitoring
|
|
16
|
+
- read how much time passed between start and stop (in milliseconds)
|
|
17
|
+
- rinse and repeat
|
|
18
|
+
|
|
19
|
+
### Methods
|
|
20
|
+
|
|
21
|
+
| Method | Description |
|
|
22
|
+
| ------ | ------------------------------------------------ |
|
|
23
|
+
| start | Starts measuring performance |
|
|
24
|
+
| stop | Stops measuring performance and store the result |
|
|
25
|
+
|
|
26
|
+
| Getter | Type | Description |
|
|
27
|
+
| ---------------- | ------ | -------------------- |
|
|
28
|
+
| results | Object | |
|
|
29
|
+
| results.duration | Number | Time in milliseconds |
|
|
30
|
+
|
|
31
|
+
### Examples
|
|
32
|
+
|
|
33
|
+
#### Basic performance gathering
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { Performance } from "@node-cli/perf;
|
|
37
|
+
const perf = new Performance();
|
|
38
|
+
|
|
39
|
+
// Somewhere in your code, you want to start measuring performance:
|
|
40
|
+
perf.start();
|
|
41
|
+
// Do long lasting actions
|
|
42
|
+
(...)
|
|
43
|
+
// When done, tell performance to stop:
|
|
44
|
+
perf.stop();
|
|
45
|
+
// The duration can now be found in the Performance class getter `results`:
|
|
46
|
+
console.log(`It took ${perf.results.duration} milliseconds to run...`);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Multiple performance gatherings
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { Performance } from "@node-cli/perf;
|
|
53
|
+
const perf = new Performance();
|
|
54
|
+
|
|
55
|
+
// Somewhere in your code, you want to start measuring performance:
|
|
56
|
+
perf.start();
|
|
57
|
+
// Do long lasting actions
|
|
58
|
+
(...)
|
|
59
|
+
// When done, tell performance to stop:
|
|
60
|
+
perf.stop();
|
|
61
|
+
// Save the results
|
|
62
|
+
const res1 = perf.results.duration;
|
|
63
|
+
|
|
64
|
+
// Further down in your code, start measuring another performance:
|
|
65
|
+
perf.start();
|
|
66
|
+
// Do other long lasting actions
|
|
67
|
+
(...)
|
|
68
|
+
// When done, tell performance to stop:
|
|
69
|
+
perf.stop();
|
|
70
|
+
// Save the results
|
|
71
|
+
const res2 = perf.results.duration;
|
|
72
|
+
|
|
73
|
+
// -> res1 and res2 will have 2 different duration results.
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
MIT © Arno Versini
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from \"./performance.js\";\n"],"names":[],"mappings":"AAAA,cAAc,mBAAmB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { PerformanceObserver } from "node:perf_hooks";
|
|
3
|
+
export declare class Performance {
|
|
4
|
+
perfObserver: PerformanceObserver;
|
|
5
|
+
startMarkerName: any;
|
|
6
|
+
measureName: string;
|
|
7
|
+
constructor();
|
|
8
|
+
start(): void;
|
|
9
|
+
stop(): void;
|
|
10
|
+
static now(): number;
|
|
11
|
+
get now(): typeof Performance.now;
|
|
12
|
+
get results(): {
|
|
13
|
+
duration: number;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PerformanceObserver, performance } from "node:perf_hooks";
|
|
2
|
+
import { Logger } from "@node-cli/logger";
|
|
3
|
+
import { uniqueID } from "./unique-id.js";
|
|
4
|
+
const logger = new Logger({
|
|
5
|
+
boring: process.env.NODE_ENV === "test"
|
|
6
|
+
});
|
|
7
|
+
export class Performance {
|
|
8
|
+
perfObserver;
|
|
9
|
+
startMarkerName;
|
|
10
|
+
measureName;
|
|
11
|
+
constructor(){
|
|
12
|
+
this.perfObserver = new PerformanceObserver(/* istanbul ignore next */ ()=>{
|
|
13
|
+
performance.clearMeasures();
|
|
14
|
+
});
|
|
15
|
+
this.perfObserver.observe({
|
|
16
|
+
type: "measure"
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
start() {
|
|
20
|
+
if (this.startMarkerName) {
|
|
21
|
+
logger.error("Performance.start() can only be called once");
|
|
22
|
+
} else {
|
|
23
|
+
this.startMarkerName = uniqueID();
|
|
24
|
+
performance.mark(this.startMarkerName);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
stop() {
|
|
28
|
+
if (this.startMarkerName) {
|
|
29
|
+
const stopMarkerName = uniqueID();
|
|
30
|
+
this.measureName = `internal-${this.startMarkerName}-${stopMarkerName}`;
|
|
31
|
+
performance.mark(stopMarkerName);
|
|
32
|
+
performance.measure(this.measureName, this.startMarkerName, stopMarkerName);
|
|
33
|
+
this.startMarkerName = undefined;
|
|
34
|
+
} else {
|
|
35
|
+
logger.error("Performance.stop() can only be called once after Performance.start()");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
static now() {
|
|
39
|
+
return performance?.now() * 1e6;
|
|
40
|
+
}
|
|
41
|
+
get now() {
|
|
42
|
+
return Performance.now;
|
|
43
|
+
}
|
|
44
|
+
get results() {
|
|
45
|
+
return {
|
|
46
|
+
duration: performance?.getEntriesByName(this.measureName)[0]?.duration || undefined
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=performance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/performance.ts"],"sourcesContent":["import { PerformanceObserver, performance } from \"node:perf_hooks\";\n\nimport { Logger } from \"@node-cli/logger\";\nimport { uniqueID } from \"./unique-id.js\";\n\nconst logger = new Logger({\n\tboring: process.env.NODE_ENV === \"test\",\n});\n\nexport class Performance {\n\tperfObserver: PerformanceObserver;\n\tstartMarkerName: any;\n\tmeasureName: string;\n\n\tconstructor() {\n\t\tthis.perfObserver = new PerformanceObserver(\n\t\t\t/* istanbul ignore next */ () => {\n\t\t\t\tperformance.clearMeasures();\n\t\t\t}\n\t\t);\n\t\tthis.perfObserver.observe({ type: \"measure\" });\n\t}\n\n\tstart() {\n\t\tif (this.startMarkerName) {\n\t\t\tlogger.error(\"Performance.start() can only be called once\");\n\t\t} else {\n\t\t\tthis.startMarkerName = uniqueID();\n\t\t\tperformance.mark(this.startMarkerName);\n\t\t}\n\t}\n\n\tstop() {\n\t\tif (this.startMarkerName) {\n\t\t\tconst stopMarkerName = uniqueID();\n\t\t\tthis.measureName = `internal-${this.startMarkerName}-${stopMarkerName}`;\n\n\t\t\tperformance.mark(stopMarkerName);\n\n\t\t\tperformance.measure(\n\t\t\t\tthis.measureName,\n\t\t\t\tthis.startMarkerName,\n\t\t\t\tstopMarkerName\n\t\t\t);\n\t\t\tthis.startMarkerName = undefined;\n\t\t} else {\n\t\t\tlogger.error(\n\t\t\t\t\"Performance.stop() can only be called once after Performance.start()\"\n\t\t\t);\n\t\t}\n\t}\n\n\tstatic now() {\n\t\treturn performance?.now() * 1e6;\n\t}\n\n\tget now() {\n\t\treturn Performance.now;\n\t}\n\n\tget results() {\n\t\treturn {\n\t\t\tduration:\n\t\t\t\tperformance?.getEntriesByName(this.measureName)[0]?.duration ||\n\t\t\t\tundefined,\n\t\t};\n\t}\n}\n"],"names":["PerformanceObserver","performance","Logger","uniqueID","logger","boring","process","env","NODE_ENV","Performance","perfObserver","startMarkerName","measureName","constructor","clearMeasures","observe","type","start","error","mark","stop","stopMarkerName","measure","undefined","now","results","duration","getEntriesByName"],"mappings":"AAAA,SAASA,mBAAmB,EAAEC,WAAW,QAAQ,kBAAkB;AAEnE,SAASC,MAAM,QAAQ,mBAAmB;AAC1C,SAASC,QAAQ,QAAQ,iBAAiB;AAE1C,MAAMC,SAAS,IAAIF,OAAO;IACzBG,QAAQC,QAAQC,IAAIC,aAAa;AAClC;AAEA,OAAO,MAAMC;IACZC,aAAkC;IAClCC,gBAAqB;IACrBC,YAAoB;IAEpBC,aAAc;QACb,IAAI,CAACH,eAAe,IAAIV,oBACvB,wBAAwB,GAAG;YAC1BC,YAAYa;QACb;QAED,IAAI,CAACJ,aAAaK,QAAQ;YAAEC,MAAM;QAAU;IAC7C;IAEAC,QAAQ;QACP,IAAI,IAAI,CAACN,iBAAiB;YACzBP,OAAOc,MAAM;QACd,OAAO;YACN,IAAI,CAACP,kBAAkBR;YACvBF,YAAYkB,KAAK,IAAI,CAACR;QACvB;IACD;IAEAS,OAAO;QACN,IAAI,IAAI,CAACT,iBAAiB;YACzB,MAAMU,iBAAiBlB;YACvB,IAAI,CAACS,cAAc,CAAC,SAAS,EAAE,IAAI,CAACD,gBAAgB,CAAC,EAAEU,eAAe,CAAC;YAEvEpB,YAAYkB,KAAKE;YAEjBpB,YAAYqB,QACX,IAAI,CAACV,aACL,IAAI,CAACD,iBACLU;YAED,IAAI,CAACV,kBAAkBY;QACxB,OAAO;YACNnB,OAAOc,MACN;QAEF;IACD;IAEA,OAAOM,MAAM;QACZ,OAAOvB,aAAauB,QAAQ;IAC7B;IAEA,IAAIA,MAAM;QACT,OAAOf,YAAYe;IACpB;IAEA,IAAIC,UAAU;QACb,OAAO;YACNC,UACCzB,aAAa0B,iBAAiB,IAAI,CAACf,YAAY,CAAC,EAAE,EAAEc,YACpDH;QACF;IACD;AACD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a random number to append to an `id` string.
|
|
3
|
+
*
|
|
4
|
+
* NOTE: we are still using the good old lodash uniqueId when
|
|
5
|
+
* the code is not in production, so that the results are a
|
|
6
|
+
* little bit more consistent tests after test instead of
|
|
7
|
+
* being completely random, so that they do not break
|
|
8
|
+
* potential snapshot tests.
|
|
9
|
+
*
|
|
10
|
+
* @param {String} prefix - When a prefix is provided, the
|
|
11
|
+
* function will return a random
|
|
12
|
+
* number appended to the provided
|
|
13
|
+
* prefix.
|
|
14
|
+
*
|
|
15
|
+
* @returns {String} - Returns a string with random numbers.
|
|
16
|
+
*/
|
|
17
|
+
export declare const uniqueID: (prefix?: string) => string;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
/**
|
|
3
|
+
* Generate a random number to append to an `id` string.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: we are still using the good old lodash uniqueId when
|
|
6
|
+
* the code is not in production, so that the results are a
|
|
7
|
+
* little bit more consistent tests after test instead of
|
|
8
|
+
* being completely random, so that they do not break
|
|
9
|
+
* potential snapshot tests.
|
|
10
|
+
*
|
|
11
|
+
* @param {String} prefix - When a prefix is provided, the
|
|
12
|
+
* function will return a random
|
|
13
|
+
* number appended to the provided
|
|
14
|
+
* prefix.
|
|
15
|
+
*
|
|
16
|
+
* @returns {String} - Returns a string with random numbers.
|
|
17
|
+
*/ export const uniqueID = (prefix)=>{
|
|
18
|
+
if (process.env.NODE_ENV !== "production") {
|
|
19
|
+
return _.uniqueId(prefix);
|
|
20
|
+
}
|
|
21
|
+
// Extract the decimal part
|
|
22
|
+
const randomNumber = `${Math.random()}`.split(".")[1];
|
|
23
|
+
return prefix ? `${prefix}${randomNumber}` : randomNumber;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=unique-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/unique-id.ts"],"sourcesContent":["import _ from \"lodash\";\n\n/**\n * Generate a random number to append to an `id` string.\n *\n * NOTE: we are still using the good old lodash uniqueId when\n * the code is not in production, so that the results are a\n * little bit more consistent tests after test instead of\n * being completely random, so that they do not break\n * potential snapshot tests.\n *\n * @param {String} prefix - When a prefix is provided, the\n * function will return a random\n * number appended to the provided\n * prefix.\n *\n * @returns {String} - Returns a string with random numbers.\n */\nexport const uniqueID = (prefix?: string): string => {\n\tif (process.env.NODE_ENV !== \"production\") {\n\t\treturn _.uniqueId(prefix);\n\t}\n\t// Extract the decimal part\n\tconst randomNumber = `${Math.random()}`.split(\".\")[1];\n\treturn prefix ? `${prefix}${randomNumber}` : randomNumber;\n};\n"],"names":["_","uniqueID","prefix","process","env","NODE_ENV","uniqueId","randomNumber","Math","random","split"],"mappings":"AAAA,OAAOA,OAAO,SAAS;AAEvB;;;;;;;;;;;;;;;CAeC,GACD,OAAO,MAAMC,WAAW,CAACC;IACxB,IAAIC,QAAQC,IAAIC,aAAa,cAAc;QAC1C,OAAOL,EAAEM,SAASJ;IACnB;IACA,2BAA2B;IAC3B,MAAMK,eAAe,CAAC,EAAEC,KAAKC,SAAS,CAAC,CAACC,MAAM,IAAI,CAAC,EAAE;IACrD,OAAOR,SAAS,CAAC,EAAEA,OAAO,EAAEK,aAAa,CAAC,GAAGA;AAC9C,EAAE"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@node-cli/perf",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Arno Versini",
|
|
6
|
+
"description": "Set of performance tools for Node.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": "./dist/index.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"node": ">=16",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@node-cli/logger": ">=1.0.0",
|
|
15
|
+
"lodash": "4.17.21"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "yarn run clean && yarn run build:types && yarn run build:js && yarn run build:barrel",
|
|
19
|
+
"build:barrel": "barrelsby --delete --directory dist --pattern \"**/*.d.ts\" --name \"index.d\"",
|
|
20
|
+
"build:js": "swc --source-maps --out-dir dist src",
|
|
21
|
+
"build:types": "tsc",
|
|
22
|
+
"clean": "rimraf dist types coverage",
|
|
23
|
+
"lint": "prettier --write \"src/*.ts\" && eslint --fix \"src/*.ts\"",
|
|
24
|
+
"test": "cross-env-shell NODE_OPTIONS=--experimental-vm-modules jest",
|
|
25
|
+
"test:coverage": "npm run test -- --coverage",
|
|
26
|
+
"watch": "swc --watch --out-dir dist src"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "231e9a91b395b50b9ddb0b4fe6f8663a50f141a5"
|
|
32
|
+
}
|