Created
January 29, 2016 16:36
-
-
Save lmarburger/b8e17182624f271754f8 to your computer and use it in GitHub Desktop.
Measure ruby vm variance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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