summaryrefslogtreecommitdiffhomepage
path: root/tasks/benchmark.rake
blob: f682f8ee1c393ea547762acc1102dd81edfe8bc3 (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
module MRuby
  BENCHMARK_REPEAT = 2
end

$dat_files = []

def bm_files
  Dir.glob("#{MRUBY_ROOT}/benchmark/*.rb")
end

def plot_file
  File.join(MRUBY_ROOT, 'benchmark', 'bm.pdf')
end

def plot
  opts_file = "#{MRUBY_ROOT}/benchmark/plot.gpl"
  opts = File.read(opts_file).each_line.to_a.map(&:strip).join(';')
  opts += ';plot '

  dat_files = $dat_files.group_by {|f| File.dirname(f).split(File::SEPARATOR)[-1]}

  opts += dat_files.keys.map do |data_file|
    %Q['-' u 2:3:4:xtic(1) w hist title columnheader(1)]
  end.join(',')
  opts += ';'

  cmd = %Q{gnuplot -p -e "#{opts}"}

  IO.popen(cmd, 'w') do |p|
    dat_files.each do |target_name, bm_files|
      p.puts target_name
      bm_files.each do |bm_file|
        puts target_name
        p.write File.read(bm_file)
        print File.read(bm_file)
      end
      p.puts "e"
    end
  end
end


MRuby.each_target do |target|
  mruby_path = "#{target.build_dir}/bin/mruby"
  if File.file? mruby_path
    bm_files.each do |bm_file|
      bm_name = File.basename bm_file, ".rb"

      dat_dir = File.join('benchmark', target.name)
      dat_file = File.join(dat_dir, "#{bm_name}.dat")
      $dat_files << dat_file

      directory dat_dir

      file dat_file => [bm_file, dat_dir] do |task|
        print bm_name
        puts "..."

        data = (0...MRuby::BENCHMARK_REPEAT).map do |n|
          str = %x{(time -f "%e %S %U" #{mruby_path} #{bm_file}) 2>&1 >/dev/null}
          str.split(' ').map(&:to_f)
        end

        File.open(task.name, "w") do |f|
          data = data.map {|_,r,s| (r + s) / 2.0}
          min = data.min
          max = data.max
          avg = data.inject(&:+) / data.size
          f.puts "#{bm_name.gsub('_', '-')} #{avg} #{min} #{max}"
        end
      end
    end
  end
end

file plot_file => $dat_files do
  plot
end

task :benchmark => plot_file do
  plot
end