Class: Benchmark::BigO::TermPlot

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/bigo/termplot.rb

Instance Method Summary collapse

Constructor Details

#initialize(report_data, sizes) ⇒ TermPlot

Returns a new instance of TermPlot.



5
6
7
8
9
# File 'lib/benchmark/bigo/termplot.rb', line 5

def initialize report_data, sizes
  @data = report_data
  @sizes = sizes
  @dat_file = Tempfile.new 'termplot'
end

Instance Method Details

#commandsObject



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
# File 'lib/benchmark/bigo/termplot.rb', line 44

def commands

  cmds = []
  cmds << "set term dumb"
  cmds << "set key top left vertical inside width 3"

  data_idx = 0

  while data_idx < @data.length

    if data_idx == 0
      str = "plot '#{@dat_file.path}'"
    else
      str = "\"\""
    end

    str += " using 1:#{data_idx+2} title '#{title_for(data_idx)}' with lines"
    str += ",\\" unless data_idx == @data.length-1

    cmds << str

    data_idx += 1
  end

  cmds
end

#dat_linesObject



31
32
33
34
35
36
37
38
# File 'lib/benchmark/bigo/termplot.rb', line 31

def dat_lines
  lines = []
  @sizes.each do |size|
    line = [size] + @data.collect{|d| d[:data][size] }
    lines << line.join('   ')
  end
  lines
end

#generateObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/benchmark/bigo/termplot.rb', line 11

def generate

  write_dat_file

  begin
    IO.popen("gnuplot", "w") { |io| io.puts commands }
  rescue
    puts "You need to have gnuplot installed!"
    puts "brew install gnuplot"
  end
end

#title_for(index) ⇒ Object



40
41
42
# File 'lib/benchmark/bigo/termplot.rb', line 40

def title_for index
  @data[index][:name]
end

#write_dat_fileObject



23
24
25
26
27
28
29
# File 'lib/benchmark/bigo/termplot.rb', line 23

def write_dat_file
  File.open(@dat_file.path, 'w') do |file|
    dat_lines.each do |line|
      file.puts line
    end
  end
end