SoFunction
Updated on 2025-04-05

Detailed explanation of the time-consuming method of SpringBoot statistical interface request

Write in front

The speed of the interface request time represents the speed of obtaining the corresponding data, and also represents the speed of the user requesting page data. You can often use the speed of the interface request speed to perform corresponding optimization!

In the past, we might have done it to add the current time at the beginning of each interface method, and subtract the start time at the end to represent the access time of the interface.

The specific code is as follows:

@RequestMapping("/test")
public String test(){
    long startTime = ();
    //The call business code here is omitted    ("Access time is:"+(()-startTime));
    return "Access interface successful";
}

If there are hundreds of interfaces and each interface needs to count the corresponding access time, it will have to be written hundreds of times. This is not in line with our common sense. So is there a way to not modify the corresponding interface method, and it can be applied to all interfaces or specified interfaces just by writing it once.

We can think of AOP technology at the first time. AOP is a common technology in Spring. AOP can enhance the interface method without modifying the original code. Using AOP can isolate various parts of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development.

Solution

According to the above, we need to go to AOP, and the first thing that cannot be missing is the corresponding dependency.

Introduce corresponding dependencies

<!--aspectj-->
<dependency>
	<groupId></groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
	<version>2.7.4</version>
</dependency>

Custom annotations

The time-consuming and number of accesses of statistical interfaces does not require each interface to be used. For example, some interfaces that are not frequently accessed do not count their accesses, so we can customize an annotation. As long as this annotation is applied to the corresponding interface method, Spring will scan and perform the corresponding statistical time-consuming operation.

import ;
import ;
import ;
import ;
import ;

/**
  * Statistics Method/Interface Time-consuming Annotation
  *
  * @author devcheng
  */
@Documented
@Target()
@Retention()
public @interface CostTime {

}

Define AOP facets

If a custom annotation is applied to the interface method, it will be scanned by Spring. Here I am using @Pointcut and @Around to use it together.

import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;

/**
  * Statistics Method/Interface Time-consuming Annotation
  *
  * @author devcheng
  */
@Aspect
@Component
@Slf4j
public class CostTimeAspect {

    @Pointcut(value = "@annotation()")
    public void costTime() {
    }

    @Around("costTime()")
    public Object costTimeAround(ProceedingJoinPoint joinPoint) {
        Object obj = null;
        try {
            long beginTime = ();
            obj = ();
            //Get the method name            String method = ().getName();
            //Get the class name            String className=().getDeclaringTypeName();
            // Calculation time            long cost = () - beginTime;
            ("kind:[{}],method:[{}] Time-consuming interface:[{}]", className,method, cost + "millisecond");
        } catch (Throwable throwable) {
            ();
        }
        return obj;
    }

}

Used on statistical interface

@GetMapping("/V4/getSignsPredictDetail")
@ResponseBody
@CostTime
public String getSignsPredictDetail(String name) {
    if ((name)) {
        return "[]";
    }
    return (name);
}

Used for statistics and timing tasks

@Scheduled(cron = "55 */5 * * * ?")
@CostTime
public void scenesSignTask() {
    // Business logic}

Run the output

2022-11-18 10:31:51.523 [http-nio-8886-exec-8] ERROR  Line:32  - kind:[],method:[getWeather] Time-consuming interface:[0millisecond]
2022-11-18 10:31:52.122 [http-nio-8886-exec-9] ERROR  Line:32  - kind:[],method:[getWeather] Time-consuming interface:[1millisecond]
2022-11-18 10:31:55.073 [http-nio-8886-exec-15] ERROR  Line:32  - kind:[.CityBrain4Controller],method:[getScrollingMessages] Time-consuming interface:[2millisecond]
2022-11-18 10:31:55.076 [http-nio-8886-exec-3] ERROR  Line:32  - kind:[],method:[getWeather] Time-consuming interface:[1millisecond]

The above is a detailed explanation of the time-consuming method for SpringBoot statistical interface requests. For more information about SpringBoot interface requests, please pay attention to my other related articles!