Class: StackProf::Middleware
- Inherits:
-
Object
- Object
- StackProf::Middleware
- Defined in:
- lib/stackprof/middleware.rb
Class Attribute Summary collapse
-
.enabled ⇒ Object
(also: enabled?)
Returns the value of attribute enabled.
-
.interval ⇒ Object
Returns the value of attribute interval.
-
.mode ⇒ Object
Returns the value of attribute mode.
-
.path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/stackprof/middleware.rb', line 5 def initialize(app, = {}) @app = app @options = @num_reqs = [:save_every] || nil Middleware.mode = [:mode] || :cpu Middleware.interval = [:interval] || 1000 Middleware.enabled = [:enabled] Middleware.path = [:path] || 'tmp' at_exit{ Middleware.save? } if [:save_at_exit] end |
Class Attribute Details
.enabled ⇒ Object Also known as: enabled?
Returns the value of attribute enabled.
31 32 33 |
# File 'lib/stackprof/middleware.rb', line 31 def enabled @enabled end |
.interval ⇒ Object
Returns the value of attribute interval.
31 32 33 |
# File 'lib/stackprof/middleware.rb', line 31 def interval @interval end |
.mode ⇒ Object
Returns the value of attribute mode.
31 32 33 |
# File 'lib/stackprof/middleware.rb', line 31 def mode @mode end |
.path ⇒ Object
Returns the value of attribute path.
31 32 33 |
# File 'lib/stackprof/middleware.rb', line 31 def path @path end |
Class Method Details
.save(filename = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/stackprof/middleware.rb', line 34 def save(filename = nil) if results = StackProf.results FileUtils.mkdir_p(Middleware.path) filename ||= "stackprof-#{results[:mode]}-#{Process.pid}-#{Time.now.to_i}.dump" File.open(File.join(Middleware.path, filename), 'wb') do |f| f.write Marshal.dump(results) end filename end end |
Instance Method Details
#call(env) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/stackprof/middleware.rb', line 17 def call(env) StackProf.start(mode: Middleware.mode, interval: Middleware.interval) if Middleware.enabled? @app.call(env) ensure if Middleware.enabled? StackProf.stop if @num_reqs && (@num_reqs-=1) == 0 @num_reqs = @options[:save_every] Middleware.save end end end |