UNPKG

1.3 kBPlain TextView Raw
1import { _hb, CommonLogger, NumberStack } from '@naturalcycles/js-lib'
2import { yellow } from '../colors'
3import { gzipBuffer } from '../util/zip.util'
4
5export class SizeStack extends NumberStack {
6 constructor(public name: string, size: number) {
7 super(size)
8 }
9
10 total = 0
11
12 override push(item: any): this {
13 this.total += item
14 return super.push(item)
15 }
16
17 getStats(): string {
18 // const pcs = this.percentiles([50, 90])
19
20 return [
21 ' ' + this.name,
22 'avg',
23 yellow(_hb(this.avg())),
24 // 'p50',
25 // yellow(_hb(pcs[50])),
26 // 'p90',
27 // yellow(_hb(pcs[90])),
28 'total',
29 yellow(_hb(this.total)),
30 ].join(' ')
31 }
32
33 static async countItem(
34 item: any,
35 logger: CommonLogger,
36 sizes?: SizeStack,
37 sizesZipped?: SizeStack,
38 ): Promise<void> {
39 if (!sizes) return
40
41 // try-catch, because we don't want to fail the pipeline on logProgress
42 try {
43 const buf = Buffer.from(JSON.stringify(item))
44 sizes.push(buf.byteLength)
45
46 if (sizesZipped) {
47 const { byteLength } = await gzipBuffer(buf)
48 sizesZipped.push(byteLength)
49 }
50 } catch (err) {
51 logger.warn(
52 `transformLogProgress failed to JSON.stringify the chunk: ${(err as Error).message}`,
53 )
54 }
55 }
56}