java runtime monitoring method / process efficiency

  Introduction:
  
  This peripheral interface also kept in the write cycle of the interface to return a small item, a pure tone is small but the external interface, data transfer is over (the JSONArray converted), then determination value, then a different process, the key is relatively large amount of data, which has not yet begun on line just finished the test also measured it has not yet begun to think of themselves first take a look at the efficiency of each method, the province's large data projects hang (circulation judge lot, sometimes there are two nested for loop), is the pure monitoring in time, no memory and cpu monitoring.
  
  The main advantage of the Spring AOP technology, statistical methods you want to cross-cut process, start the timer before the method execution, stop the clock after the implementation of the method, the method is to get the timing of this time-consuming.
  
  Steps:
  
  First, write your own classes to implement Interceptor MethodInterceptor class, to a method for cutting, run timing code
  
  method Spring AOP XML configuration, monitoring and configuration needs cut method (custom Interceptor)
  
  the Java code is:
  
  copy the code
  
  package com. cplatform.tencent.task;
  
  Import the java.util.HashMap;
  
  Import a java.util.Map;
  
  Import org.aopalliance.intercept.MethodInterceptor;
  
  Import org.aopalliance.intercept.MethodInvocation;
  
  Import org.apache.commons.lang.time.StopWatch ;
  
  / **
  
  * run-time test method
  
  @Author liuyt *
  
  * PM @date 2014-11-16 3:39:08
  
  * bolgs http://www.cnblogs.com/liuyitian/
  
  * /
  
  public class MethodTimeActive the implements MethodInterceptor {
  
  / **
  
  * custom map set, key : method name, value: [0: number of runs, 1: total time]
  
  * /
  
  public static the Map <String, Long []> = new new methodTest the HashMap <String, Long []> ();
  
  / **
  
  * interception to be performed method
  
  * /
  
  public Object Invoke (the MethodInvocation Invocation) throws the Throwable {
  
  // Create a timer
  
  the StopWatch Watch the StopWatch new new = ();
  
  // start timer
  
  watch.start ();
  
  // perform a method
  
  Object object = invocation.proceed ( );
  
  // timer is stopped
  
  watch.stop (www.yaoshiyulegw.com);
  
  // method name
  
  Invocation.getMethod methodName = String () getName ();.
  
  // Get time timer
  
  Long Time = watch.getTime ();
  
  IF (methodTest.containsKey (methodName)) {
  
  Long [] X = methodTest.get (methodName) ;
  
  X [0] ++;
  
  X [. 1] + = Time;
  
  } the else {
  
  methodTest.put (methodName, new new Long [] {1L, Time});
  
  }
  
  return Object;
  
  }
  
  }
  
  copy the code
  
  XML configuration:
  
  copy Code
  
  < ! - logging in a class method takes time AOP ->
  
  <AOP: config>
  
  - syntax definition Pointcut Spring 2.0 can use AspectJ, where the custom package is located to intercept method - <>!
  
  <AOP: advisor id = "methodTimeLog" advice- ref = "methodTimeAdvice" pointcut = "execution (* com.cplatform.tencent.sync .. *. * (..))" />
  
  <aop: advisor id = www.changjianggw.com " methodTimeLog2" www.haihongyule.com advice-ref = "methodTimeAdvice" pointcut = "execution (* com.cplatform.tencent.utils .. * * (..).)" />
  
  </ AOP: config>
  
  <the bean ID = "methodTimeAdvice" class = "WWW" .honghaiyLpt.com "com.cplatform.tencent.task.MethodTimeActive" />
  
  copy the code
  
  using AOP Aspect oriented technology, XML configuration inside one thousand Wan do not ignore the following configuration:
  
  the TEST:
  
  copy Code
  
  Package Penalty for the 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.www.chaoyuepint.com context.ActiveProfiles;
  
  org.springframework.test.context.ContextConfiguration Import;
  
  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 (www.yuntianyul.cn locations = { "CLASSPATH: Spring-Configuration / * XML."})
  
  @ActiveProfiles ( "Production" www.feishenbo.cn)
  
  CopyOfDBTest class {public
  
  @Autowired
  
  Private AppConfig appconfig; // this is what I used in the project can be ignored
  
  @Autowired
  
  Private persistenceTicketService persistenceTicketService;
  
  // test a method, this method calls inside a lot of business treatment
  
  @Test
  
  public void testInsertOrUpdate () {
  
  persistenceTicketService.insertOrUpdate (appConfig.getTicketCityIds (), appConfig.getAgentId ());
  
  }
  
  // After the test method is complete, remove Map defined set of data taken
  
  @After
  
  public void testMethodActive () {
  
  map <String, Long []> = Map MethodTimeActive.methodTest;
  
  the Set <String> map.keySet SET = ();
  
  Long [] X = null;
  
  for (String S: SET) {
  
  X = as map.get (S);
  
  System.out.println (s + ":" + x [0] + " views," + x [1] + " msec");
  
  }
  
  }
  
  }
  
  copy the code
  
  used above, the Spring and the WEB junit to test the method, Referring specifically: SpringJunit4 unit test
  
  console output:

Guess you like

Origin www.cnblogs.com/qwangxiao/p/11087030.html