Created
July 1, 2010 18:29
-
-
Save jbalogh/460353 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
logs = {} | |
regex = re.compile('z.timer:INFO (?P<method>\w+) "(?P<url>[^"]+)" ' | |
'\((?P<code>\d+)\) (?P<time>[\d.]+) :') | |
def read(line): | |
match = regex.search(line) | |
if match: | |
method, url, code, time = match.groups() | |
logs.setdefault(url, {'times': [], 'methods': {}}) | |
logs[url]['times'].append(float(time)) | |
m = logs[url]['methods'].setdefault(method, {}) | |
m.setdefault(code, 0) | |
m[code] += 1 | |
for line in open(sys.argv[1]): | |
read(line) | |
for stats in logs.values(): | |
t = stats['times'] | |
stats['max'] = max(t) | |
stats['count'] = cnt = len(t) | |
stats['total'] = sum(t) | |
stats['avg'] = avg = stats['total'] / stats['count'] | |
stats['stddev'] = (sum((x - avg) ** 2 for x in t) / cnt) ** .5 | |
key = lambda f: (f[1]['count'], f[1]['avg']) | |
headings = ''.join('<th>%s</th>' % x for x in 'url avg count total max stddev responses'.split()) | |
rows = [] | |
for url, s in sorted(logs.items(), key=key, reverse=True): | |
if not (s['total'] > 0 and s['count'] > 20): | |
continue | |
dl = [] | |
for method, counts in s['methods'].items(): | |
dl.append('<dt>%s</dt>' % method) | |
for code, count in counts.items(): | |
dl.append('<dd><b>%s</b>: %s' % (code, count)) | |
dl = '<dl>%s</dl>' % ''.join(dl) | |
r = '<tr><td>%s</td><td>%.2f</td><td>%s</td><td>%.2f</td><td>%.2f</td><td>%.2f</td><td>%s</td></tr>' % ( | |
url, s['avg'], s['count'], s['total'], s['max'], s['stddev'], dl) | |
rows.append(r) | |
h = '<style>tr:nth-child(even){ background-color: #ddd; }</style><code><table><thead>%s</thead><tbody>%s</tbody></table></code>' % (headings, ''.join(rows)) | |
open('public_html/woo.html', 'w').write(h) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment