@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
46 lines (45 loc) • 1.58 kB
JavaScript
import { _hb } from '@naturalcycles/js-lib';
import { _ms } from '@naturalcycles/js-lib/datetime/time.util.js';
import { boldWhite, dimWhite } from '../../colors/colors.js';
export class NDJsonStats {
static create(o = {}) {
return Object.assign(new NDJsonStats(), o);
}
static empty() {
return new NDJsonStats();
}
static createCombined(stats) {
// oxlint-disable-next-line unicorn/no-array-reduce
return stats.reduce((statsTotal, stats) => statsTotal.add(stats), new NDJsonStats());
}
tookMillis = 0;
rows = 0;
sizeBytes = 0;
get rpsTotal() {
return Math.round(this.rows / ((this.tookMillis || 1) / 1000));
}
get bpsTotal() {
return this.sizeBytes === 0 ? 0 : Math.round(this.sizeBytes / ((this.tookMillis || 1) / 1000));
}
get avgBytesPerRow() {
return Math.round(this.sizeBytes / this.rows);
}
/**
* Non-mutating addition, returns new object
*/
add(s) {
return NDJsonStats.create({
tookMillis: this.tookMillis + s.tookMillis,
rows: this.rows + s.rows,
sizeBytes: this.sizeBytes + s.sizeBytes,
});
}
toPretty(name) {
return [
`Processed ${name ? boldWhite(name) + ': ' : ''}${dimWhite(this.rows)} rows, ${dimWhite(_hb(this.sizeBytes))} in ${dimWhite(_ms(this.tookMillis))}`,
dimWhite(this.rpsTotal + ' rows/sec'),
dimWhite(_hb(this.avgBytesPerRow) + '/row'),
dimWhite(_hb(this.bpsTotal) + '/sec'),
].join(', ');
}
}