Class: Pwrake::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/report/stat.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Stat

Returns a new instance of Stat.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pwrake/report/stat.rb', line 5

def initialize(data)
  @data = data
  @n = data.size
  if @n>0
    @min = data.min
    @max = data.max
    @sum = data.inject(0){|s,x| s+x}
    @mean = @sum/@n
    @median = calc_median
    @mean_absolute_deviation = data.inject(0){|s,x| (x-@mean).abs} / @n
    if false and @n>1
      @variance = data.inject(0){|s,x| y=x-@mean; y**2} / (@n-1)
      @sdev = Math.sqrt(@variance)
      @skew = data.inject(0){|s,x| y=(x-@mean)/@sdev; y**3} / @n
      @kurt = data.inject(0){|s,x| y=(x-@mean)/@sdev; y**4} / @n - 3
    end
  end
end

Instance Attribute Details

#binObject (readonly)

Returns the value of attribute bin.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def bin
  @bin
end

#histObject (readonly)

Returns the value of attribute hist.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def hist
  @hist
end

#hist_maxObject (readonly)

Returns the value of attribute hist_max.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def hist_max
  @hist_max
end

#hist_minObject (readonly)

Returns the value of attribute hist_min.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def hist_min
  @hist_min
end

#kurtObject (readonly)

Returns the value of attribute kurt.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def kurt
  @kurt
end

#maxObject (readonly)

Returns the value of attribute max.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def max
  @max
end

#meanObject (readonly)

Returns the value of attribute mean.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def mean
  @mean
end

#mean_absolute_deviationObject (readonly)

Returns the value of attribute mean_absolute_deviation.



26
27
28
# File 'lib/pwrake/report/stat.rb', line 26

def mean_absolute_deviation
  @mean_absolute_deviation
end

#medianObject (readonly)

Returns the value of attribute median.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def median
  @median
end

#minObject (readonly)

Returns the value of attribute min.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def min
  @min
end

#nObject (readonly)

Returns the value of attribute n.



24
25
26
# File 'lib/pwrake/report/stat.rb', line 24

def n
  @n
end

#sdevObject (readonly)

Returns the value of attribute sdev.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def sdev
  @sdev
end

#skewObject (readonly)

Returns the value of attribute skew.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def skew
  @skew
end

#sumObject (readonly)

Returns the value of attribute sum.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def sum
  @sum
end

#varianceObject (readonly)

Returns the value of attribute variance.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def variance
  @variance
end

Class Method Details

.html_thObject



97
98
99
100
101
102
103
104
# File 'lib/pwrake/report/stat.rb', line 97

def self.html_th
  a = %w[command count sum mean median min max]
  "<tr>" + "<th></th>"*2 +
    "<th colspan=#{a.size-2}>time (seconds)</th>" +
    "</tr>\n<tr>" +
    "<th>%s</th>" * a.size % a +
    "</tr>\n"
end

Instance Method Details

#calc_medianObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pwrake/report/stat.rb', line 58

def calc_median
  if @n==1
    @data[0]
  elsif @n==2
    @mean
  else
    case @n%2
    when 1
      i = (@n-1)/2
      @data.sort[i]
    else
      i = @n/2
      s = @data.sort
      (s[i]+s[i+1])/2
    end
  end
end

#fmt(x) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pwrake/report/stat.rb', line 76

def fmt(x)
  case x
  when Numeric
    a = x.abs
    if a == 0
      "0"
    elsif a < 1
      "%.3g" % x
    else
      "%.3f" % x
    end
  else
    x.to_s
  end
end

#hist_eachObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pwrake/report/stat.rb', line 46

def hist_each
  if @hist
    n = @hist.size
    n.times do |i|
      x1 = 10**(@bin*(i+@i_min))
      x2 = 10**(@bin*(i+1+@i_min))
      y  = @hist[i]
      yield x1,x2,y
    end
  end
end

#html_tdObject



92
93
94
95
# File 'lib/pwrake/report/stat.rb', line 92

def html_td
  '<td align="right">%i</td><td align="right">%s</td><td align="right">%s</td><td align="right">%s</td><td align="right">%s</td><td align="right">%s</td>' %
    [@n, fmt(@sum), fmt(@mean), fmt(@median), fmt(@min), fmt(@max)]
end

#make_logx_histogram(bin) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pwrake/report/stat.rb', line 30

def make_logx_histogram(bin)
  if @min>0
  @bin = bin # 1.0/10
  @i_max = (Math.log10(@max)/@bin).floor
  @i_min = (Math.log10(@min)/@bin).floor
  @hist_min = 10**(@i_min * @bin)
  @hist_max = 10**((@i_max+1) * @bin)
  @hist = Array.new(@i_max-@i_min+1,0)
  @data.each do |x|
    i = (Math.log10(x)/@bin-@i_min).floor
    raise "invalid index i=#{i}" if i<0 || i>@i_max-@i_min
    @hist[i] += 1
  end
  end
end