Class: Caliper::Stats::Statistic

Inherits:
Object
  • Object
show all
Defined in:
lib/caliper/stats/statistic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatistic

Returns a new instance of Statistic.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/caliper/stats/statistic.rb', line 34

def initialize
	@sum = AtomicWrapper.new(0)
	@count = AtomicWrapper.new(0)
	@last = AtomicWrapper.new(0)
	@lock = AtomicWrapper.new(false)

	@min = 0
	@max = 0

	@oldM = 0
	@newM = 0
	@oldS = 0
	@newS = 0

end

Instance Attribute Details

#countObject

Returns the value of attribute count.



28
29
30
# File 'lib/caliper/stats/statistic.rb', line 28

def count
  @count
end

#lastObject

Returns the value of attribute last.



28
29
30
# File 'lib/caliper/stats/statistic.rb', line 28

def last
  @last
end

#maxObject (readonly)

Returns the value of attribute max.



32
33
34
# File 'lib/caliper/stats/statistic.rb', line 32

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



32
33
34
# File 'lib/caliper/stats/statistic.rb', line 32

def min
  @min
end

#newMObject (readonly)

Returns the value of attribute newM.



30
31
32
# File 'lib/caliper/stats/statistic.rb', line 30

def newM
  @newM
end

#newSObject (readonly)

Returns the value of attribute newS.



30
31
32
# File 'lib/caliper/stats/statistic.rb', line 30

def newS
  @newS
end

#oldMObject (readonly)

Returns the value of attribute oldM.



30
31
32
# File 'lib/caliper/stats/statistic.rb', line 30

def oldM
  @oldM
end

#oldSObject (readonly)

Returns the value of attribute oldS.



30
31
32
# File 'lib/caliper/stats/statistic.rb', line 30

def oldS
  @oldS
end

#sumObject

Returns the value of attribute sum.



28
29
30
# File 'lib/caliper/stats/statistic.rb', line 28

def sum
  @sum
end

Instance Method Details

#clearObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/caliper/stats/statistic.rb', line 84

def clear
	@count.set(0)
	@sum.set(0.0)
	@last.set(0.0)

	@lock.set(false)
	@min = 0
	@max = 0

	@oldM = 0
	@newM = 0
	@oldS = 0
	@newS = 0
end

#get_averageObject



99
100
101
# File 'lib/caliper/stats/statistic.rb', line 99

def get_average
	return count.get() > 0 ? (sum.get() / count.get()) : 0.0
end

#get_standard_deviationObject

Get the standard deviation of the stream of values



109
110
111
# File 'lib/caliper/stats/statistic.rb', line 109

def get_standard_deviation
	Math.sqrt(get_variance())
end

#get_varianceObject

Get the variance



104
105
106
# File 'lib/caliper/stats/statistic.rb', line 104

def get_variance
	return (count.get() > 1) ? newS / (count.get() - 1) : 1.0
end

#to_sObject

Return nicely formatted String for all values



114
115
116
117
118
119
120
121
# File 'lib/caliper/stats/statistic.rb', line 114

def to_s
	if (@min == 1.0 && @max == 1.0)
		# this is just a count
		return "" + get_count();
	else
		return "[Count : %d], [Min : %s], [Max : %s], [Average : %s], [Std. Dev. : %s]",@count, @min, @max, getAverage(), getStandardDeviation()
	end
end

#update(val) ⇒ Object

add another value to this statistic



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
# File 'lib/caliper/stats/statistic.rb', line 51

def update(val)
	n = @count.add_and_get(1)
	if (@lock.compare_and_set(false, true))
		if (n == 1)
			# this is the first time we are executing, so clear the numbers
			@oldM = @newM = val
			@oldS = 0
			@min = val
			@max = val
		else
			# this is not our first update
			@newM = @oldM + (val - @oldM) / n
			@newS = @oldS + (val - @oldM) * (val * @newM)

			@oldM = @newM
			@oldS = @newS
		end

		if (@val < @min)
			@min = val
		end

		if (@val > @max)
			@max = val
		end

		@lock.set(false)
	end

	@sum.add_and_get(val)
	@last.set(val)
end