@bemoje/profiler 2.2.1 → 2.2.3

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/lib/index.d.cts CHANGED
@@ -1,2 +1,161 @@
1
- export { Profiler } from './Profiler/Profiler.cjs';
2
- import 'util';
1
+ import { inspect, InspectOptions } from 'util';
2
+
3
+ type Any = any;
4
+
5
+ type TFunction<R = Any, A extends Any[] = Any[]> = (...args: A) => R;
6
+
7
+ type FunctionPrototype = typeof Function.prototype;
8
+
9
+ type TargetType = 'static' | 'prototype' | 'module' | 'standalone';
10
+ type FunctionType = 'get' | 'set' | 'method' | 'function';
11
+
12
+ /**
13
+ * Abstract base class for identifying and naming functions in profiling contexts.
14
+ */
15
+ declare abstract class AbstractFunctionIdentifier<T extends FunctionPrototype | object> {
16
+ readonly target: T;
17
+ abstract readonly targetType: TargetType;
18
+ abstract readonly type: FunctionType;
19
+ abstract readonly name: string;
20
+ constructor(target: T);
21
+ toJSON(): string;
22
+ [inspect.custom](): string;
23
+ }
24
+
25
+ /**
26
+ * @see functionSpy
27
+ */
28
+ interface IFunctionSpyStrategy<T, Data> {
29
+ /**
30
+ * Action to perform before the function is invoked.
31
+ * @param thisContext The context in which the function is invoked.
32
+ * @param args The arguments passed to the function. Modify this array to change the arguments passed to the function.
33
+ * @returns Data to be passed to @see onReturn.
34
+ */
35
+ onInvoke<Args extends Any[]>(thisContext: T, args: Args): Data;
36
+ /**
37
+ * Action to perform before the function returns.
38
+ * @param data The data returned by @see onInvoke.
39
+ * @param retval The return value of the function.
40
+ * @returns The value to be returned by the wrapped function.
41
+ */
42
+ onReturn<Ret>(data: Data, retval: Ret): Ret;
43
+ }
44
+
45
+ /**
46
+ * Profiler for functions, measuring execution time and call counts.
47
+ */
48
+ declare class FunctionProfiler<T extends object> implements IFunctionSpyStrategy<T, bigint> {
49
+ readonly id: AbstractFunctionIdentifier<T>;
50
+ static instances: FunctionProfiler<object>[];
51
+ calls: number;
52
+ totalTimeNs?: number;
53
+ minTimeNs?: number;
54
+ maxTimeNs?: number;
55
+ constructor(id: AbstractFunctionIdentifier<T>);
56
+ /**
57
+ * Average execution time in nanoseconds
58
+ */
59
+ get avgTimeNs(): number | undefined;
60
+ /**
61
+ * Get the result of the profiling
62
+ */
63
+ getResult(): FunctionProfilerResult;
64
+ /**
65
+ * @see IFunctionSpyStrategy.onInvoke
66
+ */
67
+ onInvoke(): bigint;
68
+ /**
69
+ * @see IFunctionSpyStrategy.onReturn
70
+ */
71
+ onReturn<Ret>(t0: bigint, retval: Ret): Ret;
72
+ /**
73
+ * Add a new time measurement
74
+ */
75
+ protected add(t0: bigint, t1: bigint): void;
76
+ /**
77
+ * Nanoseconds to microseconds
78
+ */
79
+ protected nsToUs(ns: number): number;
80
+ [inspect.custom](): FunctionProfilerResult;
81
+ toJSON(): FunctionProfilerResult;
82
+ }
83
+ type FunctionProfilerResult = {
84
+ /**
85
+ * Number of method calls
86
+ */
87
+ calls: number;
88
+ /**
89
+ * Total execution time in microseconds
90
+ */
91
+ totalTimeUs?: number;
92
+ /**
93
+ * Total execution time in microseconds
94
+ */
95
+ avgTimeUs?: number;
96
+ /**
97
+ * Fastest execution time in microseconds
98
+ */
99
+ minTimeUs?: number;
100
+ /**
101
+ * Slowest execution time in microseconds
102
+ */
103
+ maxTimeUs?: number;
104
+ };
105
+
106
+ /**
107
+ * A utility class for profiling functions, classes, and modules to measure execution time and other performance metrics.
108
+ *
109
+ * @remarks
110
+ * The Profiler provides various static methods to wrap functions, classes, and modules with profiling functionality.
111
+ * This allows for detailed performance monitoring, including metrics like total execution time, average execution time,
112
+ * minimum and maximum execution times, and call counts.
113
+ *
114
+ * Profiling can be enabled or disabled globally using the `enabled` static property.
115
+ */
116
+ declare class Profiler {
117
+ static enabled: boolean;
118
+ static get data(): FunctionProfiler<object>[];
119
+ /**
120
+ * Class profiling
121
+ */
122
+ static class<T extends FunctionPrototype & {
123
+ profiler?: ProfilerClassOptions;
124
+ }>(target: T, options?: ProfilerClassOptions): T;
125
+ /**
126
+ * Class profiling decorator
127
+ */
128
+ static class<T extends FunctionPrototype & {
129
+ profiler?: ProfilerClassOptions;
130
+ }>(options: ProfilerClassOptions): (target: T) => T;
131
+ static classStatic<T extends FunctionPrototype & {
132
+ profiler?: ProfilerClassOptions;
133
+ }>(target: T, ignoreKeys?: Iterable<string | symbol>): T;
134
+ static classPrototype<T extends FunctionPrototype & {
135
+ profiler?: ProfilerClassOptions;
136
+ }>(target: T, ignoreKeys?: Iterable<string | symbol>): T;
137
+ static module<T extends object>(target: T, moduleName: string, ignoreKeys?: Iterable<string | symbol>): T;
138
+ static fn<T extends TFunction>(func: T): T;
139
+ static fn<T extends TFunction>(name: string, func: T): T;
140
+ static getResults(options?: GetProfilerResultsOptions): ProfilerResultReadableEntry[];
141
+ static printResults(options?: PrintProfilerResultsOptions): void;
142
+ static [inspect.custom](depth?: number, options?: InspectOptions): {
143
+ [k: string]: FunctionProfilerResult;
144
+ };
145
+ static toJSON(): ProfilerResultReadableEntry[];
146
+ }
147
+ type ProfilerClassOptions = {
148
+ ignoreStaticKeys?: Iterable<string | symbol>;
149
+ ignorePrototypeKeys?: Iterable<string | symbol>;
150
+ };
151
+ type GetProfilerResultsOptions = {
152
+ sortBy?: keyof FunctionProfilerResult;
153
+ };
154
+ type PrintProfilerResultsOptions = {
155
+ sortBy?: keyof FunctionProfilerResult;
156
+ update?: (entries: ProfilerResultReadableEntry[]) => ProfilerResultReadableEntry[];
157
+ noColor?: boolean;
158
+ };
159
+ type ProfilerResultReadableEntry = [string, FunctionProfilerResult];
160
+
161
+ export { Profiler };
package/lib/index.d.ts CHANGED
@@ -1,2 +1,161 @@
1
- export { Profiler } from './Profiler/Profiler.js';
2
- import 'util';
1
+ import { inspect, InspectOptions } from 'util';
2
+
3
+ type Any = any;
4
+
5
+ type TFunction<R = Any, A extends Any[] = Any[]> = (...args: A) => R;
6
+
7
+ type FunctionPrototype = typeof Function.prototype;
8
+
9
+ type TargetType = 'static' | 'prototype' | 'module' | 'standalone';
10
+ type FunctionType = 'get' | 'set' | 'method' | 'function';
11
+
12
+ /**
13
+ * Abstract base class for identifying and naming functions in profiling contexts.
14
+ */
15
+ declare abstract class AbstractFunctionIdentifier<T extends FunctionPrototype | object> {
16
+ readonly target: T;
17
+ abstract readonly targetType: TargetType;
18
+ abstract readonly type: FunctionType;
19
+ abstract readonly name: string;
20
+ constructor(target: T);
21
+ toJSON(): string;
22
+ [inspect.custom](): string;
23
+ }
24
+
25
+ /**
26
+ * @see functionSpy
27
+ */
28
+ interface IFunctionSpyStrategy<T, Data> {
29
+ /**
30
+ * Action to perform before the function is invoked.
31
+ * @param thisContext The context in which the function is invoked.
32
+ * @param args The arguments passed to the function. Modify this array to change the arguments passed to the function.
33
+ * @returns Data to be passed to @see onReturn.
34
+ */
35
+ onInvoke<Args extends Any[]>(thisContext: T, args: Args): Data;
36
+ /**
37
+ * Action to perform before the function returns.
38
+ * @param data The data returned by @see onInvoke.
39
+ * @param retval The return value of the function.
40
+ * @returns The value to be returned by the wrapped function.
41
+ */
42
+ onReturn<Ret>(data: Data, retval: Ret): Ret;
43
+ }
44
+
45
+ /**
46
+ * Profiler for functions, measuring execution time and call counts.
47
+ */
48
+ declare class FunctionProfiler<T extends object> implements IFunctionSpyStrategy<T, bigint> {
49
+ readonly id: AbstractFunctionIdentifier<T>;
50
+ static instances: FunctionProfiler<object>[];
51
+ calls: number;
52
+ totalTimeNs?: number;
53
+ minTimeNs?: number;
54
+ maxTimeNs?: number;
55
+ constructor(id: AbstractFunctionIdentifier<T>);
56
+ /**
57
+ * Average execution time in nanoseconds
58
+ */
59
+ get avgTimeNs(): number | undefined;
60
+ /**
61
+ * Get the result of the profiling
62
+ */
63
+ getResult(): FunctionProfilerResult;
64
+ /**
65
+ * @see IFunctionSpyStrategy.onInvoke
66
+ */
67
+ onInvoke(): bigint;
68
+ /**
69
+ * @see IFunctionSpyStrategy.onReturn
70
+ */
71
+ onReturn<Ret>(t0: bigint, retval: Ret): Ret;
72
+ /**
73
+ * Add a new time measurement
74
+ */
75
+ protected add(t0: bigint, t1: bigint): void;
76
+ /**
77
+ * Nanoseconds to microseconds
78
+ */
79
+ protected nsToUs(ns: number): number;
80
+ [inspect.custom](): FunctionProfilerResult;
81
+ toJSON(): FunctionProfilerResult;
82
+ }
83
+ type FunctionProfilerResult = {
84
+ /**
85
+ * Number of method calls
86
+ */
87
+ calls: number;
88
+ /**
89
+ * Total execution time in microseconds
90
+ */
91
+ totalTimeUs?: number;
92
+ /**
93
+ * Total execution time in microseconds
94
+ */
95
+ avgTimeUs?: number;
96
+ /**
97
+ * Fastest execution time in microseconds
98
+ */
99
+ minTimeUs?: number;
100
+ /**
101
+ * Slowest execution time in microseconds
102
+ */
103
+ maxTimeUs?: number;
104
+ };
105
+
106
+ /**
107
+ * A utility class for profiling functions, classes, and modules to measure execution time and other performance metrics.
108
+ *
109
+ * @remarks
110
+ * The Profiler provides various static methods to wrap functions, classes, and modules with profiling functionality.
111
+ * This allows for detailed performance monitoring, including metrics like total execution time, average execution time,
112
+ * minimum and maximum execution times, and call counts.
113
+ *
114
+ * Profiling can be enabled or disabled globally using the `enabled` static property.
115
+ */
116
+ declare class Profiler {
117
+ static enabled: boolean;
118
+ static get data(): FunctionProfiler<object>[];
119
+ /**
120
+ * Class profiling
121
+ */
122
+ static class<T extends FunctionPrototype & {
123
+ profiler?: ProfilerClassOptions;
124
+ }>(target: T, options?: ProfilerClassOptions): T;
125
+ /**
126
+ * Class profiling decorator
127
+ */
128
+ static class<T extends FunctionPrototype & {
129
+ profiler?: ProfilerClassOptions;
130
+ }>(options: ProfilerClassOptions): (target: T) => T;
131
+ static classStatic<T extends FunctionPrototype & {
132
+ profiler?: ProfilerClassOptions;
133
+ }>(target: T, ignoreKeys?: Iterable<string | symbol>): T;
134
+ static classPrototype<T extends FunctionPrototype & {
135
+ profiler?: ProfilerClassOptions;
136
+ }>(target: T, ignoreKeys?: Iterable<string | symbol>): T;
137
+ static module<T extends object>(target: T, moduleName: string, ignoreKeys?: Iterable<string | symbol>): T;
138
+ static fn<T extends TFunction>(func: T): T;
139
+ static fn<T extends TFunction>(name: string, func: T): T;
140
+ static getResults(options?: GetProfilerResultsOptions): ProfilerResultReadableEntry[];
141
+ static printResults(options?: PrintProfilerResultsOptions): void;
142
+ static [inspect.custom](depth?: number, options?: InspectOptions): {
143
+ [k: string]: FunctionProfilerResult;
144
+ };
145
+ static toJSON(): ProfilerResultReadableEntry[];
146
+ }
147
+ type ProfilerClassOptions = {
148
+ ignoreStaticKeys?: Iterable<string | symbol>;
149
+ ignorePrototypeKeys?: Iterable<string | symbol>;
150
+ };
151
+ type GetProfilerResultsOptions = {
152
+ sortBy?: keyof FunctionProfilerResult;
153
+ };
154
+ type PrintProfilerResultsOptions = {
155
+ sortBy?: keyof FunctionProfilerResult;
156
+ update?: (entries: ProfilerResultReadableEntry[]) => ProfilerResultReadableEntry[];
157
+ noColor?: boolean;
158
+ };
159
+ type ProfilerResultReadableEntry = [string, FunctionProfilerResult];
160
+
161
+ export { Profiler };