Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling a Java Spring application [closed]

I have a Spring application that I believe has some bottlenecks, so I'd like to run it with a profiler to measure what functions take how much time. Any recommendations to how I should do that?

I'm running STS, the project is a maven project, and I'm running Spring 3.0.1

like image 311
niklassaers Avatar asked Mar 19 '10 07:03

niklassaers


People also ask

How do I Profilate a spring boot application?

Profiling is achieved by instrumenting either the program source code or its binary executable form using a tool called a profiler (or code profiler). Profilers may use a number of different techniques, such as event-based, statistical, instrumented, and simulation methods.

How do I profile an app in Java?

Choose Profile > Advanced Commands > Run Profiler Calibration from the main menu. Choose the Java Platform to be used for profiling and click OK.

How do I refresh application context in spring boot?

For a Spring Boot Actuator application, some additional management endpoints are available. You can use: POST to /actuator/env to update the Environment and rebind @ConfigurationProperties and log levels. /actuator/refresh to re-load the boot strap context and refresh the @RefreshScope beans.

How do Java profilers work?

Sampling profilers work by periodically querying the JVM for all the running threads and getting the stack trace for each thread. It then determines what method each thread was executing when the sample was taken and compares the samples to determine how much time was spent in that method.


2 Answers

I've done this using Spring AOP.

Sometime I need an information about how much time does it take to execute some methods in my project (Controller's method in example).

In servlet xml I put

<aop:aspectj-autoproxy/>

Also, I need to create the class for aspects:

@Component
@Aspect
public class SystemArchitecture {

    @Pointcut("execution(* org.mywebapp.controller..*.*(..))")
    public void businessController() {
    }
}

And profiler aspect:

@Component
@Aspect
public class TimeExecutionProfiler {

    private static final Logger logger = LoggerFactory.getLogger(TimeExecutionProfiler.class);

    @Around("org.mywebapp.util.aspects.SystemArchitecture.businessController()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        logger.info("ServicesProfiler.profile(): Going to call the method: {}", pjp.getSignature().getName());
        Object output = pjp.proceed();
        logger.info("ServicesProfiler.profile(): Method execution completed.");
        long elapsedTime = System.currentTimeMillis() - start;
        logger.info("ServicesProfiler.profile(): Method execution time: " + elapsedTime + " milliseconds.");

        return output;
    }

    @After("org.mywebapp.util.aspects.SystemArchitecture.businessController()")
    public void profileMemory() {
        logger.info("JVM memory in use = {}", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
    }
}

That's all. When I request a page from my webapp, the information about method executing time and JVM memory usage prints out in my webapp's log file.

like image 143
Yuri.Bulkin Avatar answered Oct 14 '22 10:10

Yuri.Bulkin


I recommend VisualVM for general application profiling. It is available in the JDK from version 1.6_10, and imho much faster and usable than Eclipse TPTP.

(If your Spring application works in an application server (e.g. Tomcat) you could try to deploy it to the tc Server developer edition (available in the STS downloads). It has interesting monitoring capabilities. It seems that tc Server developer edition is not maintained anymore.)

UPDATE 2019.02.22.: updated VisualVM url (thanks for @Jeff) and tc Server information. Personally I currently use Glowroot for monitoring Spring applications running in an application server.

like image 22
Csaba_H Avatar answered Oct 14 '22 12:10

Csaba_H