UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.NDJsonStats = void 0;
4const js_lib_1 = require("@naturalcycles/js-lib");
5const colors_1 = require("../../colors");
6class NDJsonStats {
7 constructor() {
8 this.tookMillis = 0;
9 this.rows = 0;
10 this.sizeBytes = 0;
11 }
12 static create(o = {}) {
13 return Object.assign(new NDJsonStats(), o);
14 }
15 static empty() {
16 return new NDJsonStats();
17 }
18 static createCombined(stats) {
19 // eslint-disable-next-line unicorn/no-array-reduce
20 return stats.reduce((statsTotal, stats) => statsTotal.add(stats), new NDJsonStats());
21 }
22 get rpsTotal() {
23 return Math.round(this.rows / ((this.tookMillis || 1) / 1000));
24 }
25 get bpsTotal() {
26 return this.sizeBytes === 0 ? 0 : Math.round(this.sizeBytes / ((this.tookMillis || 1) / 1000));
27 }
28 get avgBytesPerRow() {
29 return Math.round(this.sizeBytes / this.rows);
30 }
31 /**
32 * Non-mutating addition, returns new object
33 */
34 add(s) {
35 return NDJsonStats.create({
36 tookMillis: this.tookMillis + s.tookMillis,
37 rows: this.rows + s.rows,
38 sizeBytes: this.sizeBytes + s.sizeBytes,
39 });
40 }
41 toPretty(name) {
42 return [
43 `Processed ${name ? colors_1.boldWhite(name) + ': ' : ''}${colors_1.dimWhite(this.rows)} rows, ${colors_1.dimWhite(js_lib_1._hb(this.sizeBytes))} in ${colors_1.dimWhite(js_lib_1._ms(this.tookMillis))}`,
44 `${colors_1.dimWhite(this.rpsTotal + ' rows/sec')}`,
45 `${colors_1.dimWhite(js_lib_1._hb(this.avgBytesPerRow) + '/row')}`,
46 `${colors_1.dimWhite(js_lib_1._hb(this.bpsTotal) + '/sec')}`,
47 ].join(', ');
48 }
49}
50exports.NDJsonStats = NDJsonStats;