@naturalcycles/bench-lib 1.6.7 → 2.0.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.
@@ -46,7 +46,6 @@ export interface RunBenchOptions {
46
46
  *
47
47
  * @default true
48
48
  */
49
- writePlot?: boolean;
50
49
  /**
51
50
  * Plot ascii chart in the terminal:)
52
51
  *
@@ -5,8 +5,6 @@ const tslib_1 = require("tslib");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
7
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"));
10
8
  const asciiChart_util_1 = require("./asciiChart.util");
11
9
  /**
12
10
  * Wraps `runBench` in `runScript` for convenience, so it can be run in top-level without `await`.
@@ -24,7 +22,7 @@ exports.runBenchScript = runBenchScript;
24
22
  * Only DeferredFunctions are allowed, because of: https://github.com/bestiejs/benchmark.js/issues/111
25
23
  */
26
24
  async function runBench(opt) {
27
- const { runs = 1, writeSummary = true, writePlot = true, asciiPlot = true, name = 'runBench', } = opt;
25
+ const { runs = 1, writeSummary = true, asciiPlot = true, name = 'runBench' } = opt;
28
26
  const { reportDirPath = `./tmp/${name}` } = opt;
29
27
  console.log(`running benchmark...\n\n`);
30
28
  const results = [];
@@ -45,20 +43,23 @@ async function runBench(opt) {
45
43
  // names.forEach(name => {
46
44
  // summary[name] = results.hz.map(map => map[name]!)
47
45
  // })
48
- (0, nodejs_lib_1._ensureDirSync)(reportDirPath);
46
+ nodejs_lib_1.fs2.ensureDir(reportDirPath);
49
47
  const summaryJsonPath = `${reportDirPath}/${name}.json`;
50
- (0, nodejs_lib_1._writeJsonSync)(summaryJsonPath, avg, { spaces: 2 });
48
+ nodejs_lib_1.fs2.writeJson(summaryJsonPath, avg, { spaces: 2 });
51
49
  console.log(`saved ${(0, nodejs_lib_1.dimGrey)(summaryJsonPath)}`);
52
50
  }
53
- if (writePlot) {
54
- (0, nodejs_lib_1._ensureDirSync)(reportDirPath);
55
- const spec = benchResultsToVegaSpec(avg);
56
- const view = new vega_1.default.View(vega_1.default.parse(spec), { renderer: 'none' });
57
- const svg = await view.toSVG();
58
- const plotPath = `${reportDirPath}/${name}.svg`;
59
- (0, nodejs_lib_1._writeFileSync)(plotPath, svg);
60
- console.log(`saved ${(0, nodejs_lib_1.dimGrey)(plotPath)}`);
61
- }
51
+ // Vega plots are currently disabled
52
+ // if (writePlot) {
53
+ // fs2.ensureDir(reportDirPath)
54
+ //
55
+ // const spec = benchResultsToVegaSpec(avg)
56
+ // const view = new vega.View(vega.parse(spec), { renderer: 'none' })
57
+ // const svg = await view.toSVG()
58
+ //
59
+ // const plotPath = `${reportDirPath}/${name}.svg`
60
+ // fs2.writeFile(plotPath, svg)
61
+ // console.log(`saved ${dimGrey(plotPath)}`)
62
+ // }
62
63
  if (asciiPlot) {
63
64
  console.log('\n' + (0, asciiChart_util_1.plotAsciiChart)(avg));
64
65
  }
@@ -100,38 +101,43 @@ async function runBenchOnce(opt, run) {
100
101
  });
101
102
  return await defer;
102
103
  }
103
- function benchResultsToVegaSpec(map) {
104
- const values = Object.entries(map).map(([name, hz]) => {
105
- return {
106
- name,
107
- hz,
108
- };
109
- });
110
- // console.log(values)
111
- const liteSpec = {
112
- // title: 'title',
113
- // "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
114
- data: {
115
- values,
104
+ /*
105
+ function benchResultsToVegaSpec(map: HertzMap): Spec {
106
+ const values = Object.entries(map).map(([name, hz]) => {
107
+ return {
108
+ name,
109
+ hz,
110
+ }
111
+ })
112
+
113
+ // console.log(values)
114
+
115
+ const liteSpec: TopLevelSpec = {
116
+ // title: 'title',
117
+ // "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
118
+ data: {
119
+ values,
120
+ },
121
+ mark: 'bar',
122
+ encoding: {
123
+ y: {
124
+ field: 'name',
125
+ type: 'ordinal',
126
+ axis: {
127
+ title: '',
116
128
  },
117
- mark: 'bar',
118
- encoding: {
119
- y: {
120
- field: 'name',
121
- type: 'ordinal',
122
- axis: {
123
- title: '',
124
- },
125
- },
126
- x: {
127
- field: 'hz',
128
- type: 'quantitative',
129
- axis: {
130
- title: 'ops/sec',
131
- },
132
- },
129
+ },
130
+ x: {
131
+ field: 'hz',
132
+ type: 'quantitative',
133
+ axis: {
134
+ title: 'ops/sec',
133
135
  },
134
- };
135
- const { spec } = vega_lite_1.default.compile(liteSpec);
136
- return spec;
136
+ },
137
+ },
138
+ }
139
+
140
+ const { spec } = vegaLite.compile(liteSpec)
141
+ return spec
137
142
  }
143
+ */
@@ -81,7 +81,6 @@ export interface RunCannonOptions {
81
81
  *
82
82
  * @default true
83
83
  */
84
- writePlots?: boolean;
85
84
  /**
86
85
  * Will write to ${reportDirPath}/${name}.${rawSummary}.json
87
86
  * It is a raw summary from Autocannon.
@@ -1,11 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runCannon = exports.runCannonScript = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const js_lib_1 = require("@naturalcycles/js-lib");
6
5
  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"));
9
6
  /**
10
7
  * Wraps `runBench` in `runScript` for convenience, so it can be run in top-level without `await`.
11
8
  */
@@ -22,7 +19,6 @@ async function runCannon(profiles, optInput = {}) {
22
19
  const opt = {
23
20
  name: 'Benchmark',
24
21
  reportDirPath: `./tmp/${optInput.name || 'Benchmark'}`,
25
- writePlots: true,
26
22
  writeSummary: true,
27
23
  writeRawSummary: true,
28
24
  runs: 2,
@@ -52,7 +48,7 @@ async function runCannon(profiles, optInput = {}) {
52
48
  });
53
49
  }
54
50
  const { reportDirPath } = opt;
55
- (0, nodejs_lib_1._ensureDirSync)(reportDirPath);
51
+ nodejs_lib_1.fs2.ensureDir(reportDirPath);
56
52
  const resultByProfile = {};
57
53
  const summaries = [];
58
54
  for await (const profileName of Object.keys(profiles)) {
@@ -66,15 +62,15 @@ async function runCannon(profiles, optInput = {}) {
66
62
  console.table(summaries);
67
63
  if (opt.writeSummary) {
68
64
  const summaryJsonPath = `${reportDirPath}/${opt.name}.summary.json`;
69
- (0, nodejs_lib_1._writeJsonSync)(summaryJsonPath, summaries, { spaces: 2 });
65
+ nodejs_lib_1.fs2.writeJson(summaryJsonPath, summaries, { spaces: 2 });
70
66
  console.log(`saved ${(0, nodejs_lib_1.dimGrey)(summaryJsonPath)}`);
71
67
  }
72
68
  if (opt.writeRawSummary) {
73
- (0, nodejs_lib_1._writeJsonSync)(`${reportDirPath}/${opt.name}.rawSummary.json`, resultByProfile, { spaces: 2 });
74
- }
75
- if (opt.writePlots) {
76
- await writePlotFiles(opt, summaries);
69
+ nodejs_lib_1.fs2.writeJson(`${reportDirPath}/${opt.name}.rawSummary.json`, resultByProfile, { spaces: 2 });
77
70
  }
71
+ // if (opt.writePlots) {
72
+ // await writePlotFiles(opt, summaries)
73
+ // }
78
74
  return summaries;
79
75
  }
80
76
  exports.runCannon = runCannon;
@@ -132,64 +128,85 @@ function toSummary(name, result) {
132
128
  '2xx': result['2xx'],
133
129
  };
134
130
  }
135
- async function writePlotFiles(opt, summaries) {
136
- const { reportDirPath, name } = opt;
137
- (0, nodejs_lib_1._ensureDirSync)(reportDirPath);
138
- let fields = ['rpsAvg', 'latencyAvg', 'latency50', 'latency90', 'latency99', 'throughputAvg'];
139
- if (!opt.includeLatencyPercentiles) {
140
- fields = fields.filter(f => !f.startsWith('latency') || f === 'latencyAvg');
141
- }
142
- const specs = autocannonSummaryToVegaSpecs(fields, summaries);
143
- await (0, js_lib_1.pMap)(Object.keys(specs), async (specName) => {
144
- // create a new view instance for a given Vega JSON spec
145
- const view = new vega_1.default.View(vega_1.default.parse(specs[specName]), { renderer: 'none' });
146
- // generate a static SVG image
147
- const svg = await view.toSVG();
148
- // console.log(svg)
149
- (0, nodejs_lib_1._writeFileSync)(`${reportDirPath}/${name}.${specName}.svg`, svg);
150
- });
151
- (0, nodejs_lib_1._writeFileSync)(`${reportDirPath}/${name}.md`, mdContent(opt, Object.keys(specs)));
131
+ /*
132
+ async function writePlotFiles(
133
+ opt: RunCannonNormalizedOptions,
134
+ summaries: AutocannonSummary[],
135
+ ): Promise<void> {
136
+ const { reportDirPath, name } = opt
137
+ fs2.ensureDir(reportDirPath)
138
+
139
+ let fields = ['rpsAvg', 'latencyAvg', 'latency50', 'latency90', 'latency99', 'throughputAvg']
140
+ if (!opt.includeLatencyPercentiles) {
141
+ fields = fields.filter(f => !f.startsWith('latency') || f === 'latencyAvg')
142
+ }
143
+
144
+ const specs = autocannonSummaryToVegaSpecs(fields, summaries)
145
+
146
+ await pMap(Object.keys(specs), async specName => {
147
+ // create a new view instance for a given Vega JSON spec
148
+ const view = new vega.View(vega.parse(specs[specName]!), { renderer: 'none' })
149
+
150
+ // generate a static SVG image
151
+ const svg = await view.toSVG()
152
+ // console.log(svg)
153
+
154
+ fs2.writeFile(`${reportDirPath}/${name}.${specName}.svg`, svg)
155
+ })
156
+
157
+ fs2.writeFile(`${reportDirPath}/${name}.md`, mdContent(opt, Object.keys(specs)))
152
158
  }
153
- function autocannonSummaryToVegaSpecs(fields, summaries) {
154
- // console.log(summary)
155
- const specs = {};
156
- fields.forEach(field => {
157
- const liteSpec = {
158
- // title: 'title',
159
- // "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
160
- data: {
161
- values: summaries,
162
- },
163
- mark: 'bar',
164
- encoding: {
165
- y: {
166
- field: 'name',
167
- type: 'ordinal',
168
- axis: {
169
- title: '',
170
- },
171
- },
172
- x: {
173
- field,
174
- type: 'quantitative',
175
- axis: {
176
- title: field,
177
- },
178
- },
179
- },
180
- };
181
- const { spec } = vega_lite_1.default.compile(liteSpec);
182
- specs[field] = spec;
183
- });
184
- return specs;
159
+
160
+ function autocannonSummaryToVegaSpecs(
161
+ fields: string[],
162
+ summaries: AutocannonSummary[],
163
+ ): StringMap<Spec> {
164
+ // console.log(summary)
165
+
166
+ const specs: StringMap<Spec> = {}
167
+
168
+ fields.forEach(field => {
169
+ const liteSpec: TopLevelSpec = {
170
+ // title: 'title',
171
+ // "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
172
+ data: {
173
+ values: summaries,
174
+ },
175
+ mark: 'bar',
176
+ encoding: {
177
+ y: {
178
+ field: 'name',
179
+ type: 'ordinal',
180
+ axis: {
181
+ title: '',
182
+ },
183
+ },
184
+ x: {
185
+ field,
186
+ type: 'quantitative',
187
+ axis: {
188
+ title: field,
189
+ },
190
+ },
191
+ },
192
+ }
193
+
194
+ const { spec } = vegaLite.compile(liteSpec)
195
+ specs[field] = spec
196
+ })
197
+
198
+ return specs
185
199
  }
186
- function mdContent(opt, specNames) {
187
- return [
188
- `# ${opt.name}`,
189
- opt.beforeText,
190
- specNames.map(specName => `![](./${opt.name}.${specName}.svg)`).join('\n'),
191
- opt.afterText,
192
- ]
193
- .filter(Boolean)
194
- .join('\n\n');
200
+
201
+
202
+ function mdContent(opt: RunCannonNormalizedOptions, specNames: string[]): string {
203
+ return [
204
+ `# ${opt.name}`,
205
+ opt.beforeText,
206
+ specNames.map(specName => `![](./${opt.name}.${specName}.svg)`).join('\n'),
207
+ opt.afterText,
208
+ ]
209
+ .filter(Boolean)
210
+ .join('\n\n')
195
211
  }
212
+ */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/bench-lib",
3
3
  "scripts": {
4
- "prepare": "husky install"
4
+ "prepare": "husky"
5
5
  },
6
6
  "dependencies": {
7
7
  "@naturalcycles/js-lib": "^14.0.0",
@@ -9,9 +9,7 @@
9
9
  "autocannon": "^7.0.0",
10
10
  "benchmark": "^2.1.4",
11
11
  "express": "^4.17.1",
12
- "helmet": "^7.0.0",
13
- "vega": "^5.10.1",
14
- "vega-lite": "^5.0.0"
12
+ "helmet": "^7.0.0"
15
13
  },
16
14
  "devDependencies": {
17
15
  "@naturalcycles/dev-lib": "^13.6.0",
@@ -36,7 +34,7 @@
36
34
  "engines": {
37
35
  "node": ">=18.12.0"
38
36
  },
39
- "version": "1.6.7",
37
+ "version": "2.0.0",
40
38
  "description": "Benchmarking library, based on Benchmark.js and Autocannon",
41
39
  "author": "Natural Cycles Team",
42
40
  "license": "MIT"