Class: JmeterPerf::Helpers::RunningStatistisc

Inherits:
Object
  • Object
show all
Defined in:
lib/jmeter_perf/helpers/running_statistics.rb

Overview

Note:

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

Instance Method Summary collapse

Constructor Details

#initializeRunningStatistisc

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

#avgFloat (readonly)

Returns the running average of the numbers added.

Returns:

  • (Float)

    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.

Parameters:

  • num (Float)

    the number to add to the running statistics



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.

Examples:

Requesting the 10th, 50th, and 95th percentiles

get_percentiles(0.1, 0.5, 0.95) #=> [some_value_for_10th, some_value_for_50th, some_value_for_95th]

Parameters:

  • percentiles (Array<Float>)

    the requested percentiles (e.g., 0.5 for the 50th percentile)

Returns:

  • (Array<Float>)

    an array of calculated percentiles corresponding to the requested 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_deviationFloat Also known as: std

Calculates the standard deviation of the numbers added so far.

Returns:

  • (Float)

    the standard deviation, or 0 if fewer than two values have been added



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