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

Add memory_profiler error when not installed #488

Merged
merged 1 commit into from Apr 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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