summaryrefslogtreecommitdiffstats
path: root/storage/mroonga/vendor/groonga/benchmark/geo-distance-summary.rb
blob: 6559cb60f1709d949b555ef24b2c2122b626f674 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'fileutils'
require 'optparse'

class BenchmarkSummary
  attr_accessor :options

  def initialize(options)
    @options = options
    @options[:count] ||= 10
  end

  def calc_total(data)
    total = {}
    @options[:count].times do |i|
      data[i + 1].each do |key, value|
        if total[key]
          total[key] = total[key] + value
        else
          total[key] = value
        end
      end
    end
    total
  end

  def print_average(data)
    total = calc_total(data)
    text = "|項目|"
    @options[:count].times do |i|
      text << "#{i + 1}|"
      text << "平均|\n" if i == @options[:count] - 1
    end
    total.each do |key, value|
      line = [key]
      @options[:count].times do |i|
        data[i + 1].each do |data_key, data_value|
          if key == data_key
            line << data_value
          end
        end
      end
      line << [value / @options[:count].to_f]
      text << sprintf("|%s|\n", line.join("|"))
    end
    puts text
  end

  def print_performance(before, after)
    before_total = calc_total(before)
    after_total = calc_total(after)
    ratio = {}
    before_total.each do |key, value|
      ratio[key] = after_total[key] / value
    end
    text = "|項目|変更前|変更後|比率|備考|\n"
    ratio.each do |key, value|
      text << sprintf("|%s|%f|%f|%f||\n",
              key,
              before_total[key] / @options[:count].to_f,
              after_total[key] / @options[:count].to_f,
              ratio[key])
    end
    puts text
  end

  def parse_log(logs)
    parse_result = {}
    logs.each do |index, log|
      File.open(log, "r") do |file|
        data = file.read
        entry = {}
        data.split("\n").each do |line|
          if line =~ /\s*(.+?):\s+\((.+)\)/
            entry[$1] = $2.to_f
          end
        end
        parse_result[index] = entry
      end
    end
    parse_result
  end
end

=begin

Usage: geo-distance-summary.rb \
-b run-bench-geo-distance-orig-N1000 \
-a run-bench-geo-distance-work-N1000

NOTE: expected that there are run-bench-geo-distance-orig-N1000-1.log \
... \
run-bench-geo-distance-orig-N1000-[N].log.

=end

if __FILE__ == $0

  options = {}
  parser = OptionParser.new
  parser.on("-b", "--before PREFIX",
            "log file name must be PREFIX-[N].log") do |prefix|
    options[:before] = prefix
  end
  parser.on("-a", "--after PREFIX",
            "log file name must be PREFIX-[N].log") do |prefix|
    options[:after] = prefix
  end
  parser.on("-n", "data count") do |count|
    options[:count] = count
  end

  parser.parse!(ARGV)

  if not options.has_key?(:before) or not options.has_key?(:after)
    puts(parser.to_s)
    exit
  end

  bench_before_log = {}
  bench_after_log = {}
  Dir.glob("#{options[:before]}*.log") do |log|
    log =~ /(.+)-(\d+)\.log$/
    bench_before_log[$2.to_i] = log
  end
  Dir.glob("#{options[:after]}*.log") do |log|
    log =~ /(.+)-(\d+)\.log$/
    bench_after_log[$2.to_i] = log
  end

  bench = BenchmarkSummary.new(options)
  bench_before = bench.parse_log(bench_before_log)
  bench_after = bench.parse_log(bench_after_log)

  bench.print_average(bench_before)
  bench.print_average(bench_after)
  bench.print_performance(bench_before, bench_after)
end