Skip to content

Instantly share code, notes, and snippets.

@olegp
Created February 25, 2012 10:50
Show Gist options
  • Save olegp/1907815 to your computer and use it in GitHub Desktop.
Save olegp/1907815 to your computer and use it in GitHub Desktop.
Time Combined Log Analyzer
var stream = require('fs-base').open(system.args[2]);
var stats = {}, paths = [], line;
while(line = stream.readLine()) {
try {
var l = line.substr(line.indexOf('"'));
l = l.split(/"([^"]+)"/);
var url = l[1].split(' ')[1].substr(1).split('/');
url = url.concat(url.pop().split('?'));
var time = +l[6].trim();
if(isNaN(time)) throw new Error('NaN ' + l[6].trim());
var component, s = stats, p = '';
while(component = url.shift()) {
p = p + '/' + component;
if(!s[component]) {
paths.push(s[component] = {time: 0, max: 0, count: 0, path: p, nodes: {}});
}
s = s[component];
s.time += time;
s.count ++;
s.max = time > s.max ? time : s.max;
s = s.nodes;
}
} catch(e) {
}
}
paths.forEach(function(e) {
e.average = e.time / e.count;
});
var cb = system.args[4] || 'time';
var result = paths.sort(function(a, b) {
if(a[cb] < b[cb]) return 1;
if(a[cb] > b[cb]) return -1;
return 0;
}).map(function(e) {
return e.time.toFixed(3) + ' ' + e.path + ' ' + e.count + ' ' + e.max.toFixed(3) + ' ' + e.average.toFixed(3);
});
for(var l = system.args[3] || result.length; l --;) {
console.log(result.shift());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment