In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to know the execution time of each method of the whole system". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "how to know the execution time of each method in the whole system"!
(1) Preface
Recently, by the end of the year, the system has been stable for three months since it was launched. However, the project manager feels that the system has slowed down recently and wants to know the execution time of all methods in the front end and back end of the entire system. Yes, you heard me correctly. It's every way. This requirement is blinded as soon as it is put forward, and each method is given a calculation of execution time. Isn't this putting pressure on the system in vain?
(2) ideas
Now that I have mentioned it, let's give some ideas first. In fact, it is very simple, each method records a timestamp before execution and a timestamp after execution, and the subtraction is the execution time. But it's not realistic to change every method, so much code, it's too much work to hack into, and what if one day the project manager doesn't want this feature?
As soon as you hear this requirement, you should think of AOP, which has been memorized by Spring,IOC and AOP for so long, and now is the time to apply it.
(3) use AOP to record time
The concept of AOP is not mentioned here. Aspect-oriented, non-intrusive development is suitable for this requirement. And 20 lines of code can solve the problem.
3.1 introduction of dependent org.springframework.boot spring-boot-starter-aop3.2 configuration section
First configure a pointcut, where I want the pointcut to take effect on all methods in the TestController class
@ Slf4j@Component@Aspect@Order (10) public class LogAspect {/ / configure the pointcut, that is, the location @ Pointcut ("execution (* com.javayz.codinguser.controller.TestController.* (..)")) Public void pointCut () {} / / uses surround notification to record a time subtraction @ Around ("pointCut ()") public Object doLog (ProceedingJoinPoint proceedingJoinPoint) {long startTime = System.currentTimeMillis (); Object object=null; try {object=proceedingJoinPoint.proceed ();} catch (Throwable throwable) {throwable.printStackTrace () before and after the method execution. } finally {long endTime = System.currentTimeMillis (); log.info (proceedingJoinPoint.getSignature (). ToShortString () + "method executed:" + (endTime-startTime) + "ms");} return object;}}
The TestController method looks like this:
@ RestController@RequestMapping ("/ testApi") public class TestController {@ GetMapping ("/ test1") public String testAop () {try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();} return "testAop";}} 3.3
Call / testApi/test1 directly, and the effect comes out.
3.4 customize a comment
Now it can meet the needs of the project manager, and if the new code is not within the scope of the pointcut, it will not take effect again, so define a comment and write a comment directly when you want to record it.
@ Target (value = ElementType.METHOD) @ Retention (value = RetentionPolicy.RUNTIME) @ Documentedpublic @ interface LogAnnotation {}
Add comments when configuring pointcuts
@ Pointcut ("execution (* com.javayz.codinguser.controller.TestController.* (..)) | | @ annotation (com.javayz.codinguser.annotation.LogAnnotation)") public void pointCut () {}
Well, add this comment when you need to execute it and OK it.
(IV) the question of recording data
The project manager also wants to be able to check the execution time of the method at any time, so the data must be saved.
Now there are 40,000 people using this system. If it is stored in the database, I define three fields in the table: time, caller, calling time, and calling method. A piece of data is about 40 bytes in size. According to the use of 40, 000 people, at least 40 million pieces of data, or about 1.5 gigabytes, will be generated in a day. The amount of data does not matter, the pressure to write to the database is even greater.
If it exists in the log, the log will become very large, and it is not convenient to troubleshoot.
Thank you for your reading. The above is the content of "how to know the execution time of each method in the whole system". After the study of this article, I believe you have a deeper understanding of how to know the execution time of each method in the whole system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.