@@ -113,6 +113,25 @@ def help?
113113 def flamegraph?
114114 profile_value == 'flamegraph'
115115 end
116+
117+ def profile_gc?
118+ profile_value == 'profile-gc'
119+ end
120+
121+ def profile_memory?
122+ profile_value == 'profile-memory'
123+ end
124+
125+ def memory_profiler_options
126+ options = {
127+ ignore_files : @query_params [ 'memory_profiler_ignore_files' ] ,
128+ allow_files : @query_params [ 'memory_profiler_allow_files' ] ,
129+ }
130+
131+ options [ :top ] = Integer ( @query_params [ 'memory_profiler_top' ] ) if @query_params . key? ( 'memory_profiler_top' )
132+
133+ options
134+ end
116135 end
117136
118137 class << self
@@ -297,14 +316,33 @@ def call(env)
297316 client_settings . disable_profiling = false
298317
299318 # profile gc
300- if matches_action? ( 'profile-gc' , env )
319+ if query_settings . profile_gc?
320+ return tool_disabled_message ( client_settings ) if !advanced_debugging_enabled?
301321 current . measure = false if current
302322 return serve_profile_gc ( env , client_settings )
303323 end
304324
305325 # profile memory
306- if matches_action? ( 'profile-memory' , env )
307- return serve_profile_memory ( env , client_settings )
326+ if query_settings . profile_memory?
327+ return tool_disabled_message ( client_settings ) if !advanced_debugging_enabled?
328+
329+ unless defined? ( MemoryProfiler ) && MemoryProfiler . respond_to? ( :report )
330+ message = "Please install the memory_profiler gem and require it: add gem 'memory_profiler' to your Gemfile"
331+ status , headers , body = @app . call ( env )
332+ body . close if body . respond_to? :close
333+
334+ return client_settings . handle_cookie (
335+ text_result ( message , status : 500 , headers : headers )
336+ )
337+ end
338+
339+ result = StringIO . new
340+ report = MemoryProfiler . report ( query_settings . memory_profiler_options ) do
341+ _ , _ , body = @app . call ( env )
342+ body . close if body . respond_to? :close
343+ end
344+ report . pretty_print ( result )
345+ return client_settings . handle_cookie ( text_result ( result . string ) )
308346 end
309347
310348 # any other requests past this point are going to the app to be profiled
@@ -354,7 +392,6 @@ def call(env)
354392 if defined? ( StackProf ) && StackProf . respond_to? ( :run )
355393 # do not sully our profile with mini profiler timings
356394 current . measure = false
357- match_data = action_parameters ( env ) [ 'flamegraph_sample_rate' ]
358395
359396 sample_rate = query_settings . flamegraph_sample_rate || config . flamegraph_sample_rate
360397 mode = query_settings . flamegraph_mode || config . flamegraph_mode
0 commit comments