-
Notifications
You must be signed in to change notification settings - Fork 0
/
served-stats.rb
executable file
·68 lines (53 loc) · 2.54 KB
/
served-stats.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
raise "Please specify a data file" unless ARGV.size > 0
file = ARGV[0]
require "uri"
req = {}
pad = {:uri => 0, :time => 0, :freq => 0, :avg => 0}
# this takes an explicit uri and tries to use pattern matching to make it into a generic uri
# e.g. /projects/foobar/cards/10 --> /projects/[:identifier]/cards/[:numeric]
# this allows us to make meaningful aggregations
def tokenize_uri(u)
u.gsub!(/^\/projects\/$/, "/projects")
u.gsub!(/\/(\d+)$/, "/[:numeric]")
u.gsub!(/\/(\d+)\//, "/[:numeric]/")
u.gsub!(/\/(\d+)\.xml$/, "/[:numeric].xml")
u.gsub!(/\/confirm_delete\/([\w]+)$/, "/confirm_delete/[:identifier]")
u.gsub!(/\/delete\/([\w]+)$/, "/delete/[:identifier]")
u.gsub!(/^\/projects\/([^\/]+)\//, "/projects/[:identifier]/")
u.gsub!(/^\/api\/v2\/projects\/([^\/]+)\//, "/api/v2/projects/[:identifier]/")
u.gsub!(/^\/api\/v2\/projects\/([^\/]+)\.xml$/, "/api/v2/projects/[:identifier].xml")
u.gsub!(/^\/programs\/([^\/]+)\//, "/programs/[:identifier]/")
u.gsub!(/^\/programs\/([^\/]+)\/projects\/([^\/]+)/, "/programs/[:identifier]/projects/[:identifier]")
u.gsub!(/^\/(projects|programs)\/(\w+)\/?$/, "/\\1/[:identifier]")
u.gsub!(/\/wiki\/([^\/]+)/, "/wiki/[:identifier]")
u.gsub!(/\/objectives\/([^\/]+)/, "/objectives/[:identifier]")
u.gsub!(/\/templatize\/([^\/]+)$/, "/templatize/[:identifier]")
u.gsub!(/\/feeds\/([^\/]+)\.atom$/, "/feeds/[:hash].atom")
u
end
File.open(file, "r") do |f|
f.each_line do |k|
data = k.split(" ")
time = data.shift
uri = URI.parse(data.shift[1..-2].split("?").first).path
uri = tokenize_uri(uri)
req[uri] ||= [0, 0, 0.0]
req[uri][0] += time.to_i # total time
req[uri][1] += 1 # count
req[uri][2] = (req[uri][0].to_f / req[uri][1].to_f).round(2) # avg per run
pad[:uri] = uri.size unless pad[:uri] >= uri.size
pad[:time] = req[uri][0].to_s.size unless pad[:time] >= req[uri][0].to_s.size
pad[:freq] = req[uri][1].to_s.size unless pad[:freq] >= req[uri][1].to_s.size
pad[:avg] = req[uri][2].to_s.size unless pad[:avg] >= req[uri][2].to_s.size
end
end
sorted_by_total_time = req.to_a.sort_by {|e| -e[1][0]}
File.open("served-results.log", "w") do |f|
header = "#{"uri".ljust(pad[:uri])} #{"time".to_s.rjust(pad[:time])} #{"freq".to_s.rjust(pad[:freq])} #{"avg".to_s.rjust(pad[:avg])}"
f.puts header
f.puts "-" * header.size
sorted_by_total_time.each do |pair|
f.puts "#{pair[0].ljust(pad[:uri])} #{pair[1][0].to_s.rjust(pad[:time])} #{pair[1][1].to_s.rjust(pad[:freq])} #{("%0.2f" % pair[1][2]).rjust(pad[:avg])}"
end
end