Class: Benchmark::BigO::Chart

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

Constant Summary collapse

TYPES =
[:const, :logn, :n, :nlogn, :n_sq]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report_data, sizes) ⇒ Chart

Returns a new instance of Chart.



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

def initialize report_data, sizes
  @data = report_data.freeze
  @sizes = sizes.freeze

  @sample_size = @sizes.first

  # can't take log of 1,
  # so it can't be used as the sample
  if @sample_size == 1
    @sample_size = @sizes[1]
  end
end

Instance Attribute Details

#sample_sizeObject

Returns the value of attribute sample_size.



7
8
9
# File 'lib/benchmark/bigo/chart.rb', line 7

def sample_size
  @sample_size
end

Instance Method Details

#comparison_for(data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/benchmark/bigo/chart.rb', line 66

def comparison_for data
  sample = data[:data][@sample_size]

  comparison = [data]

  TYPES.each do |type|
    comparison << generate_data_for(type, sample)
  end

  comparison
end

#data_generator(type, n, sample) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/benchmark/bigo/chart.rb', line 106

def data_generator type, n, sample
  factor = factor_for(type, sample)

  case type
  when :const
    factor
  when :logn
    Math.log10(n) * factor

  when :n
    n * factor

  when :nlogn
    n * Math.log10(n) * factor

  when :n_sq
    n * n * factor

  end
end

#factor_for(type, sample) ⇒ Object

calculate the scaling factor for the given type and sample using sample_size



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/benchmark/bigo/chart.rb', line 128

def factor_for type, sample
  case type
  when :const
    sample.to_f
  when :logn
    sample.to_f/Math.log10(@sample_size)

  when :n
    sample.to_f/@sample_size

  when :nlogn
    sample.to_f/(@sample_size * Math.log10(@sample_size))

  when :n_sq
    sample.to_f/(@sample_size * @sample_size)
  end
end

#generate(config = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/benchmark/bigo/chart.rb', line 22

def generate config={}

  charts = [ { name: 'Growth Chart',
               data: @data,
               opts: opts_for(@data) } ]

  if config[:compare]
    for entry_data in @data
      charts << { name: entry_data[:name],
                  data: comparison_for(entry_data),
                  opts: opts_for([entry_data]) }
    end
  end

  charts
end

#generate_data_for(type, sample) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/benchmark/bigo/chart.rb', line 78

def generate_data_for type, sample

  # for the given sizes, create a hash from an array
  # the keys of the hash are the sizes
  # the values are the generated data for this type of comparison
  data = Hash[ @sizes.map {|n| [n, data_generator(type, n, sample) ] } ]

  { name: title_for(type), data: data }

end

#opts_for(data) ⇒ Object



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

def opts_for data
  data = [data] unless Array === data

  min = data.collect{|d| d[:data].values.min }.min
  max = data.collect{|d| d[:data].values.max }.max

  orange = "#f0662d"
  purple = "#8062a6"
  light_green = "#7bc545"
  med_blue = "#0883b2"
  yellow = "#ffaa00"
  teal = "#00c7c3"

  {
    discrete: true,
    width: "800px",
    height: "500px",
    min: (min * 0.8).floor,
    max: (max * 1.2).ceil,
    library: {
      colors: [orange, purple, light_green, med_blue, yellow, teal],
      xAxis: {type: 'linear', title: {text: "Size"}},
      yAxis: {type: 'linear', title: {text: "Microseconds per Iteration"}}
    }
  }
end

#title_for(type) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/benchmark/bigo/chart.rb', line 89

def title_for type

  case type
  when :const
    'const'
  when :logn
    'log n'
  when :n
    'n'
  when :nlogn
    'n log n'
  when :n_sq
    'n squared'
  end

end