Skip to content

Instantly share code, notes, and snippets.

@sonots
Created September 16, 2014 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonots/9ae0167d31d7f4b42c9a to your computer and use it in GitHub Desktop.
Save sonots/9ae0167d31d7f4b42c9a to your computer and use it in GitHub Desktop.
require 'uri'
stats = {}
STDIN.readlines.each do |line|
uri, reqtime = line.split("\t")
path = uri.split('?').first
reqtime = reqtime.to_f
stat = stats[path] ||= { :count => 0, :sum => 0, :min => 0, :max => 0}
stat[:sum] += reqtime
stat[:max] = [stat[:max], reqtime].max
stat[:min] = [stat[:min], reqtime].min
stat[:count] += 1
end
stats.each do |path, val|
val[:mean] = val[:sum] / val[:count].to_f
out = { :path => path }.merge(val)
puts out.map {|key, v| "#{key}:#{v}" }.join("\t")
end
@sonots
Copy link
Author

sonots commented Sep 16, 2014

$ cat /var/log/nginx/access.log | lltsv -k uri,reqtime -K | ruby stat.rb
path:/  count:2195      sum:23.611000000000107  min:0   max:0.089       mean:0.010756719817767702
path:/stylesheets/bootstrap.min.css     count:2403      sum:0.0 min:0   max:0   mean:0.0
path:/stylesheets/bootflat.min.css      count:2403      sum:0.0 min:0   max:0   mean:0.0
path:/stylesheets/isucon-bank.css       count:2403      sum:0.0 min:0   max:0   mean:0.0
path:/images/isucon-bank.png    count:2403      sum:0.0 min:0   max:0   mean:0.0
path:/login     count:1201      sum:355.7159999999995   min:0   max:0.56        mean:0.29618318068276395
path:/mypage    count:208       sum:63.59299999999999   min:0   max:0.492       mean:0.30573557692307685
path:/report    count:1 sum:18.965      min:0   max:18.965      mean:18.965

@sonots
Copy link
Author

sonots commented Sep 16, 2014

cat /var/log/nginx/access.log | lltsv -k uri,reqtime -K | ruby stat.rb  | lltsv -k sum,count,mean,path -K | sort -n -r
355.7159999999995       1201    0.29618318068276395     /login
63.59299999999999       208     0.30573557692307685     /mypage
23.611000000000107      2195    0.010756719817767702    /
18.965  1       18.965  /report
0.0     2403    0.0     /stylesheets/isucon-bank.css
0.0     2403    0.0     /stylesheets/bootstrap.min.css
0.0     2403    0.0     /stylesheets/bootflat.min.css
0.0     2403    0.0     /images/isucon-bank.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment