Monday, February 16, 2015

Program Task execution time -Spring StopWatch


Stop watch allows us to measure elapsed time or execution time of different task

improving the readability of application code and   measuring performance and execution time of important piece of codes specially methods or loops which executes most of the time.

TaskCompletionHistory  has three method task , task1, task2

program will demonstrate  time taken by each methods and summary (detail description)


import org.springframework.util.StopWatch;

public class TaskCompletionHistory {


public static void main(String[] args) throws Exception {

// create the new StopWatch by springframework
StopWatch watch = new StopWatch("-- Task Completion History --");
// starts StopWatch with specified task name
watch.start("time taken by function task");
task();
task1();
task2();
watch.stop();
// prints the short Summary of taks function
System.out.println("total task info" + watch.shortSummary());

// prints the detail of taks function

System.out.println(watch.prettyPrint());
}

public static void task() throws Exception {

Thread.sleep(100);
}

public static void task1() throws Exception {

Thread.sleep(200);
}

public static void task2() throws Exception {

Thread.sleep(300);
}
}





OUTPUT:-
total task infoStopWatch '-- Task Completion History --': running time (millis) = 600
StopWatch '-- Task Completion History --': running time (millis) = 600
-----------------------------------------
ms     %     Task name
-----------------------------------------
00100  017%  time taken by function task
00200  033%  time taken by function task1
00300  050%  time taken by function task2












No comments:

Post a Comment