Home  >  Spring

Spring StopWatch Example

By Arvind Rai, March 30, 2022
On this page we will learn using Spring StopWatch utility class.
1. The Spring StopWatch is a simple stop watch that allows for timing of number of tasks, exposing total running time and running time for each named task.
2. The use of StopWatch is same as System.nanoTime() method.
3. The StopWatch class is normally used to verify performance during proof-of-concept work and in development. It is not used for production applications.
4. Since Spring 5.2, running time is tracked and reported in nanoseconds.
5. The instance of StopWatch is not thread-safe and does not use synchronization.

6. Find some of the StopWatch methods.
currentTaskName() : Returns currently running task name.
getLastTaskName() : Returns last task name.
getLastTaskTimeMillis() : Returns the time taken by the last task in milliseconds.
getLastTaskTimeNanos() : Returns the time taken by the last task in nanoseconds.
getTaskCount() : Returns the number of task.
getTotalTimeMillis() : Returns the total time in milliseconds for all tasks.
getTotalTimeNanos() : Returns the total time in nanoseconds for all tasks.
getTotalTimeSeconds() : Returns the total time in seconds for all tasks.
isRunning() : Checks if StopWatch is running.
prettyPrint() : Generates results as a table describing all tasks performed.
start() : Starts the task.
stop() : Stops the current task.



Example

UserAspect.java
package com.concretepage;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Aspect
@Component
public class UserAspect {
	@Around("execution(* com.concretepage.UserService.doTask(..))")
	public void userAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
		StopWatch stwatch = new StopWatch(getClass().getSimpleName());
		try{
			stwatch.start(joinPoint.getSignature().getName());
			joinPoint.proceed();
		} finally{
			stwatch.stop();
			System.out.println(stwatch.prettyPrint());
		}
	}
} 
The @Around annotation is used for around advice.
UserService.java
package com.concretepage;
import org.springframework.stereotype.Component;

@Component
public class UserService {
	public void doTask()  {
		System.out.println("do some task.");
	}
} 
AspectConfig.java
package com.concretepage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.concretepage")
public class AspectConfig {
} 
SpringTest.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringTest {
   public static void main(String[] args) {
       AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
       ctx.register(AspectConfig.class);
       ctx.refresh();
       UserService userService = ctx.getBean(UserService.class);
       userService.doTask();           
   }
}  
Output
Spring StopWatch Example

Reference

Spring StopWatch

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us