peek-performance_bar 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2c04d7b3f6ba4afd971e159eb2983568f5764e3
4
- data.tar.gz: 5ae24118aec944b1b484400206a58c4f94356083
3
+ metadata.gz: 2003553056d22a680513e8904728ee685521fc8c
4
+ data.tar.gz: e456e37f2b0c875e61c66a2c7cd823548ae69533
5
5
  SHA512:
6
- metadata.gz: edb58ad8a521fe61d63aee6d9d2e1c673d6903683d6eb60df350f7fce8c3f836aebd3c2d9486ab3e5906fe2e5c7e7c27e492572e02803cdd1833309a47e9c81a
7
- data.tar.gz: ab30b7b810f8dc7dc43298c462e4e5f9dbb1a69b8960c1b7e72856ea9f15080f6d2b4b310db01fca05adcf983a3f63c7de948e9a91ee0be2619fff7858f66445
6
+ metadata.gz: 7d2ae35cfc7c1187acc8e3aa0b9dc8c93e3caaaf0c277dcdfc99be02ed6edd9631edc8194e5c89d72470e806ef049f6bb5df3022bfa9cc7825f49a5388cf89b6
7
+ data.tar.gz: 33c66f9522e2ea4f7279b2f4b4e91661ef29e18a2ac67d04a7eea8679c9c04ce8f07ae3898ebf1f9644933855b3b7e55cbdbaed91da1eed8dacd327a74afc468
data/CHANGELOG.md CHANGED
@@ -26,3 +26,7 @@
26
26
  # 1.1.5
27
27
 
28
28
  - Don't strip `X-Request-Start` header which New Relic relies on. - #20
29
+
30
+ # 1.1.6
31
+
32
+ - Namespace ProcessUtilization middleware to Peek::Views::PerformanceBar - #21 (@jnunemaker)
@@ -0,0 +1,144 @@
1
+ module Peek
2
+ module Views
3
+ class PerformanceBar
4
+ # Middleware that tracks the amount of time this process spends processing
5
+ # requests, as opposed to being idle waiting for a connection. Statistics
6
+ # are dumped to rack.errors every 5 minutes.
7
+ #
8
+ # NOTE This middleware is not thread safe. It should only be used when
9
+ # rack.multiprocess is true and rack.multithread is false.
10
+ class ProcessUtilization
11
+ class << self
12
+ # The instance of this middleware in a single-threaded production server.
13
+ # Useful for fetching stats about the current request:
14
+ #
15
+ # o = Rack::ProcessUtilization.singleton
16
+ # time, calls = o.gc_stats if o.track_gc?
17
+ attr_accessor :singleton
18
+ end
19
+
20
+ def initialize(app, opts={})
21
+ @app = app
22
+ @window = opts[:window] || 100
23
+ @horizon = nil
24
+ @requests = nil
25
+ @active_time = nil
26
+ @total_requests = 0
27
+
28
+ self.class.singleton = self
29
+ end
30
+
31
+ # time when we began sampling. this is reset every once in a while so
32
+ # averages don't skew over time.
33
+ attr_accessor :horizon
34
+
35
+ # total number of requests that have been processed by this worker since
36
+ # the horizon time.
37
+ attr_accessor :requests
38
+
39
+ # decimal number of seconds the worker has been active within a request
40
+ # since the horizon time.
41
+ attr_accessor :active_time
42
+
43
+ # total requests processed by this worker process since it started
44
+ attr_accessor :total_requests
45
+
46
+ # the amount of time since the horizon
47
+ def horizon_time
48
+ Time.now - horizon
49
+ end
50
+
51
+ # decimal number of seconds this process has been active since the horizon
52
+ # time. This is the inverse of the active time.
53
+ def idle_time
54
+ horizon_time - active_time
55
+ end
56
+
57
+ # percentage of time this process has been active since the horizon time.
58
+ def percentage_active
59
+ (active_time / horizon_time) * 100
60
+ end
61
+
62
+ # percentage of time this process has been idle since the horizon time.
63
+ def percentage_idle
64
+ (idle_time / horizon_time) * 100
65
+ end
66
+
67
+ # number of requests processed per second since the horizon
68
+ def requests_per_second
69
+ requests / horizon_time
70
+ end
71
+
72
+ # average response time since the horizon in milliseconds
73
+ def average_response_time
74
+ (active_time / requests.to_f) * 1000
75
+ end
76
+
77
+ # called exactly once before the first request is processed by a worker
78
+ def first_request
79
+ reset_horizon
80
+ end
81
+
82
+ # reset various counters before the new request
83
+ def reset_stats
84
+ @start = Time.now
85
+ end
86
+
87
+ # resets the horizon and all dependent variables
88
+ def reset_horizon
89
+ @horizon = Time.now
90
+ @active_time = 0.0
91
+ @requests = 0
92
+ end
93
+
94
+ # called immediately after a request to record statistics, update the
95
+ # procline, and dump information to the logfile
96
+ def record_request
97
+ now = Time.now
98
+ diff = (now - @start)
99
+ @active_time += diff
100
+ @requests += 1
101
+
102
+ reset_horizon if now - horizon > @window
103
+ rescue => boom
104
+ warn "ProcessUtilization#record_request failed: #{boom.inspect}"
105
+ end
106
+
107
+ # Body wrapper. Yields to the block when body is closed. This is used to
108
+ # signal when a response is fully finished processing.
109
+ class Body
110
+ def initialize(body, &block)
111
+ @body = body
112
+ @block = block
113
+ end
114
+
115
+ def each(&block)
116
+ @body.each(&block)
117
+ end
118
+
119
+ def close
120
+ @body.close if @body.respond_to?(:close)
121
+ @block.call
122
+ nil
123
+ end
124
+ end
125
+
126
+ # Rack entry point.
127
+ def call(env)
128
+ @env = env
129
+ reset_stats
130
+
131
+ @total_requests += 1
132
+ first_request if @total_requests == 1
133
+
134
+ env['process.request_start'] = @start.to_f
135
+ env['process.total_requests'] = total_requests
136
+
137
+ status, headers, body = @app.call(env)
138
+ body = Body.new(body) { record_request }
139
+ [status, headers, body]
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -1,10 +1,10 @@
1
- require 'rack/process_utilization'
1
+ require 'peek/views/performance_bar/process_utilization'
2
2
 
3
3
  module Peek
4
4
  module PerformanceBar
5
5
  class Railtie < ::Rails::Engine
6
6
  initializer 'peek.performance_bar.mount_process_utilization' do |app|
7
- app.config.middleware.use Rack::ProcessUtilization
7
+ app.config.middleware.use Peek::Views::PerformanceBar::ProcessUtilization
8
8
  end
9
9
  end
10
10
  end
@@ -1,5 +1,5 @@
1
1
  module Peek
2
2
  module PerformanceBar
3
- VERSION = '1.1.5'
3
+ VERSION = '1.1.6'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peek-performance_bar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garrett Bjerkhoel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-13 00:00:00.000000000 Z
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: peek
@@ -47,7 +47,7 @@ files:
47
47
  - lib/peek-performance_bar/railtie.rb
48
48
  - lib/peek-performance_bar/version.rb
49
49
  - lib/peek/views/performance_bar.rb
50
- - lib/rack/process_utilization.rb
50
+ - lib/peek/views/performance_bar/process_utilization.rb
51
51
  homepage: https://github.com/peek/peek-performance_bar
52
52
  licenses: []
53
53
  metadata: {}
@@ -1,140 +0,0 @@
1
- module Rack
2
- # Middleware that tracks the amount of time this process spends processing
3
- # requests, as opposed to being idle waiting for a connection. Statistics
4
- # are dumped to rack.errors every 5 minutes.
5
- #
6
- # NOTE This middleware is not thread safe. It should only be used when
7
- # rack.multiprocess is true and rack.multithread is false.
8
- class ProcessUtilization
9
- class << self
10
- # The instance of this middleware in a single-threaded production server.
11
- # Useful for fetching stats about the current request:
12
- #
13
- # o = Rack::ProcessUtilization.singleton
14
- # time, calls = o.gc_stats if o.track_gc?
15
- attr_accessor :singleton
16
- end
17
-
18
- def initialize(app, opts={})
19
- @app = app
20
- @window = opts[:window] || 100
21
- @horizon = nil
22
- @requests = nil
23
- @active_time = nil
24
- @total_requests = 0
25
-
26
- self.class.singleton = self
27
- end
28
-
29
- # time when we began sampling. this is reset every once in a while so
30
- # averages don't skew over time.
31
- attr_accessor :horizon
32
-
33
- # total number of requests that have been processed by this worker since
34
- # the horizon time.
35
- attr_accessor :requests
36
-
37
- # decimal number of seconds the worker has been active within a request
38
- # since the horizon time.
39
- attr_accessor :active_time
40
-
41
- # total requests processed by this worker process since it started
42
- attr_accessor :total_requests
43
-
44
- # the amount of time since the horizon
45
- def horizon_time
46
- Time.now - horizon
47
- end
48
-
49
- # decimal number of seconds this process has been active since the horizon
50
- # time. This is the inverse of the active time.
51
- def idle_time
52
- horizon_time - active_time
53
- end
54
-
55
- # percentage of time this process has been active since the horizon time.
56
- def percentage_active
57
- (active_time / horizon_time) * 100
58
- end
59
-
60
- # percentage of time this process has been idle since the horizon time.
61
- def percentage_idle
62
- (idle_time / horizon_time) * 100
63
- end
64
-
65
- # number of requests processed per second since the horizon
66
- def requests_per_second
67
- requests / horizon_time
68
- end
69
-
70
- # average response time since the horizon in milliseconds
71
- def average_response_time
72
- (active_time / requests.to_f) * 1000
73
- end
74
-
75
- # called exactly once before the first request is processed by a worker
76
- def first_request
77
- reset_horizon
78
- end
79
-
80
- # reset various counters before the new request
81
- def reset_stats
82
- @start = Time.now
83
- end
84
-
85
- # resets the horizon and all dependent variables
86
- def reset_horizon
87
- @horizon = Time.now
88
- @active_time = 0.0
89
- @requests = 0
90
- end
91
-
92
- # called immediately after a request to record statistics, update the
93
- # procline, and dump information to the logfile
94
- def record_request
95
- now = Time.now
96
- diff = (now - @start)
97
- @active_time += diff
98
- @requests += 1
99
-
100
- reset_horizon if now - horizon > @window
101
- rescue => boom
102
- warn "ProcessUtilization#record_request failed: #{boom.inspect}"
103
- end
104
-
105
- # Body wrapper. Yields to the block when body is closed. This is used to
106
- # signal when a response is fully finished processing.
107
- class Body
108
- def initialize(body, &block)
109
- @body = body
110
- @block = block
111
- end
112
-
113
- def each(&block)
114
- @body.each(&block)
115
- end
116
-
117
- def close
118
- @body.close if @body.respond_to?(:close)
119
- @block.call
120
- nil
121
- end
122
- end
123
-
124
- # Rack entry point.
125
- def call(env)
126
- @env = env
127
- reset_stats
128
-
129
- @total_requests += 1
130
- first_request if @total_requests == 1
131
-
132
- env['process.request_start'] = @start.to_f
133
- env['process.total_requests'] = total_requests
134
-
135
- status, headers, body = @app.call(env)
136
- body = Body.new(body) { record_request }
137
- [status, headers, body]
138
- end
139
- end
140
- end