Class: RawkLog::Stat

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

Constant Summary collapse

DEFAULT_LABEL_SIZE =
30
HEADER =
"Count Sum(secs)     Max  Median     Avg     Min     Std"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Stat

Returns a new instance of Stat.



9
10
11
12
13
14
15
16
17
# File 'lib/rawk_log/stat.rb', line 9

def initialize(key)
  @key=key
  @min = nil
  @max = nil
  @sum = 0
  @sum_squares = 0
  @count = 0
  @values = []
end

Class Method Details

.testObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rawk_log/stat.rb', line 87

def self.test
  stat = Stat.new(30)
  stat.add(5)
  stat.add(6)
  stat.add(8)
  stat.add(9)
  results = [ 7==stat.median ? "median Success" : "median Failure" ]
  results <<= (7==stat.average ? "average Success" : "average Failure")
  results <<= (158==(stat.standard_deviation*100).round ? "std Success" : "std Failure")
  puts results.join("\n")
  exit (results.select{|m| m =~ /Failure/}.size)
end

Instance Method Details

#add(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rawk_log/stat.rb', line 19

def add(value)
  @new_log_format = !value.is_a?(Float)
  value=1.0*value
  @count+=1
  @min = value unless @min
  @min = value if value<@min
  @max = value unless @max
  @max = value if value>@max
  @sum += value
  @sum_squares += value*value
  @values << value
end

#averageObject



56
57
58
# File 'lib/rawk_log/stat.rb', line 56

def average
  @count > 0 ? @sum/@count : @sum
end

#countObject



40
41
42
# File 'lib/rawk_log/stat.rb', line 40

def count
  @count
end

#header(label_size = DEFAULT_LABEL_SIZE) ⇒ Object



32
33
34
# File 'lib/rawk_log/stat.rb', line 32

def header(label_size = DEFAULT_LABEL_SIZE)
  sprintf "%*s  %s" % [-label_size, "Request", HEADER]
end

#keyObject



36
37
38
# File 'lib/rawk_log/stat.rb', line 36

def key
  @key
end

#maxObject



52
53
54
# File 'lib/rawk_log/stat.rb', line 52

def max
  @max
end

#medianObject



60
61
62
63
64
65
66
67
# File 'lib/rawk_log/stat.rb', line 60

def median
  return nil unless @values
  l = @values.length
  return nil unless l>0
  @values.sort!
  return (@values[l/2-1]+@values[l/2])/2 if l%2==0
  @values[(l+1)/2-1]
end

#minObject



48
49
50
# File 'lib/rawk_log/stat.rb', line 48

def min
  @min
end

#standard_deviationObject



69
70
71
72
# File 'lib/rawk_log/stat.rb', line 69

def standard_deviation
  return 0 if @count<=1
  Math.sqrt((@sum_squares - (@sum*@sum/@count))/ (@count) )
end

#sumObject



44
45
46
# File 'lib/rawk_log/stat.rb', line 44

def sum
  @sum
end

#to_s(label_size = DEFAULT_LABEL_SIZE) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rawk_log/stat.rb', line 74

def to_s(label_size = DEFAULT_LABEL_SIZE)
  if count > 0
    if @new_log_format
      sprintf("%*s %6d %9.2f %7d %7d %7d %7d %7d",-label_size, key,count,(sum.to_f/1000),max,median,average,min,standard_deviation)
    else
      sprintf("%*s %6d %9.2f %7d %7d %7d %7d %7d",-label_size, key,count,sum,max*1000.0,median*1000.0,average*1000.0,min*1000.0,standard_deviation*1000.0)
      #sprintf("%*s %6d %9.2f %7.2f %7.2f %7.2f %7.2f %7.2f",-label_size,key,count,sum,max,median,average,min,standard_deviation)
    end
  else
      sprintf("%*s %6d",-label_size,key,0)
  end
end