Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Created January 29, 2016 16:36
Show Gist options
  • Save lmarburger/b8e17182624f271754f8 to your computer and use it in GitHub Desktop.
Save lmarburger/b8e17182624f271754f8 to your computer and use it in GitHub Desktop.
Measure ruby vm variance
require 'hitimes'
require 'librato/metrics'
require 'thread'
class RuntimeInstrumentation
def self.queue
@queue ||= Librato::Metrics::Queue
.new(source: ENV.fetch('SOURCE_NAME'),
autosubmit_interval: ENV.fetch('METRIC_SUBMIT_INTERVAL', 10).to_i)
end
def queue
self.class.queue
end
attr_reader :interval, :mutex, :thread
def initialize
@interval = 1.0
@mutex = Mutex.new
@running = false
end
def running?
mutex.synchronize { @running }
end
def start
return self if thread && thread.alive?
mutex.synchronize { @running = true }
@thread = Thread.new do
while running?
measure_time = Time.now
ruby_interval = Hitimes::Interval.now
sleep @interval.to_f
variance = ruby_interval.duration - @interval.to_f
record_variance variance, measure_time
end
end
self
end
def stop
mutex.synchronize { @running = false }
end
def record_variance(value, measure_time)
data = { 'sonar.ruby.variance' =>
{ :measure_time => measure_time,
:value => value }}
queue.add data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment