Skip to content

Commit

Permalink
Add memory_profiler error when not installed (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcgibbon committed Apr 7, 2021
1 parent cfbcf5d commit 94670b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/mini_profiler/profiler.rb
Expand Up @@ -281,6 +281,15 @@ def call(env)
# profile memory
if query_string =~ /pp=profile-memory/
return tool_disabled_message(client_settings) if !advanced_debugging_enabled?

unless defined?(MemoryProfiler) && MemoryProfiler.respond_to?(:report)
message = "Please install the memory_profiler gem and require it: add gem 'memory_profiler' to your Gemfile"
_, _, body = @app.call(env)
body.close if body.respond_to? :close

return client_settings.handle_cookie(text_result(message))
end

query_params = Rack::Utils.parse_nested_query(query_string)
options = {
ignore_files: query_params['memory_profiler_ignore_files'],
Expand Down
32 changes: 27 additions & 5 deletions spec/integration/middleware_spec.rb
Expand Up @@ -39,18 +39,40 @@ def app
end
end

describe 'with analyze-memory query' do
describe 'when enable_advanced_debugging_tools is true' do
def app
Rack::Builder.new do
use Rack::MiniProfiler
run lambda { |_env| [200, { 'Content-Type' => 'text/html' }, [+'<html><body><h1>Hi</h1></body></html>']] }
end
end

it 'should return ObjectSpace statistics if advanced tools are enabled' do
Rack::MiniProfiler.config.enable_advanced_debugging_tools = true
do_get(pp: 'analyze-memory')
expect(last_response.body).to include('Largest strings:')
before(:each) { Rack::MiniProfiler.config.enable_advanced_debugging_tools = true }

describe 'with analyze-memory query' do
it 'should return ObjectSpace statistics' do
do_get(pp: 'analyze-memory')
expect(last_response.body).to include('Largest strings:')
end
end

describe 'with profile-memory query' do
it 'should return memory_profiler error message' do
do_get(pp: 'profile-memory')
expect(last_response.body).to eq(
'Please install the memory_profiler gem and require it: add gem \'memory_profiler\' to your Gemfile'
)
end
end

describe 'with flamegraph query' do
it 'should return stackprof error message' do
Rack::MiniProfiler.config.enable_advanced_debugging_tools = true
do_get(pp: 'flamegraph')
expect(last_response.body).to eq(
'Please install the stackprof gem and require it: add gem \'stackprof\' to your Gemfile'
)
end
end
end

Expand Down

0 comments on commit 94670b7

Please sign in to comment.