Class: RFuzz::Sampler
- Inherits:
-
Object
- Object
- RFuzz::Sampler
- Defined in:
- lib/rfuzz/stats.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
-
#sumsq ⇒ Object
readonly
Returns the value of attribute sumsq.
Class Method Summary collapse
-
.keys ⇒ Object
Class method that returns the headers that a CSV file would have for the values that this stats object is using.
Instance Method Summary collapse
-
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Sampler object with an optional additional message.
-
#initialize(name) ⇒ Sampler
constructor
A new instance of Sampler.
-
#mark ⇒ Object
You can just call tick repeatedly if you need the delta times between a set of sample periods, but many times you actually want to sample how long something takes between a start/end period.
-
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
-
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
-
#sample(s) ⇒ Object
Adds a sampling to the calculations.
-
#sd ⇒ Object
Calculates the standard deviation of the data so far.
-
#tick ⇒ Object
Adds a time delta between now and the last time you called this.
- #to_hash ⇒ Object
-
#to_s ⇒ Object
Returns a common display (used by dump).
-
#values ⇒ Object
An array of the values minus the name: [sum,sumsq,n,mean,sd,min,max].
Constructor Details
#initialize(name) ⇒ Sampler
Returns a new instance of Sampler.
19 20 21 22 |
# File 'lib/rfuzz/stats.rb', line 19 def initialize(name) @name = name reset end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
17 18 19 |
# File 'lib/rfuzz/stats.rb', line 17 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
17 18 19 |
# File 'lib/rfuzz/stats.rb', line 17 def min @min end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
17 18 19 |
# File 'lib/rfuzz/stats.rb', line 17 def n @n end |
#sum ⇒ Object (readonly)
Returns the value of attribute sum.
17 18 19 |
# File 'lib/rfuzz/stats.rb', line 17 def sum @sum end |
#sumsq ⇒ Object (readonly)
Returns the value of attribute sumsq.
17 18 19 |
# File 'lib/rfuzz/stats.rb', line 17 def sumsq @sumsq end |
Class Method Details
.keys ⇒ Object
Class method that returns the headers that a CSV file would have for the values that this stats object is using.
64 65 66 |
# File 'lib/rfuzz/stats.rb', line 64 def self.keys ["name","sum","sumsq","n","mean","sd","min","max"] end |
Instance Method Details
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Sampler object with an optional additional message.
48 49 50 |
# File 'lib/rfuzz/stats.rb', line 48 def dump(msg = "", out=STDERR) out.puts "#{msg}: #{self.to_s}" end |
#mark ⇒ Object
You can just call tick repeatedly if you need the delta times between a set of sample periods, but many times you actually want to sample how long something takes between a start/end period. Call mark at the beginning and then tick at the end you’ll get this kind of measurement. Don’t mix mark/tick and tick sampling together or the measurement will be meaningless.
94 95 96 |
# File 'lib/rfuzz/stats.rb', line 94 def mark @last_time = Time.now end |
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
74 75 76 |
# File 'lib/rfuzz/stats.rb', line 74 def mean @sum / @n end |
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
25 26 27 28 29 30 31 32 |
# File 'lib/rfuzz/stats.rb', line 25 def reset @sum = 0.0 @sumsq = 0.0 @last_time = Time.new @n = 0.0 @min = 0.0 @max = 0.0 end |
#sample(s) ⇒ Object
Adds a sampling to the calculations.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rfuzz/stats.rb', line 35 def sample(s) @sum += s @sumsq += s * s if @n == 0 @min = @max = s else @min = s if @min > s @max = s if @max < s end @n+=1 end |
#sd ⇒ Object
Calculates the standard deviation of the data so far.
79 80 81 82 83 84 85 86 |
# File 'lib/rfuzz/stats.rb', line 79 def sd # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) begin return Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) ) rescue Errno::EDOM return 0.0 end end |
#tick ⇒ Object
107 108 109 110 111 |
# File 'lib/rfuzz/stats.rb', line 107 def tick now = Time.now sample(now - @last_time) @last_time = now end |
#to_hash ⇒ Object
68 69 70 71 |
# File 'lib/rfuzz/stats.rb', line 68 def to_hash {"name" => @name, "sum" => @sum, "sumsq" => @sumsq, "mean" => mean, "sd" => sd, "min" => @min, "max" => @max} end |
#to_s ⇒ Object
Returns a common display (used by dump)
53 54 55 |
# File 'lib/rfuzz/stats.rb', line 53 def to_s "[%s]: SUM=%0.6f, SUMSQ=%0.6f, N=%0.6f, MEAN=%0.6f, SD=%0.6f, MIN=%0.6f, MAX=%0.6f" % values end |
#values ⇒ Object
An array of the values minus the name: [sum,sumsq,n,mean,sd,min,max]
58 59 60 |
# File 'lib/rfuzz/stats.rb', line 58 def values [@name, @sum, @sumsq, @n, mean, sd, @min, @max] end |