Class: JmeterPerf::Helpers::RunningStatistisc
- Inherits:
-
Object
- Object
- JmeterPerf::Helpers::RunningStatistisc
- Defined in:
- lib/jmeter_perf/helpers/running_statistics.rb
Overview
This class uses a TDigest data structure to keep statistics “close enough”. Accuracy is not guaranteed.
Provides running statistics for a series of numbers, including approximate percentiles and standard deviation.
Constant Summary collapse
- COMPRESS_MARKER =
The marker for when to call the compression function on the TDigest structure.
1000
Instance Attribute Summary collapse
-
#avg ⇒ Float
readonly
The running average of the numbers added.
Instance Method Summary collapse
-
#add_number(num) ⇒ void
Adds a number to the running statistics and updates the average and variance calculations.
-
#get_percentiles(*percentiles) ⇒ Array<Float>
Retrieves approximate percentiles for the data set based on the requested percentile values.
-
#initialize ⇒ RunningStatistisc
constructor
Initializes a new instance of RunningStatistisc to calculate running statistics.
-
#standard_deviation ⇒ Float
(also: #std)
Calculates the standard deviation of the numbers added so far.
Constructor Details
#initialize ⇒ RunningStatistisc
Initializes a new instance of RunningStatistisc to calculate running statistics.
18 19 20 21 22 23 |
# File 'lib/jmeter_perf/helpers/running_statistics.rb', line 18 def initialize @tdigest = ::TDigest::TDigest.new @count = 0 @avg = 0 @m2 = 0 # Sum of squares of differences from the avg end |
Instance Attribute Details
#avg ⇒ Float (readonly)
Returns the running average of the numbers added.
13 14 15 |
# File 'lib/jmeter_perf/helpers/running_statistics.rb', line 13 def avg @avg end |
Instance Method Details
#add_number(num) ⇒ void
This method returns an undefined value.
Adds a number to the running statistics and updates the average and variance calculations.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jmeter_perf/helpers/running_statistics.rb', line 29 def add_number(num) @tdigest.push(num) @count += 1 delta = num - @avg @avg += delta / @count delta2 = num - @avg @m2 += delta * delta2 # Compress data every 1000 items to maintain memory efficiency @tdigest.compress! if @count % COMPRESS_MARKER == 0 end |
#get_percentiles(*percentiles) ⇒ Array<Float>
Retrieves approximate percentiles for the data set based on the requested percentile values.
48 49 50 51 |
# File 'lib/jmeter_perf/helpers/running_statistics.rb', line 48 def get_percentiles(*percentiles) @tdigest.compress! percentiles.map { |percentile| @tdigest.percentile(percentile) } end |
#standard_deviation ⇒ Float Also known as: std
Calculates the standard deviation of the numbers added so far.
56 57 58 59 |
# File 'lib/jmeter_perf/helpers/running_statistics.rb', line 56 def standard_deviation return 0 if @count < 2 Math.sqrt(@m2 / (@count - 1)) end |