Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom dump path #5

Merged
merged 2 commits into from
Dec 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ PATH
GEM
remote: https://rubygems.org/
specs:
metaclass (0.0.1)
mocha (0.14.0)
metaclass (~> 0.0.1)
rake (10.1.0)
rake-compiler (0.9.1)
rake
Expand All @@ -14,5 +17,6 @@ PLATFORMS
ruby

DEPENDENCIES
mocha
rake-compiler
stackprof!
20 changes: 12 additions & 8 deletions lib/stackprof/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
module StackProf
class Middleware
def initialize(app, options = {})
@app = app
@options = options
@num_reqs = options[:save_every] || nil
Middleware.mode = options[:mode] || :cpu
@app = app
@options = options
@num_reqs = options[:save_every] || nil

Middleware.mode = options[:mode] || :cpu
Middleware.interval = options[:interval] || 1000
Middleware.enabled = options[:enabled]
Middleware.enabled = options[:enabled]
Middleware.path = options[:path] || 'tmp'
at_exit{ Middleware.save? } if options[:save_at_exit]
end

Expand All @@ -26,17 +28,19 @@ def call(env)
end

class << self
attr_accessor :enabled, :mode, :interval
attr_accessor :enabled, :mode, :interval, :path
alias enabled? enabled

def save
if results = StackProf.results
FileUtils.mkdir_p('tmp')
File.open("tmp/stackprof-#{results[:mode]}-#{Process.pid}-#{Time.now.to_i}.dump", 'wb') do |f|
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
end
end

end
end
end
1 change: 1 addition & 0 deletions stackprof.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_development_dependency 'rake-compiler'
s.add_development_dependency 'mocha'
end
41 changes: 41 additions & 0 deletions test/test_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
$:.unshift File.expand_path('../../lib', __FILE__)
require 'stackprof'
require 'stackprof/middleware'
require 'test/unit'
require 'mocha/setup'

class StackProf::MiddlewareTest < Test::Unit::TestCase

def test_path_default
StackProf::Middleware.new(Object.new)

assert_equal 'tmp', StackProf::Middleware.path
end

def test_path_custom
StackProf::Middleware.new(Object.new, { path: '/foo' })

assert_equal '/foo', StackProf::Middleware.path
end

def test_save_default
StackProf::Middleware.new(Object.new)

StackProf.stubs(:results).returns({ mode: 'foo' })
FileUtils.expects(:mkdir_p).with('tmp')
File.expects(:open).with(regexp_matches(/^tmp\/stackprof-foo/), 'wb')

StackProf::Middleware.save
end

def test_save_custom
StackProf::Middleware.new(Object.new, { path: '/foo' })

StackProf.stubs(:results).returns({ mode: 'foo' })
FileUtils.expects(:mkdir_p).with('/foo')
File.expects(:open).with(regexp_matches(/^\/foo\/stackprof-foo/), 'wb')

StackProf::Middleware.save
end

end