@naturalcycles/bench-lib 1.6.6 → 1.6.8
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/dist/bench.util.js +15 -16
- package/dist/cannon.profiles.js +19 -18
- package/dist/cannon.util.js +16 -17
- package/package.json +1 -1
package/dist/bench.util.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.runBench = exports.runBenchScript = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
6
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const vega = require("vega");
|
|
10
|
-
const vegaLite = require("vega-lite");
|
|
7
|
+
const benchmark_1 = tslib_1.__importDefault(require("benchmark"));
|
|
8
|
+
const vega_1 = tslib_1.__importDefault(require("vega"));
|
|
9
|
+
const vega_lite_1 = tslib_1.__importDefault(require("vega-lite"));
|
|
11
10
|
const asciiChart_util_1 = require("./asciiChart.util");
|
|
12
11
|
/**
|
|
13
12
|
* Wraps `runBench` in `runScript` for convenience, so it can be run in top-level without `await`.
|
|
@@ -15,7 +14,7 @@ const asciiChart_util_1 = require("./asciiChart.util");
|
|
|
15
14
|
function runBenchScript(opt) {
|
|
16
15
|
// fake timeout is needed to workaround `benchmark` process exiting too early when 2+ runs are used
|
|
17
16
|
const timeout = setTimeout(() => { }, 10000000);
|
|
18
|
-
(0,
|
|
17
|
+
(0, nodejs_lib_1.runScript)(async () => {
|
|
19
18
|
await runBench(opt);
|
|
20
19
|
clearTimeout(timeout);
|
|
21
20
|
});
|
|
@@ -46,19 +45,19 @@ async function runBench(opt) {
|
|
|
46
45
|
// names.forEach(name => {
|
|
47
46
|
// summary[name] = results.hz.map(map => map[name]!)
|
|
48
47
|
// })
|
|
49
|
-
|
|
48
|
+
nodejs_lib_1.fs2.ensureDir(reportDirPath);
|
|
50
49
|
const summaryJsonPath = `${reportDirPath}/${name}.json`;
|
|
51
|
-
|
|
52
|
-
console.log(`saved ${(0,
|
|
50
|
+
nodejs_lib_1.fs2.writeJson(summaryJsonPath, avg, { spaces: 2 });
|
|
51
|
+
console.log(`saved ${(0, nodejs_lib_1.dimGrey)(summaryJsonPath)}`);
|
|
53
52
|
}
|
|
54
53
|
if (writePlot) {
|
|
55
|
-
|
|
54
|
+
nodejs_lib_1.fs2.ensureDir(reportDirPath);
|
|
56
55
|
const spec = benchResultsToVegaSpec(avg);
|
|
57
|
-
const view = new
|
|
56
|
+
const view = new vega_1.default.View(vega_1.default.parse(spec), { renderer: 'none' });
|
|
58
57
|
const svg = await view.toSVG();
|
|
59
58
|
const plotPath = `${reportDirPath}/${name}.svg`;
|
|
60
|
-
|
|
61
|
-
console.log(`saved ${(0,
|
|
59
|
+
nodejs_lib_1.fs2.writeFile(plotPath, svg);
|
|
60
|
+
console.log(`saved ${(0, nodejs_lib_1.dimGrey)(plotPath)}`);
|
|
62
61
|
}
|
|
63
62
|
if (asciiPlot) {
|
|
64
63
|
console.log('\n' + (0, asciiChart_util_1.plotAsciiChart)(avg));
|
|
@@ -68,13 +67,13 @@ async function runBench(opt) {
|
|
|
68
67
|
exports.runBench = runBench;
|
|
69
68
|
async function runBenchOnce(opt, run) {
|
|
70
69
|
const defer = (0, js_lib_1.pDefer)();
|
|
71
|
-
const suite = new
|
|
70
|
+
const suite = new benchmark_1.default.Suite()
|
|
72
71
|
.on('cycle', (event) => {
|
|
73
72
|
console.log(String(event.target));
|
|
74
73
|
// console.log(event.target)
|
|
75
74
|
})
|
|
76
75
|
.on('complete', function () {
|
|
77
|
-
console.log(`Fastest in run ${(0,
|
|
76
|
+
console.log(`Fastest in run ${(0, nodejs_lib_1.yellow)(run)} is ` + this.filter('fastest').map('name'));
|
|
78
77
|
// console.log(this[0].stats)
|
|
79
78
|
// console.log(this)
|
|
80
79
|
const results = {};
|
|
@@ -133,6 +132,6 @@ function benchResultsToVegaSpec(map) {
|
|
|
133
132
|
},
|
|
134
133
|
},
|
|
135
134
|
};
|
|
136
|
-
const { spec } =
|
|
135
|
+
const { spec } = vega_lite_1.default.compile(liteSpec);
|
|
137
136
|
return spec;
|
|
138
137
|
}
|
package/dist/cannon.profiles.js
CHANGED
|
@@ -1,40 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.expressSyncFunctionFactory = exports.expressFunctionFactory = exports.expressWithMiddlewaresServerFactory = exports.bareExpressServerFactory = exports.bareNodeServerFactory = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const node_http_1 = tslib_1.__importDefault(require("node:http"));
|
|
6
|
+
const express_1 = tslib_1.__importDefault(require("express"));
|
|
7
|
+
const helmet_1 = tslib_1.__importDefault(require("helmet"));
|
|
7
8
|
const bareNodeServerFactory = async () => {
|
|
8
|
-
return
|
|
9
|
+
return node_http_1.default.createServer((req, res) => {
|
|
9
10
|
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
10
11
|
res.end(JSON.stringify({ hello: 'world' }));
|
|
11
12
|
});
|
|
12
13
|
};
|
|
13
14
|
exports.bareNodeServerFactory = bareNodeServerFactory;
|
|
14
15
|
const bareExpressServerFactory = async () => {
|
|
15
|
-
const app =
|
|
16
|
+
const app = (0, express_1.default)();
|
|
16
17
|
app.disable('etag');
|
|
17
18
|
app.disable('x-powered-by');
|
|
18
19
|
app.get('/', (req, res) => res.json({ hello: 'world' }));
|
|
19
|
-
return
|
|
20
|
+
return node_http_1.default.createServer(app);
|
|
20
21
|
};
|
|
21
22
|
exports.bareExpressServerFactory = bareExpressServerFactory;
|
|
22
23
|
/**
|
|
23
24
|
* Based on: https://github.com/fastify/benchmarks/blob/master/benchmarks/express-with-middlewares.js
|
|
24
25
|
*/
|
|
25
26
|
const expressWithMiddlewaresServerFactory = async () => {
|
|
26
|
-
const app =
|
|
27
|
+
const app = (0, express_1.default)();
|
|
27
28
|
app.disable('etag');
|
|
28
29
|
app.disable('x-powered-by');
|
|
29
30
|
app.use(require('cors')());
|
|
30
|
-
app.use(
|
|
31
|
-
app.use(
|
|
32
|
-
app.use(
|
|
33
|
-
app.use(
|
|
34
|
-
app.use(
|
|
35
|
-
app.use(
|
|
31
|
+
app.use(helmet_1.default.dnsPrefetchControl());
|
|
32
|
+
app.use(helmet_1.default.frameguard());
|
|
33
|
+
app.use(helmet_1.default.hidePoweredBy());
|
|
34
|
+
app.use(helmet_1.default.hsts());
|
|
35
|
+
app.use(helmet_1.default.ieNoOpen());
|
|
36
|
+
app.use(helmet_1.default.xssFilter());
|
|
36
37
|
app.get('/', (req, res) => res.json({ hello: 'world' }));
|
|
37
|
-
return
|
|
38
|
+
return node_http_1.default.createServer(app);
|
|
38
39
|
};
|
|
39
40
|
exports.expressWithMiddlewaresServerFactory = expressWithMiddlewaresServerFactory;
|
|
40
41
|
/**
|
|
@@ -42,21 +43,21 @@ exports.expressWithMiddlewaresServerFactory = expressWithMiddlewaresServerFactor
|
|
|
42
43
|
*/
|
|
43
44
|
function expressFunctionFactory(fn) {
|
|
44
45
|
return async () => {
|
|
45
|
-
const app =
|
|
46
|
+
const app = (0, express_1.default)();
|
|
46
47
|
app.disable('etag');
|
|
47
48
|
app.disable('x-powered-by');
|
|
48
49
|
app.get('/', async (req, res) => res.json(await fn()));
|
|
49
|
-
return
|
|
50
|
+
return node_http_1.default.createServer(app);
|
|
50
51
|
};
|
|
51
52
|
}
|
|
52
53
|
exports.expressFunctionFactory = expressFunctionFactory;
|
|
53
54
|
function expressSyncFunctionFactory(fn) {
|
|
54
55
|
return async () => {
|
|
55
|
-
const app =
|
|
56
|
+
const app = (0, express_1.default)();
|
|
56
57
|
app.disable('etag');
|
|
57
58
|
app.disable('x-powered-by');
|
|
58
59
|
app.get('/', (req, res) => res.json(fn()));
|
|
59
|
-
return
|
|
60
|
+
return node_http_1.default.createServer(app);
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
63
|
exports.expressSyncFunctionFactory = expressSyncFunctionFactory;
|
package/dist/cannon.util.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.runCannon = exports.runCannonScript = void 0;
|
|
4
|
-
const
|
|
5
|
-
const nodejs_lib_2 = require("@naturalcycles/nodejs-lib");
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
6
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
6
|
+
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
7
|
+
const vega_1 = tslib_1.__importDefault(require("vega"));
|
|
8
|
+
const vega_lite_1 = tslib_1.__importDefault(require("vega-lite"));
|
|
10
9
|
/**
|
|
11
10
|
* Wraps `runBench` in `runScript` for convenience, so it can be run in top-level without `await`.
|
|
12
11
|
*/
|
|
13
12
|
function runCannonScript(profiles, optInput = {}) {
|
|
14
13
|
// fake timeout is needed to workaround `benchmark` process exiting too early when 2+ runs are used
|
|
15
14
|
const timeout = setTimeout(() => { }, 10000000);
|
|
16
|
-
(0,
|
|
15
|
+
(0, nodejs_lib_1.runScript)(async () => {
|
|
17
16
|
await runCannon(profiles, optInput);
|
|
18
17
|
clearTimeout(timeout);
|
|
19
18
|
});
|
|
@@ -53,7 +52,7 @@ async function runCannon(profiles, optInput = {}) {
|
|
|
53
52
|
});
|
|
54
53
|
}
|
|
55
54
|
const { reportDirPath } = opt;
|
|
56
|
-
|
|
55
|
+
nodejs_lib_1.fs2.ensureDir(reportDirPath);
|
|
57
56
|
const resultByProfile = {};
|
|
58
57
|
const summaries = [];
|
|
59
58
|
for await (const profileName of Object.keys(profiles)) {
|
|
@@ -67,11 +66,11 @@ async function runCannon(profiles, optInput = {}) {
|
|
|
67
66
|
console.table(summaries);
|
|
68
67
|
if (opt.writeSummary) {
|
|
69
68
|
const summaryJsonPath = `${reportDirPath}/${opt.name}.summary.json`;
|
|
70
|
-
|
|
71
|
-
console.log(`saved ${(0,
|
|
69
|
+
nodejs_lib_1.fs2.writeJson(summaryJsonPath, summaries, { spaces: 2 });
|
|
70
|
+
console.log(`saved ${(0, nodejs_lib_1.dimGrey)(summaryJsonPath)}`);
|
|
72
71
|
}
|
|
73
72
|
if (opt.writeRawSummary) {
|
|
74
|
-
|
|
73
|
+
nodejs_lib_1.fs2.writeJson(`${reportDirPath}/${opt.name}.rawSummary.json`, resultByProfile, { spaces: 2 });
|
|
75
74
|
}
|
|
76
75
|
if (opt.writePlots) {
|
|
77
76
|
await writePlotFiles(opt, summaries);
|
|
@@ -88,7 +87,7 @@ async function runCannonProfile(profileName, serverFactory, opt) {
|
|
|
88
87
|
const url = `${host}:${port}${opt.path}`;
|
|
89
88
|
let finalResult = undefined;
|
|
90
89
|
for await (const run of (0, js_lib_1._range)(1, runs + 1)) {
|
|
91
|
-
console.log(`\n${(0,
|
|
90
|
+
console.log(`\n${(0, nodejs_lib_1.boldRed)('=== ' + profileName + ' ===')} run ${(0, nodejs_lib_1.yellow)(run)}/${(0, nodejs_lib_1.yellow)(runs)}\n`);
|
|
92
91
|
const doneDefer = (0, js_lib_1.pDefer)();
|
|
93
92
|
const instance = autocannon({
|
|
94
93
|
url,
|
|
@@ -112,7 +111,7 @@ async function runCannonProfile(profileName, serverFactory, opt) {
|
|
|
112
111
|
});
|
|
113
112
|
finalResult = await doneDefer;
|
|
114
113
|
if (!silent)
|
|
115
|
-
console.log((0,
|
|
114
|
+
console.log((0, nodejs_lib_1.dimGrey)(`cooldown ${(0, nodejs_lib_1.yellow)(cooldown)} seconds...`));
|
|
116
115
|
await (0, js_lib_1.pDelay)(cooldown * 1000);
|
|
117
116
|
}
|
|
118
117
|
await new Promise(resolve => server.close(resolve));
|
|
@@ -135,7 +134,7 @@ function toSummary(name, result) {
|
|
|
135
134
|
}
|
|
136
135
|
async function writePlotFiles(opt, summaries) {
|
|
137
136
|
const { reportDirPath, name } = opt;
|
|
138
|
-
|
|
137
|
+
nodejs_lib_1.fs2.ensureDir(reportDirPath);
|
|
139
138
|
let fields = ['rpsAvg', 'latencyAvg', 'latency50', 'latency90', 'latency99', 'throughputAvg'];
|
|
140
139
|
if (!opt.includeLatencyPercentiles) {
|
|
141
140
|
fields = fields.filter(f => !f.startsWith('latency') || f === 'latencyAvg');
|
|
@@ -143,13 +142,13 @@ async function writePlotFiles(opt, summaries) {
|
|
|
143
142
|
const specs = autocannonSummaryToVegaSpecs(fields, summaries);
|
|
144
143
|
await (0, js_lib_1.pMap)(Object.keys(specs), async (specName) => {
|
|
145
144
|
// create a new view instance for a given Vega JSON spec
|
|
146
|
-
const view = new
|
|
145
|
+
const view = new vega_1.default.View(vega_1.default.parse(specs[specName]), { renderer: 'none' });
|
|
147
146
|
// generate a static SVG image
|
|
148
147
|
const svg = await view.toSVG();
|
|
149
148
|
// console.log(svg)
|
|
150
|
-
|
|
149
|
+
nodejs_lib_1.fs2.writeFile(`${reportDirPath}/${name}.${specName}.svg`, svg);
|
|
151
150
|
});
|
|
152
|
-
|
|
151
|
+
nodejs_lib_1.fs2.writeFile(`${reportDirPath}/${name}.md`, mdContent(opt, Object.keys(specs)));
|
|
153
152
|
}
|
|
154
153
|
function autocannonSummaryToVegaSpecs(fields, summaries) {
|
|
155
154
|
// console.log(summary)
|
|
@@ -179,7 +178,7 @@ function autocannonSummaryToVegaSpecs(fields, summaries) {
|
|
|
179
178
|
},
|
|
180
179
|
},
|
|
181
180
|
};
|
|
182
|
-
const { spec } =
|
|
181
|
+
const { spec } = vega_lite_1.default.compile(liteSpec);
|
|
183
182
|
specs[field] = spec;
|
|
184
183
|
});
|
|
185
184
|
return specs;
|