@searchspring/snap-profiler 0.20.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Searchspring
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,148 @@
1
+ # Snap Profiler
2
+
3
+ <a href="https://www.npmjs.com/package/@searchspring/snap-profiler"><img alt="NPM Status" src="https://img.shields.io/npm/v/@searchspring/snap-profiler.svg?style=flat"></a>
4
+
5
+ A utility for recording how long something takes to complete. `Profiler` is used in finding API response, component rendering and Middleware execution times.
6
+
7
+
8
+ ## Dependency
9
+
10
+ Snap Profiler is a dependency of [@searchspring/snap-controller](https://github.com/searchspring/snap/tree/main/packages/snap-controller) <a href="https://www.npmjs.com/package/@searchspring/snap-controller"><img alt="NPM Status" src="https://img.shields.io/npm/v/@searchspring/snap-controller.svg?style=flat"></a>
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install --save @searchspring/snap-profiler
16
+ ```
17
+
18
+ ## Import
19
+ ```typescript
20
+ import { Profiler } from '@searchspring/snap-profiler';
21
+ ```
22
+
23
+
24
+ <h2 id="Profiler">Profiler</h2>
25
+ An optional `namespace` can be passed to the Profiler constructor for profile organization.
26
+
27
+ ```typescript
28
+ import { Profiler } from '@searchspring/snap-profiler';
29
+
30
+ const profiler = new Profiler('namespace');
31
+ ```
32
+
33
+ ### `setNamespace` method
34
+ Programatically set the namespace after construction.
35
+
36
+ ```typescript
37
+ import { Profiler } from '@searchspring/snap-profiler';
38
+
39
+ const profiler = new Profiler();
40
+
41
+ profiler.setNamespace('namespace');
42
+ ```
43
+
44
+ ### `create` method
45
+ Create a new profile.
46
+
47
+ ```typescript
48
+ import { Profiler } from '@searchspring/snap-profiler';
49
+
50
+ const profiler = new Profiler();
51
+
52
+ const searchProfile = profiler.create({
53
+ type: 'event',
54
+ name: 'search',
55
+ context: params
56
+ }: ProfileDetails);
57
+ ```
58
+
59
+ ```typescript
60
+ type ProfileDetails<T> = {
61
+ type: string;
62
+ name: string;
63
+ context: T;
64
+ }
65
+ ```
66
+
67
+ Returns an instance of `Profile`.
68
+
69
+
70
+ <h2 id="Profile">Profile</h2>
71
+
72
+ `Profile` is not an exported member of the Snap Profiler package. It is only returned in the Profiler `create` method.
73
+
74
+ ### `start` method
75
+
76
+ This will start the profiler timer.
77
+
78
+ ```typescript
79
+ searchProfile.start();
80
+ ```
81
+
82
+ ### `stop` method
83
+ This will stop the profiler timer.
84
+
85
+ ```typescript
86
+ searchProfile.stop();
87
+ ```
88
+
89
+ ### `namespace` property
90
+ Profile namespace that was set using the `Profiler` constructor or the `setNamespace` method.
91
+
92
+ ```typescript
93
+ console.log(`namespace: ${searchProfile.namespace}`);
94
+ ```
95
+
96
+ ### `type` property
97
+ Profile type that was set in the `create` method `ProfileDetails` parameters.
98
+
99
+ ```typescript
100
+ console.log(`type: ${searchProfile.type}`);
101
+ ```
102
+
103
+ ### `name` property
104
+ Profile name that was set in the `create` method `ProfileDetails` parameters.
105
+
106
+ ```typescript
107
+ console.log(`name: ${searchProfile.name}`);
108
+ ```
109
+
110
+ ### `context` property
111
+ Profile context that was set in the `create` method `ProfileDetails` parameters. The context is used to provide additional details regarding the profile. A search profile would likely contain the request parameters amoung other things.
112
+
113
+ ```typescript
114
+ console.log(`context: ${searchProfile.context}`);
115
+ ```
116
+
117
+ ### `status` property
118
+ Profile status. The default value is `pending`.
119
+
120
+ The value will change to `started` when the `start` method is invoked and to `finished` when the `stop` method is invoked.
121
+
122
+ ```typescript
123
+ console.log(`context: ${searchProfile.status}`);
124
+ ```
125
+
126
+ ### `time` property
127
+ Profile time object is of type `ProfileTime`:
128
+
129
+ ```typescript
130
+ type ProfileTime = {
131
+ date: number;
132
+ begin: number;
133
+ end: number;
134
+ run: number;
135
+ };
136
+ ```
137
+
138
+ `ProfileTime.date` - set to `Date.now()` when `start` method is invoked.
139
+
140
+ `ProfileTime.begin` - set to `window.performance.now()` when `start` method is invoked.
141
+
142
+ `ProfileTime.end` - set to `window.performance.now()` when `stop` method is invoked.
143
+
144
+ `ProfileTime.run` - set to the total running time in milliseconds between when the `start` and `stop` methods have been invoked.
145
+
146
+
147
+ ## Logging profiles
148
+ It is recommended to using the Snap Logger's `profile` method to log Snap Profiles as it provides a clean output for easy parsing.
@@ -0,0 +1,31 @@
1
+ declare type ProfileDetails<T> = {
2
+ type: string;
3
+ name: string;
4
+ context: T;
5
+ };
6
+ export declare class Profiler {
7
+ namespace: string;
8
+ profiles: Profile<any>[];
9
+ constructor(namespace?: string);
10
+ setNamespace(namespace: string): void;
11
+ create<T>({ type, name, context }: ProfileDetails<T>): Profile<T>;
12
+ }
13
+ declare type ProfileTime = {
14
+ date: number;
15
+ begin: number;
16
+ end: number;
17
+ run: number;
18
+ };
19
+ declare class Profile<T> {
20
+ namespace: string;
21
+ type: string;
22
+ name: string;
23
+ context: T;
24
+ status: string;
25
+ time: ProfileTime;
26
+ constructor(namespace: string, { type, name, context }: ProfileDetails<T>);
27
+ start(): Profile<T>;
28
+ stop(): Profile<T>;
29
+ }
30
+ export {};
31
+ //# sourceMappingURL=Profiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Profiler.d.ts","sourceRoot":"","sources":["../../src/Profiler.ts"],"names":[],"mappings":"AAAA,aAAK,cAAc,CAAC,CAAC,IAAI;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,CAAC,CAAA;CAAE,CAAC;AAEpE,qBAAa,QAAQ;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBAEpB,SAAS,CAAC,EAAE,MAAM;IAKvB,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAMrC,MAAM,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAUxE;AAED,aAAK,WAAW,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,cAAM,OAAO,CAAC,CAAC;IACP,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,CAAC,CAAC;IACX,MAAM,SAAa;IACnB,IAAI,EAAE,WAAW,CAKtB;gBAEU,SAAS,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC;IAOlE,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC;IAWnB,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC;CAWzB"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Profiler = void 0;
4
+ var Profiler = /** @class */ (function () {
5
+ function Profiler(namespace) {
6
+ this.namespace = namespace;
7
+ this.profiles = [];
8
+ }
9
+ Profiler.prototype.setNamespace = function (namespace) {
10
+ if (!this.namespace) {
11
+ this.namespace = namespace;
12
+ }
13
+ };
14
+ Profiler.prototype.create = function (_a) {
15
+ var type = _a.type, name = _a.name, context = _a.context;
16
+ if (!name) {
17
+ throw new Error('Profile name is required.');
18
+ }
19
+ var profile = new Profile(this.namespace, { type: type, name: name, context: context });
20
+ this.profiles.push(profile);
21
+ return profile;
22
+ };
23
+ return Profiler;
24
+ }());
25
+ exports.Profiler = Profiler;
26
+ var Profile = /** @class */ (function () {
27
+ function Profile(namespace, _a) {
28
+ var type = _a.type, name = _a.name, context = _a.context;
29
+ this.status = 'pending';
30
+ this.time = {
31
+ date: undefined,
32
+ begin: undefined,
33
+ end: undefined,
34
+ run: undefined,
35
+ };
36
+ this.namespace = namespace;
37
+ this.type = type;
38
+ this.name = name;
39
+ this.context = context;
40
+ }
41
+ Profile.prototype.start = function () {
42
+ if (!this.time.begin) {
43
+ this.time.date = Date.now();
44
+ this.time.begin = window.performance.now();
45
+ this.status = 'started';
46
+ }
47
+ return this;
48
+ };
49
+ Profile.prototype.stop = function () {
50
+ if (!this.time.end) {
51
+ this.time.date = Date.now();
52
+ this.time.end = window.performance.now();
53
+ this.time.run = +(this.time.end - this.time.begin).toFixed(3);
54
+ this.status = 'finished';
55
+ }
56
+ return this;
57
+ };
58
+ return Profile;
59
+ }());
@@ -0,0 +1,2 @@
1
+ export { Profiler } from './Profiler';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Profiler = void 0;
4
+ var Profiler_1 = require("./Profiler");
5
+ Object.defineProperty(exports, "Profiler", { enumerable: true, get: function () { return Profiler_1.Profiler; } });
@@ -0,0 +1,31 @@
1
+ declare type ProfileDetails<T> = {
2
+ type: string;
3
+ name: string;
4
+ context: T;
5
+ };
6
+ export declare class Profiler {
7
+ namespace: string;
8
+ profiles: Profile<any>[];
9
+ constructor(namespace?: string);
10
+ setNamespace(namespace: string): void;
11
+ create<T>({ type, name, context }: ProfileDetails<T>): Profile<T>;
12
+ }
13
+ declare type ProfileTime = {
14
+ date: number;
15
+ begin: number;
16
+ end: number;
17
+ run: number;
18
+ };
19
+ declare class Profile<T> {
20
+ namespace: string;
21
+ type: string;
22
+ name: string;
23
+ context: T;
24
+ status: string;
25
+ time: ProfileTime;
26
+ constructor(namespace: string, { type, name, context }: ProfileDetails<T>);
27
+ start(): Profile<T>;
28
+ stop(): Profile<T>;
29
+ }
30
+ export {};
31
+ //# sourceMappingURL=Profiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Profiler.d.ts","sourceRoot":"","sources":["../../src/Profiler.ts"],"names":[],"mappings":"AAAA,aAAK,cAAc,CAAC,CAAC,IAAI;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,CAAC,CAAA;CAAE,CAAC;AAEpE,qBAAa,QAAQ;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBAEpB,SAAS,CAAC,EAAE,MAAM;IAKvB,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAMrC,MAAM,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAUxE;AAED,aAAK,WAAW,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,cAAM,OAAO,CAAC,CAAC;IACP,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,CAAC,CAAC;IACX,MAAM,SAAa;IACnB,IAAI,EAAE,WAAW,CAKtB;gBAEU,SAAS,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC;IAOlE,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC;IAWnB,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC;CAWzB"}
@@ -0,0 +1,51 @@
1
+ export class Profiler {
2
+ constructor(namespace) {
3
+ this.namespace = namespace;
4
+ this.profiles = [];
5
+ }
6
+ setNamespace(namespace) {
7
+ if (!this.namespace) {
8
+ this.namespace = namespace;
9
+ }
10
+ }
11
+ create({ type, name, context }) {
12
+ if (!name) {
13
+ throw new Error('Profile name is required.');
14
+ }
15
+ const profile = new Profile(this.namespace, { type, name, context });
16
+ this.profiles.push(profile);
17
+ return profile;
18
+ }
19
+ }
20
+ class Profile {
21
+ constructor(namespace, { type, name, context }) {
22
+ this.status = 'pending';
23
+ this.time = {
24
+ date: undefined,
25
+ begin: undefined,
26
+ end: undefined,
27
+ run: undefined,
28
+ };
29
+ this.namespace = namespace;
30
+ this.type = type;
31
+ this.name = name;
32
+ this.context = context;
33
+ }
34
+ start() {
35
+ if (!this.time.begin) {
36
+ this.time.date = Date.now();
37
+ this.time.begin = window.performance.now();
38
+ this.status = 'started';
39
+ }
40
+ return this;
41
+ }
42
+ stop() {
43
+ if (!this.time.end) {
44
+ this.time.date = Date.now();
45
+ this.time.end = window.performance.now();
46
+ this.time.run = +(this.time.end - this.time.begin).toFixed(3);
47
+ this.status = 'finished';
48
+ }
49
+ return this;
50
+ }
51
+ }
@@ -0,0 +1,2 @@
1
+ export { Profiler } from './Profiler';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1 @@
1
+ export { Profiler } from './Profiler';
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@searchspring/snap-profiler",
3
+ "version": "0.20.0",
4
+ "description": "Snap Profiler",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "author": "Searchspring",
8
+ "license": "MIT",
9
+ "repository": "https://github.com/searchspring/snap",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "build": "rm -rf ./dist && tsc && tsc -p tsconfig.cjs.json",
15
+ "build:docs": "typedoc --out docs src/index.ts",
16
+ "dev": "tsc --watch",
17
+ "format": "prettier --write 'src/**/*.{js,jsx,ts,tsx}'",
18
+ "lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
19
+ "test": "jest",
20
+ "test:watch": "jest --watch"
21
+ },
22
+ "sideEffects": false,
23
+ "files": [
24
+ "dist/**/*"
25
+ ],
26
+ "gitHead": "122405b27b497c7bb6a189c30535fdc197bc3ef0"
27
+ }