public String getTopLines ( int num ) { stopCollecting ( ) ; CountMap < String > ls = new CountMap < String > ( lines ) ; StringBuilder buff = new StringBuilder ( ) ; buff . append ( "Top lines over " ) . append ( time ) . append ( " ms (" ) . append ( pauseTime ) . append ( " ms paused), with " ) . append ( total ) . append ( " counts:" ) . append ( LINE_SEPARATOR ) ; for ( int i = 0 , n = 0 ; ls . size ( ) > 0 && n < num ; i ++ ) { int highest = 0 ; List < Map . Entry < String , Integer > > bests = new ArrayList < Map . Entry < String , Integer > > ( ) ; for ( Map . Entry < String , Integer > el : ls . entrySet ( ) ) { if ( el . getValue ( ) > highest ) { bests . clear ( ) ; bests . add ( el ) ; highest = el . getValue ( ) ; } else if ( el . getValue ( ) == highest ) { bests . add ( el ) ; } } for ( Map . Entry < String , Integer > e : bests ) { ls . remove ( e . getKey ( ) ) ; int percent = 100 * highest / Math . max ( total , 1 ) ; buff . append ( "Rank: " ) . append ( i + 1 ) . append ( ", Self: " ) . append ( percent ) . append ( "%, Trace: " ) . append ( LINE_SEPARATOR ) . append ( e . getKey ( ) ) . append ( LINE_SEPARATOR ) ; n ++ ; } } return buff . toString ( ) ; }
Stop and obtain the top lines ordered by their execution time .