@webiny/api 0.0.0-unstable.9e825fd5fb → 0.0.0-unstable.aa00eecd97
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/Benchmark.d.ts +3 -4
- package/Benchmark.js +30 -22
- package/Benchmark.js.map +1 -1
- package/package.json +11 -11
- package/types.d.ts +10 -4
- package/types.js +1 -7
- package/types.js.map +1 -1
package/Benchmark.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Benchmark as BenchmarkInterface, BenchmarkEnableOnCallable, BenchmarkMeasurement, BenchmarkOutputCallable, BenchmarkRuns } from "./types";
|
|
1
|
+
import { Benchmark as BenchmarkInterface, BenchmarkEnableOnCallable, BenchmarkMeasurement, BenchmarkMeasureOptions, BenchmarkOutputCallable, BenchmarkRuns } from "./types";
|
|
2
2
|
export declare class Benchmark implements BenchmarkInterface {
|
|
3
3
|
readonly measurements: BenchmarkMeasurement[];
|
|
4
4
|
private outputDone;
|
|
@@ -9,7 +9,6 @@ export declare class Benchmark implements BenchmarkInterface {
|
|
|
9
9
|
private readonly onOutputCallables;
|
|
10
10
|
private state;
|
|
11
11
|
get elapsed(): number;
|
|
12
|
-
constructor();
|
|
13
12
|
enableOn(cb: BenchmarkEnableOnCallable): void;
|
|
14
13
|
onOutput(cb: BenchmarkOutputCallable): void;
|
|
15
14
|
enable(): void;
|
|
@@ -17,10 +16,10 @@ export declare class Benchmark implements BenchmarkInterface {
|
|
|
17
16
|
/**
|
|
18
17
|
* When running the output, we need to reverse the callables array, so that the last one added is the first one executed.
|
|
19
18
|
*
|
|
20
|
-
* The
|
|
19
|
+
* The last one is our built-in console.log output.
|
|
21
20
|
*/
|
|
22
21
|
output(): Promise<void>;
|
|
23
|
-
measure<T = any>(
|
|
22
|
+
measure<T = any>(options: BenchmarkMeasureOptions | string, cb: () => Promise<T>): Promise<T>;
|
|
24
23
|
private getIsAlreadyRunning;
|
|
25
24
|
private startRunning;
|
|
26
25
|
private endRunning;
|
package/Benchmark.js
CHANGED
|
@@ -6,17 +6,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
});
|
|
7
7
|
exports.Benchmark = void 0;
|
|
8
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
|
-
var _types = require("./types");
|
|
10
9
|
var BenchmarkState;
|
|
11
10
|
(function (BenchmarkState) {
|
|
12
11
|
BenchmarkState["DISABLED"] = "disabled";
|
|
13
12
|
BenchmarkState["ENABLED"] = "enabled";
|
|
14
13
|
BenchmarkState["UNDETERMINED"] = "undetermined";
|
|
15
14
|
})(BenchmarkState || (BenchmarkState = {}));
|
|
15
|
+
const createDefaultOutputCallable = () => {
|
|
16
|
+
return async ({
|
|
17
|
+
benchmark
|
|
18
|
+
}) => {
|
|
19
|
+
console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);
|
|
20
|
+
console.log("Benchmark measurements:");
|
|
21
|
+
console.log(benchmark.measurements);
|
|
22
|
+
};
|
|
23
|
+
};
|
|
16
24
|
class Benchmark {
|
|
17
|
-
get elapsed() {
|
|
18
|
-
return this.totalElapsed;
|
|
19
|
-
}
|
|
20
25
|
constructor() {
|
|
21
26
|
(0, _defineProperty2.default)(this, "measurements", []);
|
|
22
27
|
(0, _defineProperty2.default)(this, "outputDone", false);
|
|
@@ -26,15 +31,9 @@ class Benchmark {
|
|
|
26
31
|
(0, _defineProperty2.default)(this, "enableOnCallables", []);
|
|
27
32
|
(0, _defineProperty2.default)(this, "onOutputCallables", []);
|
|
28
33
|
(0, _defineProperty2.default)(this, "state", BenchmarkState.UNDETERMINED);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
*/
|
|
33
|
-
this.onOutputCallables.push(async () => {
|
|
34
|
-
console.log(`Benchmark total time elapsed: ${this.elapsed}ms`);
|
|
35
|
-
console.log("Benchmark measurements:");
|
|
36
|
-
console.log(this.measurements);
|
|
37
|
-
});
|
|
34
|
+
}
|
|
35
|
+
get elapsed() {
|
|
36
|
+
return this.totalElapsed;
|
|
38
37
|
}
|
|
39
38
|
enableOn(cb) {
|
|
40
39
|
this.enableOnCallables.push(cb);
|
|
@@ -52,7 +51,7 @@ class Benchmark {
|
|
|
52
51
|
/**
|
|
53
52
|
* When running the output, we need to reverse the callables array, so that the last one added is the first one executed.
|
|
54
53
|
*
|
|
55
|
-
* The
|
|
54
|
+
* The last one is our built-in console.log output.
|
|
56
55
|
*/
|
|
57
56
|
async output() {
|
|
58
57
|
/**
|
|
@@ -61,21 +60,25 @@ class Benchmark {
|
|
|
61
60
|
if (this.outputDone || this.measurements.length === 0) {
|
|
62
61
|
return;
|
|
63
62
|
}
|
|
64
|
-
const callables = this.onOutputCallables.reverse();
|
|
63
|
+
const callables = [...this.onOutputCallables].reverse();
|
|
64
|
+
callables.push(createDefaultOutputCallable());
|
|
65
65
|
for (const cb of callables) {
|
|
66
|
-
const result = await cb(
|
|
67
|
-
|
|
66
|
+
const result = await cb({
|
|
67
|
+
benchmark: this,
|
|
68
|
+
stop: () => "stop"
|
|
69
|
+
});
|
|
70
|
+
if (result === "stop") {
|
|
68
71
|
return;
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
74
|
this.outputDone = true;
|
|
72
75
|
}
|
|
73
|
-
async measure(
|
|
76
|
+
async measure(options, cb) {
|
|
74
77
|
const enabled = await this.getIsEnabled();
|
|
75
78
|
if (!enabled) {
|
|
76
79
|
return cb();
|
|
77
80
|
}
|
|
78
|
-
const measurement = this.startMeasurement(
|
|
81
|
+
const measurement = this.startMeasurement(options);
|
|
79
82
|
const isAlreadyRunning = this.getIsAlreadyRunning();
|
|
80
83
|
this.startRunning();
|
|
81
84
|
try {
|
|
@@ -83,7 +86,7 @@ class Benchmark {
|
|
|
83
86
|
} finally {
|
|
84
87
|
const measurementEnded = this.stopMeasurement(measurement);
|
|
85
88
|
this.measurements.push(measurementEnded);
|
|
86
|
-
this.addRun(measurementEnded
|
|
89
|
+
this.addRun(measurementEnded);
|
|
87
90
|
/**
|
|
88
91
|
* Only add to total time if this run is not a child of another run.
|
|
89
92
|
* And then end running.
|
|
@@ -122,7 +125,8 @@ class Benchmark {
|
|
|
122
125
|
addElapsed(measurement) {
|
|
123
126
|
this.totalElapsed = this.totalElapsed + measurement.elapsed;
|
|
124
127
|
}
|
|
125
|
-
addRun(
|
|
128
|
+
addRun(measurement) {
|
|
129
|
+
const name = `${measurement.category}#${measurement.name}`;
|
|
126
130
|
if (!this.runs[name]) {
|
|
127
131
|
this.runs[name] = 0;
|
|
128
132
|
}
|
|
@@ -131,9 +135,12 @@ class Benchmark {
|
|
|
131
135
|
setState(state) {
|
|
132
136
|
this.state = state;
|
|
133
137
|
}
|
|
134
|
-
startMeasurement(
|
|
138
|
+
startMeasurement(options) {
|
|
139
|
+
const name = typeof options === "string" ? options : options.name;
|
|
140
|
+
const category = typeof options === "string" ? "webiny" : options.category;
|
|
135
141
|
return {
|
|
136
142
|
name,
|
|
143
|
+
category,
|
|
137
144
|
start: new Date(),
|
|
138
145
|
memoryStart: process.memoryUsage().heapUsed
|
|
139
146
|
};
|
|
@@ -144,6 +151,7 @@ class Benchmark {
|
|
|
144
151
|
const elapsed = end.getTime() - measurement.start.getTime();
|
|
145
152
|
return {
|
|
146
153
|
name: measurement.name,
|
|
154
|
+
category: measurement.category,
|
|
147
155
|
start: measurement.start,
|
|
148
156
|
end,
|
|
149
157
|
elapsed,
|
package/Benchmark.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["BenchmarkState","Benchmark","elapsed","totalElapsed","constructor","UNDETERMINED","onOutputCallables","push","console","log","measurements","enableOn","cb","enableOnCallables","onOutput","enable","setState","ENABLED","disable","DISABLED","output","outputDone","length","callables","reverse","result","BenchmarkOutputCallableResponse","BREAK","measure","name","enabled","getIsEnabled","measurement","startMeasurement","isAlreadyRunning","getIsAlreadyRunning","startRunning","measurementEnded","stopMeasurement","addRun","addElapsed","endRunning","state","runs","start","Date","memoryStart","process","memoryUsage","heapUsed","end","memoryEnd","getTime","memory"],"sources":["Benchmark.ts"],"sourcesContent":["import {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkOutputCallable,\n BenchmarkOutputCallableResponse,\n BenchmarkRuns\n} from \"~/types\";\n\nenum BenchmarkState {\n DISABLED = \"disabled\",\n ENABLED = \"enabled\",\n UNDETERMINED = \"undetermined\"\n}\n\ninterface BenchmarkMeasurementStart extends Pick<BenchmarkMeasurement, \"name\" | \"start\"> {\n memoryStart: number;\n}\n\nexport class Benchmark implements BenchmarkInterface {\n public readonly measurements: BenchmarkMeasurement[] = [];\n\n private outputDone = false;\n private isAlreadyRunning = false;\n private totalElapsed = 0;\n public readonly runs: BenchmarkRuns = {};\n private readonly enableOnCallables: BenchmarkEnableOnCallable[] = [];\n private readonly onOutputCallables: BenchmarkOutputCallable[] = [];\n private state: BenchmarkState = BenchmarkState.UNDETERMINED;\n\n public get elapsed(): number {\n return this.totalElapsed;\n }\n\n public constructor() {\n /**\n * The default output is to the console.\n * This one is executed after all other user defined outputs.\n */\n this.onOutputCallables.push(async () => {\n console.log(`Benchmark total time elapsed: ${this.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(this.measurements);\n });\n }\n\n public enableOn(cb: BenchmarkEnableOnCallable): void {\n this.enableOnCallables.push(cb);\n }\n\n public onOutput(cb: BenchmarkOutputCallable): void {\n this.onOutputCallables.push(cb);\n }\n\n public enable(): void {\n this.setState(BenchmarkState.ENABLED);\n }\n\n public disable(): void {\n this.setState(BenchmarkState.DISABLED);\n }\n\n /**\n * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.\n *\n * The first one is our built-in console.log output, which we want to be the last one executed - and we need to stop output if user wants to end it.\n */\n public async output(): Promise<void> {\n /**\n * No point in outputting more than once or if no measurements were made.\n */\n if (this.outputDone || this.measurements.length === 0) {\n return;\n }\n const callables = this.onOutputCallables.reverse();\n for (const cb of callables) {\n const result = await cb(this);\n if (result === BenchmarkOutputCallableResponse.BREAK) {\n return;\n }\n }\n this.outputDone = true;\n }\n\n public async measure<T = any>(name: string, cb: () => Promise<T>): Promise<T> {\n const enabled = await this.getIsEnabled();\n if (!enabled) {\n return cb();\n }\n const measurement = this.startMeasurement(name);\n const isAlreadyRunning = this.getIsAlreadyRunning();\n this.startRunning();\n try {\n return await cb();\n } finally {\n const measurementEnded = this.stopMeasurement(measurement);\n this.measurements.push(measurementEnded);\n this.addRun(measurementEnded.name);\n /**\n * Only add to total time if this run is not a child of another run.\n * And then end running.\n */\n if (!isAlreadyRunning) {\n this.addElapsed(measurementEnded);\n this.endRunning();\n }\n }\n }\n\n private getIsAlreadyRunning(): boolean {\n return this.isAlreadyRunning;\n }\n private startRunning(): void {\n this.isAlreadyRunning = true;\n }\n private endRunning(): void {\n this.isAlreadyRunning = false;\n }\n\n private async getIsEnabled(): Promise<boolean> {\n if (this.state === BenchmarkState.ENABLED) {\n return true;\n } else if (this.state === BenchmarkState.DISABLED) {\n return false;\n }\n\n for (const cb of this.enableOnCallables) {\n const result = await cb();\n if (result) {\n this.enable();\n return true;\n }\n }\n this.disable();\n return false;\n }\n\n private addElapsed(measurement: Pick<BenchmarkMeasurement, \"elapsed\">): void {\n this.totalElapsed = this.totalElapsed + measurement.elapsed;\n }\n\n private addRun(name: string): void {\n if (!this.runs[name]) {\n this.runs[name] = 0;\n }\n this.runs[name]++;\n }\n\n private setState(state: BenchmarkState): void {\n this.state = state;\n }\n\n private startMeasurement(name: string): BenchmarkMeasurementStart {\n return {\n name,\n start: new Date(),\n memoryStart: process.memoryUsage().heapUsed\n };\n }\n\n private stopMeasurement(measurement: BenchmarkMeasurementStart): BenchmarkMeasurement {\n const end = new Date();\n const memoryEnd = process.memoryUsage().heapUsed;\n const elapsed = end.getTime() - measurement.start.getTime();\n return {\n name: measurement.name,\n start: measurement.start,\n end,\n elapsed,\n memory: memoryEnd - measurement.memoryStart\n };\n }\n}\n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,WAAW,CAACH,IAAI;MACtBe,KAAK,EAAEZ,WAAW,CAACY,KAAK;MACxBM,GAAG;MACHhD,OAAO;MACPmD,MAAM,EAAEF,SAAS,GAAGnB,WAAW,CAACc;IACpC,CAAC;EACL;AACJ;AAAC"}
|
|
1
|
+
{"version":3,"names":["BenchmarkState","createDefaultOutputCallable","benchmark","console","log","elapsed","measurements","Benchmark","UNDETERMINED","totalElapsed","enableOn","cb","enableOnCallables","push","onOutput","onOutputCallables","enable","setState","ENABLED","disable","DISABLED","output","outputDone","length","callables","reverse","result","stop","measure","options","enabled","getIsEnabled","measurement","startMeasurement","isAlreadyRunning","getIsAlreadyRunning","startRunning","measurementEnded","stopMeasurement","addRun","addElapsed","endRunning","state","name","category","runs","start","Date","memoryStart","process","memoryUsage","heapUsed","end","memoryEnd","getTime","memory"],"sources":["Benchmark.ts"],"sourcesContent":["import {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkMeasureOptions,\n BenchmarkOutputCallable,\n BenchmarkRuns\n} from \"~/types\";\n\nenum BenchmarkState {\n DISABLED = \"disabled\",\n ENABLED = \"enabled\",\n UNDETERMINED = \"undetermined\"\n}\n\ninterface BenchmarkMeasurementStart\n extends Pick<BenchmarkMeasurement, \"name\" | \"category\" | \"start\"> {\n memoryStart: number;\n}\n\nconst createDefaultOutputCallable = (): BenchmarkOutputCallable => {\n return async ({ benchmark }) => {\n console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(benchmark.measurements);\n };\n};\n\nexport class Benchmark implements BenchmarkInterface {\n public readonly measurements: BenchmarkMeasurement[] = [];\n\n private outputDone = false;\n private isAlreadyRunning = false;\n private totalElapsed = 0;\n public readonly runs: BenchmarkRuns = {};\n private readonly enableOnCallables: BenchmarkEnableOnCallable[] = [];\n private readonly onOutputCallables: BenchmarkOutputCallable[] = [];\n private state: BenchmarkState = BenchmarkState.UNDETERMINED;\n\n public get elapsed(): number {\n return this.totalElapsed;\n }\n\n public enableOn(cb: BenchmarkEnableOnCallable): void {\n this.enableOnCallables.push(cb);\n }\n\n public onOutput(cb: BenchmarkOutputCallable): void {\n this.onOutputCallables.push(cb);\n }\n\n public enable(): void {\n this.setState(BenchmarkState.ENABLED);\n }\n\n public disable(): void {\n this.setState(BenchmarkState.DISABLED);\n }\n\n /**\n * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.\n *\n * The last one is our built-in console.log output.\n */\n public async output(): Promise<void> {\n /**\n * No point in outputting more than once or if no measurements were made.\n */\n if (this.outputDone || this.measurements.length === 0) {\n return;\n }\n const callables = [...this.onOutputCallables].reverse();\n callables.push(createDefaultOutputCallable());\n for (const cb of callables) {\n const result = await cb({\n benchmark: this,\n stop: () => \"stop\"\n });\n if (result === \"stop\") {\n return;\n }\n }\n this.outputDone = true;\n }\n\n public async measure<T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ): Promise<T> {\n const enabled = await this.getIsEnabled();\n if (!enabled) {\n return cb();\n }\n const measurement = this.startMeasurement(options);\n const isAlreadyRunning = this.getIsAlreadyRunning();\n this.startRunning();\n try {\n return await cb();\n } finally {\n const measurementEnded = this.stopMeasurement(measurement);\n this.measurements.push(measurementEnded);\n this.addRun(measurementEnded);\n /**\n * Only add to total time if this run is not a child of another run.\n * And then end running.\n */\n if (!isAlreadyRunning) {\n this.addElapsed(measurementEnded);\n this.endRunning();\n }\n }\n }\n\n private getIsAlreadyRunning(): boolean {\n return this.isAlreadyRunning;\n }\n private startRunning(): void {\n this.isAlreadyRunning = true;\n }\n private endRunning(): void {\n this.isAlreadyRunning = false;\n }\n\n private async getIsEnabled(): Promise<boolean> {\n if (this.state === BenchmarkState.ENABLED) {\n return true;\n } else if (this.state === BenchmarkState.DISABLED) {\n return false;\n }\n\n for (const cb of this.enableOnCallables) {\n const result = await cb();\n if (result) {\n this.enable();\n return true;\n }\n }\n this.disable();\n return false;\n }\n\n private addElapsed(measurement: Pick<BenchmarkMeasurement, \"elapsed\">): void {\n this.totalElapsed = this.totalElapsed + measurement.elapsed;\n }\n\n private addRun(measurement: Pick<BenchmarkMeasurement, \"name\" | \"category\">): void {\n const name = `${measurement.category}#${measurement.name}`;\n if (!this.runs[name]) {\n this.runs[name] = 0;\n }\n this.runs[name]++;\n }\n\n private setState(state: BenchmarkState): void {\n this.state = state;\n }\n\n private startMeasurement(options: BenchmarkMeasureOptions | string): BenchmarkMeasurementStart {\n const name = typeof options === \"string\" ? options : options.name;\n const category = typeof options === \"string\" ? \"webiny\" : options.category;\n return {\n name,\n category,\n start: new Date(),\n memoryStart: process.memoryUsage().heapUsed\n };\n }\n\n private stopMeasurement(measurement: BenchmarkMeasurementStart): BenchmarkMeasurement {\n const end = new Date();\n const memoryEnd = process.memoryUsage().heapUsed;\n const elapsed = end.getTime() - measurement.start.getTime();\n return {\n name: measurement.name,\n category: measurement.category,\n start: measurement.start,\n end,\n elapsed,\n memory: memoryEnd - measurement.memoryStart\n };\n }\n}\n"],"mappings":";;;;;;;;IASKA,cAAc;AAAA,WAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;AAAA,GAAdA,cAAc,KAAdA,cAAc;AAWnB,MAAMC,2BAA2B,GAAG,MAA+B;EAC/D,OAAO,OAAO;IAAEC;EAAU,CAAC,KAAK;IAC5BC,OAAO,CAACC,GAAG,CAAE,iCAAgCF,SAAS,CAACG,OAAQ,IAAG,CAAC;IACnEF,OAAO,CAACC,GAAG,CAAC,yBAAyB,CAAC;IACtCD,OAAO,CAACC,GAAG,CAACF,SAAS,CAACI,YAAY,CAAC;EACvC,CAAC;AACL,CAAC;AAEM,MAAMC,SAAS,CAA+B;EAAA;IAAA,oDACM,EAAE;IAAA,kDAEpC,KAAK;IAAA,wDACC,KAAK;IAAA,oDACT,CAAC;IAAA,4CACc,CAAC,CAAC;IAAA,yDAC0B,EAAE;IAAA,yDACJ,EAAE;IAAA,6CAClCP,cAAc,CAACQ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+1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.aa00eecd97",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,17 +13,17 @@
|
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@babel/runtime": "7.20.13",
|
|
16
|
-
"@webiny/plugins": "0.0.0-unstable.
|
|
16
|
+
"@webiny/plugins": "0.0.0-unstable.aa00eecd97"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@babel/cli": "
|
|
20
|
-
"@babel/core": "
|
|
21
|
-
"@babel/preset-env": "
|
|
22
|
-
"@babel/preset-typescript": "
|
|
23
|
-
"@webiny/cli": "
|
|
24
|
-
"@webiny/project-utils": "
|
|
25
|
-
"rimraf": "
|
|
26
|
-
"ttypescript": "
|
|
19
|
+
"@babel/cli": "7.20.7",
|
|
20
|
+
"@babel/core": "7.20.12",
|
|
21
|
+
"@babel/preset-env": "7.20.2",
|
|
22
|
+
"@babel/preset-typescript": "7.18.6",
|
|
23
|
+
"@webiny/cli": "0.0.0-unstable.aa00eecd97",
|
|
24
|
+
"@webiny/project-utils": "0.0.0-unstable.aa00eecd97",
|
|
25
|
+
"rimraf": "3.0.2",
|
|
26
|
+
"ttypescript": "1.5.15",
|
|
27
27
|
"typescript": "4.7.4"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"build": "yarn webiny run build",
|
|
35
35
|
"watch": "yarn webiny run watch"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "aa00eecd97d16da9bdb0581f33d491487ce43ce9"
|
|
38
38
|
}
|
package/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface BenchmarkRuns {
|
|
|
4
4
|
}
|
|
5
5
|
export interface BenchmarkMeasurement {
|
|
6
6
|
name: string;
|
|
7
|
+
category: string;
|
|
7
8
|
start: Date;
|
|
8
9
|
end: Date;
|
|
9
10
|
elapsed: number;
|
|
@@ -12,11 +13,16 @@ export interface BenchmarkMeasurement {
|
|
|
12
13
|
export interface BenchmarkEnableOnCallable {
|
|
13
14
|
(): Promise<boolean>;
|
|
14
15
|
}
|
|
15
|
-
export
|
|
16
|
-
|
|
16
|
+
export interface BenchmarkOutputCallableParams {
|
|
17
|
+
benchmark: Benchmark;
|
|
18
|
+
stop: () => "stop";
|
|
17
19
|
}
|
|
18
20
|
export interface BenchmarkOutputCallable {
|
|
19
|
-
(
|
|
21
|
+
(params: BenchmarkOutputCallableParams): Promise<"stop" | undefined | null | void>;
|
|
22
|
+
}
|
|
23
|
+
export interface BenchmarkMeasureOptions {
|
|
24
|
+
name: string;
|
|
25
|
+
category: string;
|
|
20
26
|
}
|
|
21
27
|
export interface Benchmark {
|
|
22
28
|
elapsed: number;
|
|
@@ -25,7 +31,7 @@ export interface Benchmark {
|
|
|
25
31
|
output: () => Promise<void>;
|
|
26
32
|
onOutput: (cb: BenchmarkOutputCallable) => void;
|
|
27
33
|
enableOn: (cb: BenchmarkEnableOnCallable) => void;
|
|
28
|
-
measure: <T = any>(
|
|
34
|
+
measure: <T = any>(options: BenchmarkMeasureOptions | string, cb: () => Promise<T>) => Promise<T>;
|
|
29
35
|
enable: () => void;
|
|
30
36
|
disable: () => void;
|
|
31
37
|
}
|
package/types.js
CHANGED
|
@@ -2,10 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.BenchmarkOutputCallableResponse = void 0;
|
|
7
|
-
let BenchmarkOutputCallableResponse;
|
|
8
|
-
exports.BenchmarkOutputCallableResponse = BenchmarkOutputCallableResponse;
|
|
9
|
-
(function (BenchmarkOutputCallableResponse) {
|
|
10
|
-
BenchmarkOutputCallableResponse["BREAK"] = "break";
|
|
11
|
-
})(BenchmarkOutputCallableResponse || (exports.BenchmarkOutputCallableResponse = BenchmarkOutputCallableResponse = {}));
|
|
5
|
+
});
|
package/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import { PluginsContainer } from \"@webiny/plugins\";\n\nexport interface BenchmarkRuns {\n [key: string]: number;\n}\n\nexport interface BenchmarkMeasurement {\n name: string;\n category: string;\n start: Date;\n end: Date;\n elapsed: number;\n memory: number;\n}\n\nexport interface BenchmarkEnableOnCallable {\n (): Promise<boolean>;\n}\n\nexport interface BenchmarkOutputCallableParams {\n benchmark: Benchmark;\n stop: () => \"stop\";\n}\nexport interface BenchmarkOutputCallable {\n (params: BenchmarkOutputCallableParams): Promise<\"stop\" | undefined | null | void>;\n}\nexport interface BenchmarkMeasureOptions {\n name: string;\n category: string;\n}\nexport interface Benchmark {\n elapsed: number;\n runs: BenchmarkRuns;\n measurements: BenchmarkMeasurement[];\n output: () => Promise<void>;\n onOutput: (cb: BenchmarkOutputCallable) => void;\n enableOn: (cb: BenchmarkEnableOnCallable) => void;\n measure: <T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ) => Promise<T>;\n enable: () => void;\n disable: () => void;\n}\n\n/**\n * The main context which is constructed on every request.\n * All other contexts should extend or augment this one.\n */\nexport interface Context {\n plugins: PluginsContainer;\n args: any;\n readonly WEBINY_VERSION: string;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n hasResult: () => boolean;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n *\n * @private\n */\n _result?: any;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n setResult: (value: any) => void;\n /**\n * Not to be used outside of Webiny internal code.\n * @internal\n */\n getResult: () => void;\n /**\n * Wait for property to be defined on the object and then execute the callable.\n * In case of multiple objects defined, wait for all of them.\n */\n waitFor: <T extends Context = Context>(\n obj: string[] | string,\n cb: (context: T) => void\n ) => void;\n\n benchmark: Benchmark;\n}\n"],"mappings":""}
|