Skip to content

Instantly share code, notes, and snippets.

@pinheadmz
Created September 30, 2021 17:17
Show Gist options
  • Save pinheadmz/27499ab32381f02a318f3e936e90d039 to your computer and use it in GitHub Desktop.
Save pinheadmz/27499ab32381f02a318f3e936e90d039 to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const path = require('path');
const readline = require('readline');
(async () => {
const DIR = process.argv[2];
if (!DIR)
throw new Error('Usage:\n$ node log-parse.js <path/to/.hsd directory>');
const files =
fs.readdirSync(DIR)
.filter(name => name.indexOf('debug') !== -1);
// First file lexicographically is `debug.log` but that is last file created
const last = files.shift();
files.push(last);
let start, end;
let time = 0;
let current = 0;
const STOP = 80000;
outer:
for (const FILE of files) {
const fileStream = fs.createReadStream(path.join(DIR, FILE));
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
for await (const line of rl) {
const words = line.split(' ');
if (!start) {
const timestampchunk = words[0];
const timestamp = timestampchunk.match((/(?<=\[.*:).*?(?=\])/g))[0];
const date = new Date(timestamp);
start = date.getTime();
}
if (words[5] !== 'added')
continue;
const blockchunk = words[4];
const block = parseInt(blockchunk.match((/(?<=\().*?(?=\))/g))[0]);
if ((current + 1) !== block)
throw new Error(`Consistency error: current=${current} block=${block}`);
current = block;
const wordchunk = words[10];
const blocktime = wordchunk.match(/(?<==).*?(?=\))/g)[0];
time += parseFloat(blocktime);
if (block === STOP) {
const timestampchunk = words[0];
const timestamp = timestampchunk.match((/(?<=\[.*:).*?(?=\])/g))[0];
const date = new Date(timestamp);
end = date.getTime();
console.log(
`Height: ${block} ` +
`Total block processing time: ${time} (${time/1000/60/60} hours)`
);
break outer;
}
}
}
console.log(
'Total time elapsed: ' +
`${end - start}ms (${(end - start)/1000/60/60} hours)`
);
})().catch((err) => {
console.error(err.message);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment