Sha256: 19cc3ebb216f8e54a6406d33c3dbdf33c875a54edf2a71421bb6c176227f101c

Contents?: true

Size: 967 Bytes

Versions: 3

Compression:

Stored size: 967 Bytes

Contents

require "tdigest"
module JmeterPerf
  # NOTE: These statistics are NOT 100% accurate, but rather "close enough"
  class RunningStatistisc
    COMPRESS_MARKER = 1000 # When to call compress
    attr_reader :avg

    def initialize
      @tdigest = ::TDigest::TDigest.new
      @count = 0
      @avg = 0
      @m2 = 0 # Sum of squares of differences from the avg
    end

    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
      if @count % COMPRESS_MARKER == 0
        @tdigest.compress!
      end
    end

    def get_percentiles(*percentiles)
      @tdigest.compress!
      percentiles.map { |percentile| @tdigest.percentile(percentile) }
    end

    def standard_deviation
      return 0 if @count < 2
      Math.sqrt(@m2 / (@count - 1))
    end
    alias_method :std, :standard_deviation
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jmeter_perf-0.0.7 lib/jmeter_perf/helpers/running_statistics.rb
jmeter_perf-0.0.6 lib/jmeter_perf/helpers/running_statistics.rb
jmeter_perf-0.0.2 lib/jmeter_perf/helpers/running_statistics.rb