@@ -402,13 +402,28 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
402402
403403 all_colors = ("c" , "y" , "g" , "r" , "b" )
404404 mem_line_colors = ("k" , "b" , "r" , "g" , "c" , "y" , "m" )
405+
406+ show_trend_slope = options is not None and hasattr (options , 'slope' ) and options .slope is True
407+
405408 mem_line_label = time .strftime ("%d / %m / %Y - start at %H:%M:%S" ,
406409 time .localtime (global_start )) \
407410 + ".{0:03d}" .format (int (round (math .modf (global_start )[0 ] * 1000 )))
408411
412+ mem_trend = None
413+ if show_trend_slope :
414+ # Compute trend line
415+ mem_slope = np .polyfit (t , mem , 1 )
416+ mem_trend = np .poly1d (mem_slope )
417+ # Append slope to label
418+ mem_line_label = mem_line_label + " slope {0:.5f}" .format (mem_slope [0 ])
419+
409420 pl .plot (t , mem , "+-" + mem_line_colors [index % len (mem_line_colors )],
410421 label = mem_line_label )
411422
423+ if show_trend_slope :
424+ # Plot the trend line
425+ pl .plot (t , mem_trend (mem ), "--" , linewidth = 0.5 , color = "#00e3d8" )
426+
412427 bottom , top = pl .ylim ()
413428 bottom += 0.001
414429 top -= 0.001
@@ -422,9 +437,21 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
422437 cts = np .asarray ([item [1 ] for item in data ]) - global_start
423438 cmem = np .asarray ([item [0 ] for item in data ])
424439
440+ cmem_trend = None
441+ child_mem_trend_label = ""
442+ if show_trend_slope :
443+ # Compute trend line
444+ child_mem_slope = np .polyfit (cts , cmem , 1 )
445+ cmem_trend = np .poly1d (child_mem_slope )
446+ child_mem_trend_label = " slope {0:.5f}" .format (child_mem_slope [0 ])
447+
425448 # 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 ))
449+ pl .plot (cts , cmem , "+-" + mem_line_colors [(idx + 1 ) % len (mem_line_colors )],
450+ label = "child {}{}" .format (proc , child_mem_trend_label ))
451+
452+ if show_trend_slope :
453+ # Plot the trend line
454+ pl .plot (cts , cmem_trend (cts ), "--" , linewidth = 0.5 , color = "black" )
428455
429456 # Detect the maximal child memory point
430457 cmax_mem = cmem .max ()
@@ -710,6 +737,8 @@ def xlim_type(value):
710737 help = "Plot a time-subset of the data. E.g. to plot between 0 and 20.5 seconds: --window 0,20.5" )
711738 parser .add_argument ("--flame" , "-f" , dest = "flame_mode" , action = "store_true" ,
712739 help = "Plot the timestamps as a flame-graph instead of the default brackets" )
740+ parser .add_argument ("--slope" , "-s" , dest = "slope" , action = "store_true" ,
741+ help = "Plot a trend line and its numerical slope" )
713742 parser .add_argument ("--backend" ,
714743 help = "Specify the Matplotlib backend to use" )
715744 parser .add_argument ("profiles" , nargs = "*" ,
@@ -810,4 +839,4 @@ def main():
810839 actions [get_action ()]()
811840
812841if __name__ == "__main__" :
813- main ()
842+ main ()
0 commit comments