Permalink
Browse files

Added support for method profiling mode

  • Loading branch information...
1 parent 7d1a04f commit 92f750a95eaa6373581b58c1b323cb1ee90e0dba @bhb bhb committed Jan 17, 2011
@@ -22,9 +22,9 @@ def self.tmpdir
PROFILING_DATA_FILE = ::File.join(self.tmpdir, 'rack_perftools_profiler.prof')
PROFILING_SETTINGS_FILE = ::File.join(self.tmpdir, 'rack_perftools_profiler.config')
DEFAULT_PRINTER = :text
- MODES = [:cputime, :objects, :walltime]
+ MODES = [:cputime, :methods, :objects, :walltime]
DEFAULT_MODE = :cputime
- CHANGEABLE_MODES = [:objects]
+ CHANGEABLE_MODES = [:methods, :objects]
UNSET_FREQUENCY = "-1"
DEFAULT_GEMFILE_DIR = '.'
@@ -125,13 +125,15 @@ def set_env_vars
end
ENV['CPUPROFILE_REALTIME'] = '1' if mode_to_use == :walltime
ENV['CPUPROFILE_OBJECTS'] = '1' if mode_to_use == :objects
+ ENV['CPUPROFILE_METHODS'] = '1' if mode_to_use == :methods
ENV['CPUPROFILE_FREQUENCY'] = @frequency if @frequency != UNSET_FREQUENCY
end
def unset_env_vars
ENV.delete('CPUPROFILE_REALTIME')
ENV.delete('CPUPROFILE_FREQUENCY')
ENV.delete('CPUPROFILE_OBJECTS')
+ ENV.delete('CPUPROFILE_METHODS')
end
def profiling=(value)
@@ -49,7 +49,7 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<perftools.rb>, ["~> 0.5"])
+ s.add_runtime_dependency(%q<perftools.rb>, [">= 0.5.4"])
s.add_runtime_dependency(%q<rack>, ["~> 1.0"])
s.add_runtime_dependency(%q<open4>, ["~> 1.0"])
s.add_development_dependency(%q<rack>, ["~> 1.1"])
@@ -76,6 +76,18 @@ def profile_requests(profiled_app, requests, options = {})
assert_equal '1', objects
end
+ should "set CPUPROFILE_METHODS to 1 if mode is 'methods'" do
+ methods = ENV['CPUPROFILE_METHODS']
+ assert_nil methods
+ app = lambda do |env|
+ methods = ENV['CPUPROFILE_METHODS']
+ [200, {}, ["hi"]]
+ end
+ profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'methods')
+ profile(profiled_app, :data => :none)
+ assert_equal '1', methods
+ end
+
should "not set CPUPROFILE_FREQUENCY by default" do
frequency = ENV['CPUPROFILE_FREQUENCY']
assert_nil frequency
@@ -192,6 +204,19 @@ def profile_requests(profiled_app, requests, options = {})
assert_equal '1', objects
end
+ should "set CPUPROFILE_METHODS to 1 if mode is 'methods'" do
+ methods = ENV['CPUPROFILE_METHODS']
+ assert_nil methods
+ app = lambda do |env|
+ methods = ENV['CPUPROFILE_METHODS']
+ [200, {}, ["hi"]]
+ end
+ profiled_app = Rack::PerftoolsProfiler.new(app, :mode => :cputime)
+ modified_start_env = Rack::MockRequest.env_for('/__start__', :params => 'mode=methods')
+ profile(profiled_app, :start => modified_start_env, :data => :none)
+ assert_equal '1', methods
+ end
+
should "return to default mode if no mode is specified" do
objects = ENV['CPUPROFILE_OBJECTS']
assert_nil objects
@@ -220,7 +245,7 @@ def profile_requests(profiled_app, requests, options = {})
status, _, body = profiled_app.call(modified_start_env)
assert_equal 400, status
- assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'objects'/,
+ assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
@@ -233,7 +258,7 @@ def profile_requests(profiled_app, requests, options = {})
status, _, body = profiled_app.call(modified_start_env)
assert_equal 400, status
- assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'objects'/,
+ assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
@@ -246,7 +271,7 @@ def profile_requests(profiled_app, requests, options = {})
status, _, body = profiled_app.call(modified_start_env)
assert_equal 400, status
- assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'objects'/,
+ assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
@@ -40,6 +40,17 @@ def setup
assert_equal '1', objects
end
+ should "set CPUPROFILE_METHODS to 1 if mode is 'methods'" do
+ methods = ENV['CPUPROFILE_METHODS']
+ assert_nil methods
+ app = lambda do |env|
+ methods = ENV['CPUPROFILE_METHODS']
+ [200, {}, ["hi"]]
+ end
+ status, headers, body = Rack::PerftoolsProfiler.new(app, :mode => 'methods').call(@profiled_request_env)
+ assert_equal '1', methods
+ end
+
should "not set CPUPROFILE_FREQUENCY by default" do
frequency = ENV['CPUPROFILE_FREQUENCY']
assert_nil frequency
@@ -138,6 +149,18 @@ def setup
assert_equal '1', objects
end
+ should "set CPUPROFILE_METHODS to 1 if mode is 'methods'" do
+ methods = ENV['CPUPROFILE_METHODS']
+ assert_nil methods
+ app = lambda do |env|
+ methods = ENV['CPUPROFILE_METHODS']
+ [200, {}, ["hi"]]
+ end
+ request = Rack::MockRequest.env_for("/", :params => 'profile=true&mode=methods')
+ Rack::PerftoolsProfiler.new(app, :mode => :cputime).call(request)
+ assert_equal '1', methods
+ end
+
should "return to default mode if no mode is specified" do
objects = ENV['CPUPROFILE_OBJECTS']
assert_nil objects
@@ -159,7 +182,7 @@ def setup
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
- assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'objects'/,
+ assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
@@ -169,7 +192,7 @@ def setup
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
- assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'objects'/,
+ assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
@@ -179,7 +202,7 @@ def setup
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
- assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'objects'/,
+ assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end

0 comments on commit 92f750a

Please sign in to comment.