@atlaspack/profiler 2.14.15-typescript-d6e6d169c.0 → 2.14.15-typescript-6de04fbae.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/lib/{SamplingProfiler.d.ts → SamplingProfiler.d.mts} +1 -1
- package/lib/SamplingProfiler.js +100 -58
- package/lib/SamplingProfiler.js.flow +47 -0
- package/lib/SamplingProfiler.mjs +51 -0
- package/lib/{Trace.d.ts → Trace.d.mts} +3 -3
- package/lib/Trace.js +126 -112
- package/lib/Trace.js.flow +15 -0
- package/lib/Trace.mjs +111 -0
- package/lib/{Tracer.d.ts → Tracer.d.mts} +3 -3
- package/lib/Tracer.js +112 -114
- package/lib/Tracer.js.flow +46 -0
- package/lib/Tracer.mjs +114 -0
- package/lib/index.d.mts +4 -0
- package/lib/index.js +22 -22
- package/lib/index.mjs +3 -0
- package/lib/types.js +4 -1
- package/lib/types.mjs +1 -0
- package/package.json +21 -9
- package/src/SamplingProfiler.mts +94 -0
- package/src/{Trace.js → Trace.mts} +5 -9
- package/src/{Tracer.js → Tracer.mts} +28 -23
- package/src/index.mts +4 -0
- package/src/types.mts +6 -0
- package/test/{Tracer.test.js → Tracer.test.mts} +9 -4
- package/tsconfig.json +4 -0
- package/build-ts.js +0 -8
- package/lib/index.d.ts +0 -4
- package/src/SamplingProfiler.js +0 -93
- /package/{src/index.js → lib/index.js.flow} +0 -0
- /package/lib/{types.d.ts → types.d.mts} +0 -0
- /package/{src/types.js → lib/types.js.flow} +0 -0
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
// @flow strict-local
|
|
2
|
-
|
|
3
1
|
import type {
|
|
4
2
|
TraceEvent,
|
|
5
3
|
IDisposable,
|
|
6
4
|
PluginTracer as IPluginTracer,
|
|
7
|
-
} from '@atlaspack/types';
|
|
5
|
+
} from '@atlaspack/types-internal';
|
|
8
6
|
import type {
|
|
9
7
|
TraceMeasurement as ITraceMeasurement,
|
|
10
8
|
TraceMeasurementData,
|
|
11
|
-
} from './types';
|
|
12
|
-
// @ts-ignore
|
|
9
|
+
} from './types.mts';
|
|
13
10
|
import {ValueEmitter} from '@atlaspack/events';
|
|
14
|
-
|
|
15
11
|
import {performance} from 'perf_hooks';
|
|
16
12
|
|
|
17
|
-
let tid;
|
|
13
|
+
let tid: number;
|
|
14
|
+
|
|
18
15
|
try {
|
|
19
16
|
tid = require('worker_threads').threadId;
|
|
20
17
|
} catch {
|
|
@@ -31,7 +28,14 @@ class TraceMeasurement implements ITraceMeasurement {
|
|
|
31
28
|
#start: number;
|
|
32
29
|
// $FlowFixMe
|
|
33
30
|
#data: any;
|
|
34
|
-
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
tracer: Tracer,
|
|
34
|
+
name: string,
|
|
35
|
+
pid: number,
|
|
36
|
+
tid: number,
|
|
37
|
+
data: any,
|
|
38
|
+
) {
|
|
35
39
|
this.#name = name;
|
|
36
40
|
this.#pid = pid;
|
|
37
41
|
this.#tid = tid;
|
|
@@ -57,15 +61,15 @@ class TraceMeasurement implements ITraceMeasurement {
|
|
|
57
61
|
|
|
58
62
|
export default class Tracer {
|
|
59
63
|
#traceEmitter: ValueEmitter<TraceEvent> = new ValueEmitter();
|
|
60
|
-
|
|
61
64
|
#enabled: boolean = false;
|
|
62
65
|
|
|
63
|
-
onTrace(cb: (event: TraceEvent) =>
|
|
66
|
+
onTrace(cb: (event: TraceEvent) => unknown): IDisposable {
|
|
64
67
|
return this.#traceEmitter.addListener(cb);
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
async wrap(name: string, fn: () =>
|
|
70
|
+
async wrap(name: string, fn: () => unknown): Promise<void> {
|
|
68
71
|
let measurement = this.createMeasurement(name);
|
|
72
|
+
|
|
69
73
|
try {
|
|
70
74
|
await fn();
|
|
71
75
|
} finally {
|
|
@@ -77,20 +81,24 @@ export default class Tracer {
|
|
|
77
81
|
name: string,
|
|
78
82
|
category: string = 'Core',
|
|
79
83
|
argumentName?: string,
|
|
80
|
-
otherArgs?:
|
|
84
|
+
otherArgs?: Record<string, unknown>,
|
|
81
85
|
): ITraceMeasurement | null {
|
|
82
86
|
if (!this.enabled) return null;
|
|
83
|
-
|
|
84
87
|
// We create `args` in a fairly verbose way to avoid object
|
|
85
88
|
// allocation where not required.
|
|
86
|
-
let args:
|
|
89
|
+
let args: Record<string, unknown> | undefined;
|
|
90
|
+
|
|
87
91
|
if (typeof argumentName === 'string') {
|
|
88
|
-
args = {
|
|
92
|
+
args = {
|
|
93
|
+
name: argumentName,
|
|
94
|
+
};
|
|
89
95
|
}
|
|
96
|
+
|
|
90
97
|
if (typeof otherArgs === 'object') {
|
|
91
98
|
if (typeof args == 'undefined') {
|
|
92
99
|
args = {};
|
|
93
100
|
}
|
|
101
|
+
|
|
94
102
|
for (const [k, v] of Object.entries(otherArgs)) {
|
|
95
103
|
args[k] = v;
|
|
96
104
|
}
|
|
@@ -100,7 +108,6 @@ export default class Tracer {
|
|
|
100
108
|
categories: [category],
|
|
101
109
|
args,
|
|
102
110
|
};
|
|
103
|
-
|
|
104
111
|
return new TraceMeasurement(this, name, pid, tid, data);
|
|
105
112
|
}
|
|
106
113
|
|
|
@@ -121,13 +128,11 @@ export default class Tracer {
|
|
|
121
128
|
this.#traceEmitter.emit(event);
|
|
122
129
|
}
|
|
123
130
|
}
|
|
124
|
-
|
|
125
131
|
export const tracer: Tracer = new Tracer();
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|};
|
|
132
|
+
type TracerOpts = {
|
|
133
|
+
origin: string;
|
|
134
|
+
category: string;
|
|
135
|
+
};
|
|
131
136
|
export class PluginTracer implements IPluginTracer {
|
|
132
137
|
/** @private */
|
|
133
138
|
origin: string;
|
|
@@ -149,7 +154,7 @@ export class PluginTracer implements IPluginTracer {
|
|
|
149
154
|
name: string,
|
|
150
155
|
category?: string,
|
|
151
156
|
argumentName?: string,
|
|
152
|
-
otherArgs?:
|
|
157
|
+
otherArgs?: Record<string, unknown>,
|
|
153
158
|
): ITraceMeasurement | null {
|
|
154
159
|
return tracer.createMeasurement(
|
|
155
160
|
name,
|
package/src/index.mts
ADDED
package/src/types.mts
ADDED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
import {tracer, PluginTracer} from '../src/Tracer';
|
|
1
|
+
import type {IDisposable} from '@atlaspack/types-internal';
|
|
2
|
+
import {tracer, PluginTracer} from '../src/Tracer.mts';
|
|
3
3
|
import sinon from 'sinon';
|
|
4
4
|
import assert from 'assert';
|
|
5
5
|
|
|
6
6
|
describe('Tracer', () => {
|
|
7
|
-
let onTrace;
|
|
8
|
-
let traceDisposable;
|
|
7
|
+
let onTrace: sinon.SinonSpy;
|
|
8
|
+
let traceDisposable: IDisposable;
|
|
9
|
+
|
|
9
10
|
beforeEach(() => {
|
|
10
11
|
onTrace = sinon.spy();
|
|
11
12
|
traceDisposable = tracer.onTrace(onTrace);
|
|
12
13
|
tracer.enable();
|
|
13
14
|
});
|
|
15
|
+
|
|
14
16
|
afterEach(() => {
|
|
15
17
|
traceDisposable.dispose();
|
|
16
18
|
});
|
|
@@ -21,6 +23,7 @@ describe('Tracer', () => {
|
|
|
21
23
|
assert(measurement == null);
|
|
22
24
|
assert(onTrace.notCalled);
|
|
23
25
|
});
|
|
26
|
+
|
|
24
27
|
it('emits a basic trace event', () => {
|
|
25
28
|
const measurement = tracer.createMeasurement('test');
|
|
26
29
|
if (!measurement) return assert.fail();
|
|
@@ -35,6 +38,7 @@ describe('Tracer', () => {
|
|
|
35
38
|
}),
|
|
36
39
|
);
|
|
37
40
|
});
|
|
41
|
+
|
|
38
42
|
it('emits a complex trace event', () => {
|
|
39
43
|
const measurement = tracer.createMeasurement('test', 'myPlugin', 'aaargh', {
|
|
40
44
|
extra: 'data',
|
|
@@ -52,6 +56,7 @@ describe('Tracer', () => {
|
|
|
52
56
|
}),
|
|
53
57
|
);
|
|
54
58
|
});
|
|
59
|
+
|
|
55
60
|
it('calling end twice on measurment should be a no-op', () => {
|
|
56
61
|
const measurement = tracer.createMeasurement('test');
|
|
57
62
|
if (!measurement) return assert.fail();
|
package/tsconfig.json
ADDED
package/build-ts.js
DELETED
package/lib/index.d.ts
DELETED
package/src/SamplingProfiler.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import type {Session} from 'inspector';
|
|
3
|
-
import invariant from 'assert';
|
|
4
|
-
import ThrowableDiagnostic from '@atlaspack/diagnostic';
|
|
5
|
-
|
|
6
|
-
// https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-Profile
|
|
7
|
-
export type Profile = {|
|
|
8
|
-
nodes: Array<ProfileNode>,
|
|
9
|
-
startTime: number,
|
|
10
|
-
endTime: number,
|
|
11
|
-
samples?: Array<number>,
|
|
12
|
-
timeDeltas?: Array<number>,
|
|
13
|
-
|};
|
|
14
|
-
|
|
15
|
-
// https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ProfileNode
|
|
16
|
-
type ProfileNode = {|
|
|
17
|
-
id: number,
|
|
18
|
-
callFrame: CallFrame,
|
|
19
|
-
hitCount?: number,
|
|
20
|
-
children?: Array<number>,
|
|
21
|
-
deoptReason?: string,
|
|
22
|
-
positionTicks?: PositionTickInfo,
|
|
23
|
-
|};
|
|
24
|
-
|
|
25
|
-
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-CallFrame
|
|
26
|
-
type CallFrame = {|
|
|
27
|
-
functionName: string,
|
|
28
|
-
scriptId: string,
|
|
29
|
-
url: string,
|
|
30
|
-
lineNumber: string,
|
|
31
|
-
columnNumber: string,
|
|
32
|
-
|};
|
|
33
|
-
|
|
34
|
-
// https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-PositionTickInfo
|
|
35
|
-
type PositionTickInfo = {|
|
|
36
|
-
line: number,
|
|
37
|
-
ticks: number,
|
|
38
|
-
|};
|
|
39
|
-
|
|
40
|
-
export default class SamplingProfiler {
|
|
41
|
-
session: Session;
|
|
42
|
-
|
|
43
|
-
startProfiling(): Promise<mixed> {
|
|
44
|
-
let inspector;
|
|
45
|
-
try {
|
|
46
|
-
inspector = require('inspector');
|
|
47
|
-
} catch (err) {
|
|
48
|
-
throw new ThrowableDiagnostic({
|
|
49
|
-
diagnostic: {
|
|
50
|
-
message: `The inspector module isn't available`,
|
|
51
|
-
origin: '@atlaspack/workers',
|
|
52
|
-
hints: ['Disable build profiling'],
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
this.session = new inspector.Session();
|
|
58
|
-
this.session.connect();
|
|
59
|
-
|
|
60
|
-
return Promise.all([
|
|
61
|
-
this.sendCommand('Profiler.setSamplingInterval', {
|
|
62
|
-
interval: 100,
|
|
63
|
-
}),
|
|
64
|
-
this.sendCommand('Profiler.enable'),
|
|
65
|
-
this.sendCommand('Profiler.start'),
|
|
66
|
-
]);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
sendCommand(method: string, params?: any): Promise<{profile: Profile, ...}> {
|
|
70
|
-
invariant(this.session != null);
|
|
71
|
-
return new Promise((resolve, reject) => {
|
|
72
|
-
this.session.post(method, params, (err, p) => {
|
|
73
|
-
if (err == null) {
|
|
74
|
-
resolve((p: {profile: Profile, ...}));
|
|
75
|
-
} else {
|
|
76
|
-
reject(err);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
destroy() {
|
|
83
|
-
if (this.session != null) {
|
|
84
|
-
this.session.disconnect();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async stopProfiling(): Promise<Profile> {
|
|
89
|
-
let res = await this.sendCommand('Profiler.stop');
|
|
90
|
-
this.destroy();
|
|
91
|
-
return res.profile;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|