-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.js
85 lines (79 loc) · 2.41 KB
/
metrics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { createReadStream, } from 'fs';
import { createGunzip } from 'zlib';
import { Transform } from 'stream';
import { pipeline } from 'stream/promises'
import LineSplitStream from './line-split-stream.js';
const filename = process.argv[2];
const firstRound = Number(process.argv[3] ?? 0);
const lastRound = Number(process.argv[4] ?? Infinity);
let lineCounter=0;
const LOG_EVERY = 50_000;
const metricsData = {};
// actual processor. stream that accepts a log line (JSON as string)
export default class ProcessLineStream extends Transform {
constructor() {
super({ objectMode: true });
}
async _transform(line, encoding, callback) {
// uncomment to log progress
lineCounter++;
if (lineCounter % LOG_EVERY === 0) {
console.warn(`Ingested ${lineCounter}`);
}
if (!line.trim())
return callback();
try {
if (!(line.includes("Metrics"))) {
return callback();
}
const data = JSON.parse(line);
let { details, time } = data;
if (!details?.Metrics) {
return callback();
}
const { m } = details;
const { algod_ledger_round: round } = m;
if (firstRound <= round && round <= lastRound) {
metricsData[round] = m;
}
} catch(e) {
console.error(`Error ${e.message} line ${line}`);
}
callback();
}
}
try {
const processStream = new ProcessLineStream();
const lineStream = new LineSplitStream(processStream);
const fileStream = createReadStream(filename);
const pipes = [fileStream, filename.endsWith('gz') ? createGunzip() : null, lineStream, processStream].filter(Boolean);
// run stream, populates intermediate per-step voters{}
await pipeline(pipes);
} catch(e) {
console.error(e);
process.exit(1);
}
const header = ['metric', ...Object.keys(metricsData).map((f, i) => i>0 ? `${f},delta` : f)].join(',');
const rows = [];
if (!Object.keys(metricsData).length) {
console.warn("No metrics found");
process.exit(1);
}
const firstMetricsData = metricsData[Object.keys(metricsData)[0]];
const fields = Object.keys(firstMetricsData);
for(const field of fields) {
const row = [field];
let prev;
for(const [rnd, m] of Object.entries(metricsData)) {
let delta;
if (prev !== undefined)
delta = m[field] - prev;
row.push(m[field]);
if (delta !== undefined)
row.push(delta);
prev = m[field];
}
rows.push(row.join(','));
}
console.log(header);
console.log(rows.join('\n'));