Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which part of the source code is taking more time to execute in Java REST APIs built on SpringBoot?

I have a REST Web Service built on Spring Boot and consuming some back-end REST APIs and executing database operations along with some Redis cache transactions. It is taking more than 7 or 8 seconds to get response when i hit the API through Postman tool. Initially it was 2 or 3 seconds. When i see the executing time by using Java Date API before and after every operations like database transaction, redis cache transaction, consuming other REST services etc. and they are like 20ms, 90ms, 500ms etc., respectively. I am unable to find where the API response is getting delayed while executing the Java Code. (Also some asynchronous code is present in btw.)

For the time being, i cannot put the execution time difference across the source code due to huge amount of code and multiple service implementations in a single API call.

Is there an open-source tool to find out the source code execution time to understand the performance or response time of a REST web service to further debug or improve the performance easily ?

Normally using tools like JMeter, we can see the performance results but how can we know which part of the code is actually taking a specific time like how SONAR code quality tool that detects which line of code may cause particular kind of bugs or issues or potential bugs or security threats.

Any help is highly appreciated.

like image 421
Vamsi Avatar asked Dec 02 '25 04:12

Vamsi


1 Answers

Java Flight Recorder and Mission Control

Oracle's Java Flight Recorder is actually built into the Oracle JVM, but you need to use a JDK version that has JFR built in. Oracle also provides a set of tools called Java Mission Control that can be used to analyze and visualize the outputs of JFR. Of course, other open-source tools are also available to convert JFR outputs to flame graphs. JFR and JMC are free for development, but require commercial licenses to deploy into production. However, Oracle has kindly included JFR and JMC as of OpenJDK 11.

Spring AOP

Spring AOP can also be used to perform method profiling. See Profiling a Java Spring application and Spring App Method Profiling. This can help with simple debugging, but there is no built-in data visualization. Once you've identified problem methods (due to long or high-variance response times), you can annotate them with AOP and monitor them in a log file or using Spring Data ElasticSearch (advanced).

Simple Performance Framework for Java (SPF4J)

SPF4J is an open-source library. It's pretty easy to use but community support is lacking, so you will need to spend some time figuring out how it works. You can use annotations to flag and monitor specific methods or you can use AOP to profile the entire stack trace. The output is a series of ssdump files or Time-Series DataBase (TSDB) files, which you can visualize in a GUI tool that shows time graphs of min/max/average metrics and displays flame graphs to analyze the performance for all method calls in a stack trace. Here is a tutorial and sample code explaining how to profile a jaxrs REST API.

Flame Graph

VisualVM

I was hesitant to add VisualVM to this list, since it is mostly used for CPU and memory profiling. It doesn't specialize in timing profiles. However, it is a popular option and, upon closer inspection, it does look like you can get some timing information as shown in the screenshot below. This tool would definitely be worth looking into as well.

VisualVM

IDE Profilers

There are also tools that come with IDEs, such as NetBeans Profiler or IntelliJ Ultimate Profiling Tools (commercial) that can also do the job. IntelliJ uses Java Flight Recorder (JFR) under the hood.

like image 154
DV82XL Avatar answered Dec 04 '25 18:12

DV82XL