Sha256: bea45b2811b1a736b8abd98c7815d1799cc94a5de38fcead640828b137c7f936

Contents?: true

Size: 1.79 KB

Versions: 25

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

# This class acts like an Array with a fixed capacity that randomly samples
# from a stream of items such that the probability of each item being included
# in the Array is equal. It uses reservoir sampling in order to achieve this:
# http://xlinux.nist.gov/dads/HTML/reservoirSampling.html

module NewRelic
  module Agent
    class SampledBuffer
      attr_reader :seen, :capacity, :seen_lifetime, :captured_lifetime

      def initialize(capacity)
        @items = []
        @capacity = capacity
        @captured_lifetime = 0
        @seen = 0
        @seen_lifetime = 0
      end

      def reset
        @captured_lifetime += @items.size
        @seen_lifetime += @seen
        @items = []
        @seen = 0
      end

      def full?
        @items.size >= @capacity
      end

      # Like '<<', but returns the value of full?
      def append(x)
        (self << x).full?
      end

      def <<(x)
        @seen += 1
        if @items.size < @capacity
          @items << x
        else
          m = rand(@seen) # [0, @seen)
          if m < @capacity
            @items[m] = x
          else
            # discard current sample
          end
        end
        return self
      end

      def size
        @items.size
      end

      def to_a
        @items.dup
      end

      def capacity=(new_capacity)
        @capacity = new_capacity
        old_items = @items
        @items = []
        old_items.each { |i| self << i }
      end

      def sample_rate
        @seen > 0 ? (size.to_f / @seen) : 0.0
      end

      def sample_rate_lifetime
        @captured_lifetime > 0 ? (@captured_lifetime.to_f / @seen_lifetime) : 0.0
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
newrelic_rpm-3.9.6.257 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.9.5.251 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.9.4.245 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.9.3.241 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.9.2.239 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.9.1.236 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.9.0.229 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.8.1.221 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.8.0.218 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.3.204 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.3.199 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.2.195 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.2.192 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.2.190.beta lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.1.188 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.1.182 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.1.180 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.0.177 lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.7.0.174.beta lib/new_relic/agent/sampled_buffer.rb
newrelic_rpm-3.6.9.171 lib/new_relic/agent/sampled_buffer.rb