Class: Logging::Stats::Tracker

### Home Classes Methods

### In Files

- lib/logging/stats.rb

### Parent

Object

### Methods

- ::new
- #coalesce
- #mark
- #periodically_run
- #reset
- #sample
- #stop
- #sync
- #tick
- #time

### Files

- History.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Logging
- Logging::Appender
- Logging::Appenders
- Logging::Appenders::Buffering
- Logging::Appenders::Email
- Logging::Appenders::File
- Logging::Appenders::Growl
- Logging::Appenders::IO
- Logging::Appenders::RollingFile
- Logging::Appenders::Stderr
- Logging::Appenders::Stdout
- Logging::Appenders::StringIo
- Logging::Appenders::Syslog
- Logging::ColorScheme
- Logging::Config
- Logging::Config::Configurator
- Logging::Config::Configurator::DSL
- Logging::Config::Configurator::Error
- Logging::Config::Configurator::TopLevelDSL
- Logging::Config::YamlConfigurator
- Logging::Config::YamlConfigurator::Error
- Logging::Layout
- Logging::Layouts
- Logging::Layouts::Basic
- Logging::Layouts::Parseable
- Logging::Layouts::Pattern
- Logging::Logger
- Logging::MappedDiagnosticContext
- Logging::NestedDiagnosticContext
- Logging::Proxy
- Logging::RailsCompat
- Logging::Repository
- Logging::Repository::Singleton
- Logging::RootLogger
- Logging::Stats
- Logging::Stats::Sampler
- Logging::Stats::Tracker
- RSpec
- RSpec::Core
- RSpec::Core::Configuration
- RSpec::LoggingHelper
- RSpec::LoggingHelper
- Spec
- Spec::Runner
- Spec::Runner::Configuration
- Object
- Object::LoggingHelper
- File
- FileUtils
- Hash
- Kernel
- Module
- ReentrantMutex
- String

No matching classes.

# Logging::Stats::Tracker

The Tracker class provides synchronized access to a collection of related samplers.

### Attributes

stats[R]

### Public Class Methods

new( threadsafe = true ) click to toggle source

Create a new Tracker instance. An optional boolean can be passed in to change the "threadsafe" value of the tracker. By default all trackers are created to be threadsafe.

    # File lib/logging/stats.rb, line 155
    def initialize( threadsafe = true )
      @stats = Hash.new do |h,name|
        h[name] = ::Logging::Stats::Sampler.new(name)
      end
      @mutex = threadsafe ? ReentrantMutex.new : nil
      @runner = nil
    end

### Public Instance Methods

coalesce( other ) click to toggle source

Coalesce the samplers from the other tracker into this one. The other tracker is not modified by this method.

Coalescing the same two trackers multiple times should only be done if one of the trackers is reset between calls to this method. Otherwise statistics will be counted multiple times.

Only this tracker is locked when the coalescing is happening. It is left to the user to lock the other tracker if that is the desired behavior. This is a deliberate choice in order to prevent deadlock situations where two threads are contending on the same mutex.

    # File lib/logging/stats.rb, line 175
    def coalesce( other )
      sync {
        other.stats.each do |name,sampler|
          stats[name].coalesce(sampler)
        end
      }
    end

mark( event ) click to toggle source

Mark the named event sampler. The sampler will be created if it does not exist.

    # File lib/logging/stats.rb, line 193
    def mark( event )
      sync {stats[event].mark}
    end

periodically_run( period, &block ) click to toggle source

Periodically execute the given block at the given period. The tracker will be locked while the block is executing.

This method is useful for logging statistics at given interval.

Example

    periodically_run( 300 ) {
      logger = Logging::Logger['stats']
      tracker.each {|sampler| logger << sampler.to_s}
      tracker.reset
    }

    # File lib/logging/stats.rb, line 235
    def periodically_run( period, &block )
      raise ArgumentError, 'a runner already exists' unless @runner.nil?

      @runner = Thread.new do
        start = stop = Time.now.to_f
        loop do
          seconds = period - (stop-start)
          seconds = period if seconds <= 0
          sleep seconds

          start = Time.now.to_f
          break if Thread.current[:stop] == true
          if @mutex then @mutex.synchronize(&block)
          else block.call end
          stop = Time.now.to_f
        end
      end
    end

reset() click to toggle source

Reset all the samplers managed by this tracker.

    # File lib/logging/stats.rb, line 217
    def reset
      sync {stats.each_value {|sampler| sampler.reset}}
      self
    end

sample( event, value ) click to toggle source

Add the given value to the named event sampler. The sampler will be created if it does not exist.

    # File lib/logging/stats.rb, line 186
    def sample( event, value )
      sync {stats[event].sample(value)}
    end

stop() click to toggle source

Stop the current periodic runner if present.

    # File lib/logging/stats.rb, line 256
    def stop
      return if @runner.nil?
      @runner[:stop] = true
      @runner.wakeup if @runner.status
      @runner = nil
    end

sync { block } click to toggle source

Obtains an exclusive lock, runs the block, and releases the lock when the block completes. This method is re-entrant so that a single thread can call `sync` multiple times without hanging the thread.

    # File lib/logging/stats.rb, line 270
    def sync
      return yield if @mutex.nil?
      @mutex.synchronize {yield}
    end

tick( event ) click to toggle source

Tick the named event sampler. The sampler will be created if it does not exist.

    # File lib/logging/stats.rb, line 200
    def tick( event )
      sync {stats[event].tick}
    end

time( event ) click to toggle source

Time the execution of the given block and store the results in the named event sampler. The sampler will be created if it does not exist.

    # File lib/logging/stats.rb, line 208
    def time( event )
      sync {stats[event].mark}
      yield
    ensure
      sync {stats[event].tick}
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.