Class: Peek::Views::PerformanceBar::ProcessUtilization

Inherits:
Object
  • Object
show all
Defined in:
lib/peek/views/performance_bar/process_utilization.rb

Overview

Middleware that tracks the amount of time this process spends processing requests, as opposed to being idle waiting for a connection. Statistics are dumped to rack.errors every 5 minutes.

NOTE This middleware is not thread safe. It should only be used when rack.multiprocess is true and rack.multithread is false.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ ProcessUtilization

Returns a new instance of ProcessUtilization.



22
23
24
25
26
27
28
29
30
31
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 22

def initialize(app, opts={})
  @app = app
  @window = opts[:window] || 100
  @horizon = nil
  @requests = nil
  @active_time = nil
  @total_requests = 0

  self.class.singleton = self
end

Class Attribute Details

.singletonObject

The instance of this middleware in a single-threaded production server. Useful for fetching stats about the current request:

o = Rack::ProcessUtilization.singleton
time, calls = o.gc_stats if o.track_gc?


19
20
21
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 19

def singleton
  @singleton
end

Instance Attribute Details

#active_timeObject

decimal number of seconds the worker has been active within a request since the horizon time.



43
44
45
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 43

def active_time
  @active_time
end

#horizonObject

time when we began sampling. this is reset every once in a while so averages don’t skew over time.



35
36
37
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 35

def horizon
  @horizon
end

#requestsObject

total number of requests that have been processed by this worker since the horizon time.



39
40
41
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 39

def requests
  @requests
end

#total_requestsObject

total requests processed by this worker process since it started



46
47
48
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 46

def total_requests
  @total_requests
end

Instance Method Details

#average_response_timeObject

average response time since the horizon in milliseconds



75
76
77
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 75

def average_response_time
  (active_time / requests.to_f) * 1000
end

#call(env) ⇒ Object

Rack entry point.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 110

def call(env)
  reset_stats

  @total_requests += 1
  first_request if @total_requests == 1

  env['process.request_start'] = @start.to_f
  env['process.total_requests'] = total_requests

  status, headers, body = @app.call(env)
  body = Rack::BodyProxy.new(body) { record_request }
  [status, headers, body]
end

#first_requestObject

called exactly once before the first request is processed by a worker



80
81
82
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 80

def first_request
  reset_horizon
end

#horizon_timeObject

the amount of time since the horizon



49
50
51
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 49

def horizon_time
  Time.now - horizon
end

#idle_timeObject

decimal number of seconds this process has been active since the horizon time. This is the inverse of the active time.



55
56
57
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 55

def idle_time
  horizon_time - active_time
end

#percentage_activeObject

percentage of time this process has been active since the horizon time.



60
61
62
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 60

def percentage_active
  (active_time / horizon_time) * 100
end

#percentage_idleObject

percentage of time this process has been idle since the horizon time.



65
66
67
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 65

def percentage_idle
  (idle_time / horizon_time) * 100
end

#record_requestObject

called immediately after a request to record statistics, update the procline, and dump information to the logfile



98
99
100
101
102
103
104
105
106
107
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 98

def record_request
  now = Time.now
  diff = (now - @start)
  @active_time += diff
  @requests += 1

  reset_horizon if now - horizon > @window
rescue => boom
  warn "ProcessUtilization#record_request failed: #{boom.inspect}"
end

#requests_per_secondObject

number of requests processed per second since the horizon



70
71
72
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 70

def requests_per_second
  requests / horizon_time
end

#reset_horizonObject

resets the horizon and all dependent variables



90
91
92
93
94
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 90

def reset_horizon
  @horizon = Time.now
  @active_time = 0.0
  @requests = 0
end

#reset_statsObject

reset various counters before the new request



85
86
87
# File 'lib/peek/views/performance_bar/process_utilization.rb', line 85

def reset_stats
  @start = Time.now
end