Module: EventMachine
- Defined in:
- lib/em-monitor.rb
Overview
EM::Monitor adds a few methods to eventmachine that can help you keep track of ‘lag’ caused by long-running CPU spans on the reactor thread.
Defined Under Namespace
Classes: Monitor
Class Method Summary collapse
-
.monitor_histogram(opts = {}, &block) {|histogram, from, to| ... } ⇒ Object
Set the block to be called periodically with histogram data.
-
.monitor_spans(opts = {}, &block) {|spans, from, to| ... } ⇒ Object
Set the block to be called periodically with timing data.
-
.run(*args, &block) ⇒ Object
Run the eventmachine reactor with monitoring.
- .run_without_monitor ⇒ Object
Class Method Details
.monitor_histogram(opts = {}, &block) {|histogram, from, to| ... } ⇒ Object
Set the block to be called periodically with histogram data.
This is a convenience wrapper around monitor_spans for the common use-case of wanting to plot a histogram of loop utilisation split into time-chunks.
In the normal case you can plot these values directly and it will tell you how much CPU-time was used by spans shorter than a given length. However, care should be taken if CPU-spans are often of similar length to :interval. This can cause the actual delay between calls to the block to vary significantly and so if you’re trying to plot a line of CPU-utilization then it can give you misleading answers. If this is a concern to you then you might want to use the :cumulative mode and ask your graphing library to plot the derivative of the values over time.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/em-monitor.rb', line 97 def self.monitor_histogram(opts = {}, &block) stacked = !!opts[:stacked] cumulative = !!opts[:cumulative] interval = opts[:interval] || Monitor::DEFAULT_INTERVAL buckets = (opts[:buckets] || Monitor::DEFAULT_BUCKETS).sort buckets << (1/0.0) # ensure the histogram keys are in the right order hist = buckets.each_with_object({}){ |bucket, h| h[bucket] = 0 } monitor_spans(opts) do |spans, from, to| unless cumulative hist = buckets.each_with_object({}){ |bucket, h| h[bucket] = 0 } end if stacked spans.each do |span| buckets.each do |bucket| hist[bucket] += span if bucket > span end end else spans.each do |span| hist[buckets.detect{ |bucket| bucket > span }] += span end end block.call hist, from, to end end |
.monitor_spans(opts = {}, &block) {|spans, from, to| ... } ⇒ Object
Set the block to be called periodically with timing data.
43 44 45 46 |
# File 'lib/em-monitor.rb', line 43 def self.monitor_spans(opts = {}, &block) raise "EventMachine not initialized" unless @monitor @monitor.monitor_spans(opts[:interval] || Monitor::DEFAULT_INTERVAL, &block) end |
.run(*args, &block) ⇒ Object
Run the eventmachine reactor with monitoring.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/em-monitor.rb', line 13 def self.run(*args, &block) run_without_monitor(*args) do |*a, &b| EM::Monitor.new do |monitor| @monitor = monitor block.call(*a, &b) if block_given? end end ensure @monitor = nil end |
.run_without_monitor ⇒ Object
8 |
# File 'lib/em-monitor.rb', line 8 alias_method :run_without_monitor, :run |