Java monitoring method running time/efficiency method, java monitoring

Source: Internet
Author: User

Java monitoring method running time/efficiency method, java monitoring

Preface:

  I am writing a small project this week. Although it is small, it only calls the external interface. After the interface is called, the data returned by the non-stop loop interface (which has been converted to JSONArray) is executed, and then the value is determined, the key to doing different processing is that the data volume is still relatively large. This process has not been launched yet, and the test has not yet started. I want to first check the running efficiency of each method, the provincial data is too big for the project to be suspended (there are many loop judgments, and sometimes there are two for nested loops), that is, purely time-based monitoring without monitoring of memory and cpu.

The Spring AOP technology is mainly used to perform cross-cutting processing on the method to be counted. The timer starts before the method is executed, and the timer stops after the method is executed. the time consumed by this method is obtained.

Steps:

  • First, write your own Interceptor class to implement the MethodInterceptor class, and use it to cut in the method and run the timing code.
  • Spring aop xml configuration, configuration of methods to be monitored and cut-in methods (custom Interceptor)

Java code:

Package com. cplatform. tencent. task; import java. util. hashMap; import java. util. map; import org. aopalliance. intercept. methodInterceptor; import org. aopalliance. intercept. methodInvocation; import org. apache. commons. lang. time. stopWatch;/*** method run time test * @ author liuyt * @ date 2014-11-16 3:39:08 * bolgs http://www.cnblogs.com/liuyitian/ */public class MethodTimeActive implements MethodInterceptor {/*** custom map set, key: method name, value: [0: Running Times, 1: Total time] */public static Map <String, Long []> methodTest = new HashMap <String, long []> ();/*** intercept the method to be executed */public Object invoke (MethodInvocation invocation) throws Throwable {// create a timer StopWatch watch = new StopWatch (); // The timer starts watching. start (); // execution method Object object = invocation. proceed (); // The timer stops watching. stop (); // method name String methodName = invocation. getMethod (). getName (); // get the timer time Long time = watch. getTime (); if (methodTest. containsKey (methodName) {Long [] x = methodTest. get (methodName); x [0] ++; x [1] + = time;} else {methodTest. put (methodName, new Long [] {1L, time}) ;}return object ;}}

XML configuration:

<! -- Logging the time required for methods in a class aop --> <aop: config> <! -- Spring 2.0 can use the AspectJ syntax to define Pointcut. Here, you can customize the package of the method to intercept --> <aop: advisor id = "methodTimeLog" advice-ref = "methodTimeAdvice" pointcut = "execution (* com. cplatform. tencent. sync .. *. *(..)) "/> <aop: advisor id =" methodTimeLog2 "advice-ref =" methodTimeAdvice "pointcut =" execution (* com. cplatform. tencent. utils .. *. *(..)) "/> </aop: config> <bean id =" methodTimeAdvice "class =" com. cplatform. tencent. task. methodTimeActive "/>

When using the Aspect-Oriented AOP technology, do not ignore the following configurations in the XML Configuration:

TEST:

Package test; import java. util. map; import java. util. set; import org. junit. after; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. annotation. autowired; import org. springframework. test. context. activeProfiles; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import com. cplatform. tencent. sync. persistenceTicketService; import com. cplatform. tencent. task. methodTimeActive; import com. cplatform. tencent. utils. appConfig; @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = {"classpath: spring-configuration /*. xml "}) @ ActiveProfiles (" production ") public class CopyOfDBTest {@ Autowired private AppConfig appConfig; // This Is What I used in my project to ignore @ Autowired private PersistenceTicketService persistenceTicketService; // Test a method. This method internally calls many business processing methods @ Test public void testInsertOrUpdate () {persistenceTicketService. insertOrUpdate (appConfig. getTicketCityIds (), appConfig. getAgentId ();} // After the test method is run, retrieve the defined Map set and obtain the data @ After public void testMethodActive () {Map <String, long []> map = MethodTimeActive. methodTest; Set <String> set = map. keySet (); Long [] x = null; for (String s: set) {x = map. get (s); System. out. println (s + ":" + x [0] + "Times," + x [1] + "millisecond ");}}}

The above uses Spring and junit to test the WEB method,For more information, see:SpringJunit4 for unit testing

Console output:

 In fact, it is not difficult. You may also share a clown. You are welcome to make a brick.

                        It is not easy to write, and it is inevitable that there are omissions and errors. Please make a generous correction. It is good and recommended.

   Ps: You are welcome to reprint it. Please indicate the source for reprinting:Http://www.cnblogs.com/liuyitian/p/4101531.html

Learn a little more code every day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.