@@ -230,8 +230,10 @@ def run_action():
230230 if not program [0 ].startswith ("python" ):
231231 program .insert (0 , sys .executable )
232232 cmd_line = get_cmd_line (program )
233- program [1 :1 ] = ("-m" , "memory_profiler" , "--timestamp" ,
234- "-o" , mprofile_output )
233+ extra_args = ["-m" , "memory_profiler" , "--timestamp" , "-o" , mprofile_output ]
234+ if args .include_children :
235+ extra_args .append ("--include-children" )
236+ program [1 :1 ] = extra_args
235237 p = subprocess .Popen (program )
236238 else :
237239 cmd_line = get_cmd_line (program )
@@ -402,13 +404,28 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
402404
403405 all_colors = ("c" , "y" , "g" , "r" , "b" )
404406 mem_line_colors = ("k" , "b" , "r" , "g" , "c" , "y" , "m" )
407+
408+ show_trend_slope = options is not None and hasattr (options , 'slope' ) and options .slope is True
409+
405410 mem_line_label = time .strftime ("%d / %m / %Y - start at %H:%M:%S" ,
406411 time .localtime (global_start )) \
407412 + ".{0:03d}" .format (int (round (math .modf (global_start )[0 ] * 1000 )))
408413
414+ mem_trend = None
415+ if show_trend_slope :
416+ # Compute trend line
417+ mem_trend = np .polyfit (t , mem , 1 )
418+
419+ # Append slope to label
420+ mem_line_label = mem_line_label + " slope {0:.5f}" .format (mem_trend [0 ])
421+
409422 pl .plot (t , mem , "+-" + mem_line_colors [index % len (mem_line_colors )],
410423 label = mem_line_label )
411424
425+ if show_trend_slope :
426+ # Plot the trend line
427+ pl .plot (t , t * mem_trend [0 ] + mem_trend [1 ], "--" , linewidth = 0.5 , color = "#00e3d8" )
428+
412429 bottom , top = pl .ylim ()
413430 bottom += 0.001
414431 top -= 0.001
@@ -422,9 +439,21 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
422439 cts = np .asarray ([item [1 ] for item in data ]) - global_start
423440 cmem = np .asarray ([item [0 ] for item in data ])
424441
442+ cmem_trend = None
443+ child_mem_trend_label = ""
444+ if show_trend_slope :
445+ # Compute trend line
446+ cmem_trend = np .polyfit (cts , cmem , 1 )
447+
448+ child_mem_trend_label = " slope {0:.5f}" .format (cmem_trend [0 ])
449+
425450 # Plot the line to the figure
426- pl .plot (cts , cmem , "+-" + mem_line_colors [(idx + 1 ) % len (mem_line_colors )],
427- label = "child {}" .format (proc ))
451+ pl .plot (cts , cmem , "+-" + mem_line_colors [(idx + 1 ) % len (mem_line_colors )],
452+ label = "child {}{}" .format (proc , child_mem_trend_label ))
453+
454+ if show_trend_slope :
455+ # Plot the trend line
456+ pl .plot (cts , cts * cmem_trend [0 ] + cmem_trend [1 ], "--" , linewidth = 0.5 , color = "black" )
428457
429458 # Detect the maximal child memory point
430459 cmax_mem = cmem .max ()
@@ -710,6 +739,8 @@ def xlim_type(value):
710739 help = "Plot a time-subset of the data. E.g. to plot between 0 and 20.5 seconds: --window 0,20.5" )
711740 parser .add_argument ("--flame" , "-f" , dest = "flame_mode" , action = "store_true" ,
712741 help = "Plot the timestamps as a flame-graph instead of the default brackets" )
742+ parser .add_argument ("--slope" , "-s" , dest = "slope" , action = "store_true" ,
743+ help = "Plot a trend line and its numerical slope" )
713744 parser .add_argument ("--backend" ,
714745 help = "Specify the Matplotlib backend to use" )
715746 parser .add_argument ("profiles" , nargs = "*" ,
0 commit comments