-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.rb
81 lines (64 loc) · 1.76 KB
/
histogram.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
69
70
71
72
73
74
75
76
77
78
79
80
81
require "histogram/array"
require "optparse"
def to_ms(nanos)
nanos / 1_000_000
end
def read_data(path)
data = {
:ttcs => [],
:ttfbs => [],
:deltas => [],
}
File.open(path, "r") do |f|
f.lines.each do |line|
ttc, ttfb = line.strip.split(/\s+/)
data[:ttcs] << to_ms(Integer(ttc))
data[:ttfbs] << to_ms(Integer(ttfb))
data[:deltas] << data[:ttfbs].last - data[:ttcs].last
end
end
data
end
options = {
:nbins => 64,
:bin_size => 50,
}
option_parser = OptionParser.new do |op|
op.banner = <<-EOT
Usage: histogram [options] <ttfb|ttc|delta> [<name> <file>]+
Outputs histogram data suitable for use with gnuplot
EOT
op.on("-b BINS", "Number of bins") do |nbins|
options[:nbins] = Integer(nbins)
end
op.on("-s BIN_SIZE", "Bin size (in ms)") do |bin_size|
options[:bin_size] = Float(bin_size)
end
end
option_parser.parse!(ARGV)
if (ARGV.size <= 1) || ((ARGV.size - 1) % 2) > 0
puts option_parser.help
exit 1
end
type = (ARGV.shift + "s").to_sym
unless [:ttfbs, :ttcs, :deltas].include?(type)
puts option_parser.help
exit 1
end
series = {}
ARGV.each_with_index.chunk { |x| Integer(x[1] / 2).even? }.each do |_, x|
series[x[0][0]] = read_data(x[1][0])
end
names = series.keys.sort
data = names.map { |n| series[n][type] }
bin_locs = (0..options[:nbins]).map { |ii| options[:bin_size] * ii }
bins, *freqs = data.first.histogram(bin_locs,
:tp => :min,
:other_sets => data.slice(1, data.size - 1))
fmt = "%10s"
title_parts = ["# "].concat(names.map { |n| fmt % [n] })
puts title_parts.join(" ")
bins.each_with_index do |bin, ii|
parts = [fmt % [bin]].concat(freqs.map { |freq| fmt % [freq[ii]] })
puts parts.join(" ")
end