| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
require 'fileutils' |
| 5 |
require 'optparse' |
| 6 |
|
| 7 |
class BenchmarkSummary |
| 8 |
attr_accessor :options |
| 9 |
|
| 10 |
def initialize(options) |
| 11 |
@options = options |
| 12 |
@options[:count] ||= 10 |
| 13 |
end |
| 14 |
|
| 15 |
def calc_total(data) |
| 16 |
total = {} |
| 17 |
@options[:count].times do |i| |
| 18 |
data[i + 1].each do |key, value| |
| 19 |
if total[key] |
| 20 |
total[key] = total[key] + value |
| 21 |
else |
| 22 |
total[key] = value |
| 23 |
end |
| 24 |
end |
| 25 |
end |
| 26 |
total |
| 27 |
end |
| 28 |
|
| 29 |
def print_average(data) |
| 30 |
total = calc_total(data) |
| 31 |
text = "|項目|" |
| 32 |
@options[:count].times do |i| |
| 33 |
text << "#{i + 1}|" |
| 34 |
text << "平均|\n" if i == @options[:count] - 1 |
| 35 |
end |
| 36 |
total.each do |key, value| |
| 37 |
line = [key] |
| 38 |
@options[:count].times do |i| |
| 39 |
data[i + 1].each do |data_key, data_value| |
| 40 |
if key == data_key |
| 41 |
line << data_value |
| 42 |
end |
| 43 |
end |
| 44 |
end |
| 45 |
line << [value / @options[:count].to_f] |
| 46 |
text << sprintf("|%s|\n", line.join("|")) |
| 47 |
end |
| 48 |
puts text |
| 49 |
end |
| 50 |
|
| 51 |
def print_performance(before, after) |
| 52 |
before_total = calc_total(before) |
| 53 |
after_total = calc_total(after) |
| 54 |
ratio = {} |
| 55 |
before_total.each do |key, value| |
| 56 |
ratio[key] = after_total[key] / value |
| 57 |
end |
| 58 |
text = "|項目|変更前|変更後|比率|備考|\n" |
| 59 |
ratio.each do |key, value| |
| 60 |
text << sprintf("|%s|%f|%f|%f||\n", |
| 61 |
key, |
| 62 |
before_total[key] / @options[:count].to_f, |
| 63 |
after_total[key] / @options[:count].to_f, |
| 64 |
ratio[key]) |
| 65 |
end |
| 66 |
puts text |
| 67 |
end |
| 68 |
|
| 69 |
def parse_log(logs) |
| 70 |
parse_result = {} |
| 71 |
logs.each do |index, log| |
| 72 |
File.open(log, "r") do |file| |
| 73 |
data = file.read |
| 74 |
entry = {} |
| 75 |
data.split("\n").each do |line| |
| 76 |
if line =~ /\s*(.+?):\s+\((.+)\)/ |
| 77 |
entry[$1] = $2.to_f |
| 78 |
end |
| 79 |
end |
| 80 |
parse_result[index] = entry |
| 81 |
end |
| 82 |
end |
| 83 |
parse_result |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if __FILE__ == $0 |
| 100 |
|
| 101 |
options = {} |
| 102 |
parser = OptionParser.new |
| 103 |
parser.on("-b", "--before PREFIX", |
| 104 |
"log file name must be PREFIX-[N].log") do |prefix| |
| 105 |
options[:before] = prefix |
| 106 |
end |
| 107 |
parser.on("-a", "--after PREFIX", |
| 108 |
"log file name must be PREFIX-[N].log") do |prefix| |
| 109 |
options[:after] = prefix |
| 110 |
end |
| 111 |
parser.on("-n", "data count") do |count| |
| 112 |
options[:count] = count |
| 113 |
end |
| 114 |
|
| 115 |
parser.parse!(ARGV) |
| 116 |
|
| 117 |
if not options.has_key?(:before) or not options.has_key?(:after) |
| 118 |
puts(parser.to_s) |
| 119 |
exit |
| 120 |
end |
| 121 |
|
| 122 |
bench_before_log = {} |
| 123 |
bench_after_log = {} |
| 124 |
Dir.glob("#{options[:before]}*.log") do |log| |
| 125 |
log =~ /(.+)-(\d+)\.log$/ |
| 126 |
bench_before_log[$2.to_i] = log |
| 127 |
end |
| 128 |
Dir.glob("#{options[:after]}*.log") do |log| |
| 129 |
log =~ /(.+)-(\d+)\.log$/ |
| 130 |
bench_after_log[$2.to_i] = log |
| 131 |
end |
| 132 |
|
| 133 |
bench = BenchmarkSummary.new(options) |
| 134 |
bench_before = bench.parse_log(bench_before_log) |
| 135 |
bench_after = bench.parse_log(bench_after_log) |
| 136 |
|
| 137 |
bench.print_average(bench_before) |
| 138 |
bench.print_average(bench_after) |
| 139 |
bench.print_performance(bench_before, bench_after) |
| 140 |
end |