Skip to content

Instantly share code, notes, and snippets.

@jsok
Created July 2, 2012 00:47
Show Gist options
  • Save jsok/3030263 to your computer and use it in GitHub Desktop.
Save jsok/3030263 to your computer and use it in GitHub Desktop.
RoR log parser
A dirty little script to grab performance stats from Rails production.log file.
To speed up I recommend grep'ing out relevant lines:
egrep ('Started'|'Completed') production.log > output.txt
Will give you a CSV file which you can quickly plot in Excel.
import io
import sys
log = open('production.log', 'r')
start = 0
completed = 0
reqs = []
tmpl = {
'url': None,
'date': None,
'time': None,
'total time': None,
'views time': None,
'AR time': None,
}
current = None
for line in log.readlines():
parts = line.split(' ')
if line.startswith("Started"):
if len(parts) < 9:
continue
current = tmpl.copy()
current['url'] = parts[2]
current['time'] = parts[-2]
current['date'] = parts[-3]
start += 1
elif line.startswith("Completed"):
if len(parts) < 10:
current = tmpl.copy()
continue
current['total time'] = parts[4]
current['views time'] = parts[6]
current['AR time'] = parts[-1].split(')')[0]
completed += 1
reqs.append(current)
log.close()
print "Started: %d Completed: %d" % (start, completed)
csv = open("reqs.csv", 'w')
for entry in reqs:
csv.write("%s, %s, %s, %s, %s, %s\n" % (
entry['url'],
entry['date'],
entry['time'],
entry['total time'],
entry['views time'],
entry['AR time']
))
csv.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment