Using the Profiling Library in Spring Boot
This article is about using the library to profile methods in Spring Boot applications. It provides a mechanism for monitoring the execution time of methods using the @ActuatorProfiling annotation and Spring Boot Actuator to expose profiling data through custom endpoints.
GitHub https://github.com/litefunction/spring-boot-starter-actuator-profiling
Introduction
The profiling application is designed to help developers monitor and analyze the execution time of methods, with the @ActuatorProfiling annotation. Profiling data is exposed via Spring Boot Actuator.
Peculiarities
• Profiling the execution time of methods.
• Customizable profiling parameters.
• Information about obtaining profiling data in the actuator.
• Thread-safe management of profiling data.
Beginning of work
Maven dependency
To start using the library, add the following dependency to your project:
<dependency>
<groupId>io.github.litefunction</groupId>
<artifactId>spring-boot-starter-actuator-profiling</artifactId>
<version>1.0.0</version>
</dependency>
Configuration
The application can be configured using properties in application.properties or application.yml.
Default Configuration
profiling.actuator.maxCountList=100
profiling.actuator.enabler=true
to configure the actuator display
management.endpoints.web.exposure.include=actuatorProfilingDetail,actuatorProfiling
Usage
Adding Profiling to Methods
To profile a method, simply annotate it with @ActuatorProfiling and optionally give it a name:
import com.lite.function.profiling.ActuatorProfiling;
public class ExampleService {
@ActuatorProfiling(name = "exampleMethod")
public void exampleMethod() {
// логика метода
}
}
Access to profiling data
Profiling data is available through the provided Actuator endpoints.
Endpoints
List of profiling data
Endpoint: /actuator/actuatorProfiling
{
"executingProfilingMethod": {
"exampleMethod": 403
}
}
• exampleMethod – method name from the @ActuatorProfiling annotation
• 403 – execution time of the last method call in milliseconds
Detailed profiling data
Endpoint: /actuator/actuatorProfilingDetail/{name}
Sample answer:
{
"name": "exampleMethod",
"executingProfilingMethodVals": [
{
"delta": 506,
"localDateTime": "2024-06-23T14:28:39.523107"
},
{
"delta": 103,
"localDateTime": "2024-06-23T14:28:43.101866"
},
{
"delta": 904,
"localDateTime": "2024-06-23T14:28:44.564218"
},
{
"delta": 403,
"localDateTime": "2024-06-23T14:28:47.180746"
}
]
}
• name – method name
• executingProfilingMethodVals – list of objects with method execution time (delta in milliseconds) and timestamp (localDateTime)
Conclusion
Using a method profiling library in Spring Boot allows you to effectively monitor and analyze the execution time of methods. This is especially useful for identifying performance bottlenecks and optimizing code. Thanks to the @ActuatorProfiling annotation and integration with Spring Boot Actuator, the profiling process becomes simple and convenient.