Skip to content

Instantly share code, notes, and snippets.

@umegaya
Last active June 21, 2019 01:16
Show Gist options
  • Save umegaya/b8aad13bf94b0be11535075df46533ce to your computer and use it in GitHub Desktop.
Save umegaya/b8aad13bf94b0be11535075df46533ce to your computer and use it in GitHub Desktop.
ドキュメントの内容を解析して勝手に解析するjs
こういうのがあると
```
---------- mhttp DL ----------
1174|1466|400|700|667
1172|1067|333|633|801
752|734|299|401|633
781|600|1234|1333|600
1188|631|335|799|934
```
こういう風に集計する。
```
---------- mhttp DL ----------
1174|1466|400|700|667 => 3233
1172|1067|333|633|801 => 2834
752|734|299|401|633 => 2067
781|600|1234|1333|600 => 3767
1188|631|335|799|934 => 2699
total: 14600
```
具体的なルールは
1. ----------で始まると集計モードとなる
2. 集計モードの時|で区切られている要素を数字として足し合わせる(今は1つ目の要素は無視する)そして `=> その数字` という形でオリジナルの行に追加して出力する
3. さらに|で区切られている要素がなくなると、その集計モードの時に足し合わせていた|で区切られていた数字を全部足し合わせたものを出力する
enjoy!
``` js
const fs = require('fs')
const texts = fs.readFileSync(process.argv[2]).toString().split(/\r\n|\n/);
let rawDataSampleType = null;
let rawDataSamples;
texts.forEach(l => {
const idx = l.indexOf('----------');
if (idx === 0) {
if (l.indexOf("DL") !== -1) {
rawDataSampleType = "DL";
} else if (l.indexOf("API") !== -1) {
rawDataSampleType = "API";
} else {
throw new Error(`invalid summery type ${l}`);
}
rawDataSamples = [];
} else if (rawDataSampleType !== null) {
const m = l.match(/[|0-9]/)
if (m) {
var samples = l.split('|').map(e => Number(e));
rawDataSamples.push(samples);
console.log(`${l} => ${samples.reduce((a, c, i) => a + (i == 0 ? 0 : c), 0)}`);
} else {
let total = 0, hs_total = 0;
rawDataSamples.forEach(s => {
hs_total += s[0];
total += s.reduce((a, c, i) => a + (i == 0 ? 0 : c), 0);
});
if (rawDataSampleType === "DL") {
console.log(`${l} total: ${total} => ${6801415 / total} bytes/sec`);
} else if (rawDataSampleType === "API") {
console.log(`${l} total: ${total} => ${total / 20} ms/call, hs: ${hs_total} => ${hs_total / 5} ms/call`);
}
rawDataSampleType = null;
}
return;
}
console.log(l);
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment