Class: Rack::PerftoolsProfiler
- Inherits:
-
Object
- Object
- Rack::PerftoolsProfiler
- Defined in:
- lib/rack/contrib/perftools_profiler.rb
Overview
Pass the :printer option to pick a different result format.
Constant Summary collapse
- MODES =
%w( process_time #wall_time )
- PROFILING_DATA_FILE =
'/tmp/rack_perftools_profiler.data'
- DEFAULT_PRINTER =
:text
- PRINTER_CONTENT_TYPE =
{ :text => 'text/plain', :gif => 'image/gif', :pdf => 'application/pdf' }
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ PerftoolsProfiler
constructor
A new instance of PerftoolsProfiler.
- #printer ⇒ Object
- #profile_call(env, times) ⇒ Object
- #return_profiling_data(env, printer) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ PerftoolsProfiler
Returns a new instance of PerftoolsProfiler.
170 171 172 173 174 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 170 def initialize(app, = {}) @app = app @printer = parse_printer([:printer]) @times = ([:times] || 1).to_i end |
Class Method Details
.clear_data ⇒ Object
160 161 162 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 160 def self.clear_data ::File.delete(PROFILING_DATA_FILE) if ::File.exists?(PROFILING_DATA_FILE) end |
.with_profiling_off(app, options = {}) ⇒ Object
164 165 166 167 168 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 164 def self.with_profiling_off(app, = {}) instance = self.new(app, ) instance.send(:profiling=, false) instance end |
Instance Method Details
#call(env) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 180 def call(env) @original_request = Request.new(env) if enable_profiling_request? action = Action.for_request(@original_request, self) action.act action.response elsif disable_profiling_request? action = Action.for_request(@original_request, self) action.act action.response elsif profiling_data_request? if in_profiling_mode? [400, {'Content-Type' => 'text/plain'}, 'No profiling data available.'] else return_profiling_data(env, @printer) end else if mode = profiling?(env.clone) action = Action.for_request(@original_request, self) action.act action.response else @app.call(env) end end end |
#printer ⇒ Object
176 177 178 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 176 def printer @printer end |
#profile_call(env, times) ⇒ Object
215 216 217 218 219 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 215 def profile_call(env, times) PerfTools::CpuProfiler.start(PROFILING_DATA_FILE) do times.times { result = @app.call(env) } end end |
#return_profiling_data(env, printer) ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 221 def return_profiling_data(env, printer) if ::File.exists?(PROFILING_DATA_FILE) [200, headers(printer, env), print(printer)] else [404, {'Content-Type' => 'text/plain'}, 'No profiling data available.'] end end |
#start ⇒ Object
207 208 209 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 207 def start start_profiling end |
#stop ⇒ Object
211 212 213 |
# File 'lib/rack/contrib/perftools_profiler.rb', line 211 def stop stop_profiling end |