Use AOP to record performance data for Java Web Applications

Source: Internet
Author: User

Use AOP to record performance data for Java Web Applications

As a developer, application performance is always one of our most interesting topics. However, not all developers understand the performance of their own applications, let alone quickly locate performance bottlenecks and implement solutions.

Most of Beijing's Velocity sponsors this year are engaged in the APM field and provide performance analysis, visualization, and even optimization solutions. The products of these vendors seem to be able to help developers of Small and Medium-sized Enterprises solve their application performance defects, but almost all of these products have a fatal defect: Extremely invasive.

Developers must embed the tracking code provided by the APM vendor in the business production code to use the Saas service provided by the APM vendor. In the ever-changing technological tide, such code-level intrusion and binding always worries developers. If I am an architect and I am still using Saas APM for self-built APM, I will also consider it with caution.

However, whether self-built APM or Saas services are used, the underlying model is nothing more than real-time processing of massive logs, and the data source is the performance logs generated by applications.

If we have data, let's look at data. If all we have are opinions, let's go with mine.

Jim Barksdale

This is an era where data is king. To put it bluntly, data can guide everything!

To put it bluntly, if we do not want to use the highly intrusive service provided by APM, we can only build our own services. For example, we can use AOP to collect the call tree in the thread and the call overhead and output logs, then, ELK (Elasticsearch, Logstash, and Kibana) is used to collect logs and provide functions such as search and visualization. If the collected logs are only used for offline computing, you can directly use Flume to write logs to HDFS.

As the system traffic increases, the above solution will gradually become unable to cope with it, and then you need to implement a high-performance log collection Agent on your own, write the collected logs into MQ that can carry a large number of accumulated messages, such as Kafuka, and then use Storm/JStorm for real-time stream computing.

A few days ago I made a simple attempt to capture call trees and overhead Based on AOP.

Capture call tree and time overhead

The most common method to get the time overhead of a code block in Java isSystem.currentTimeMillis(). Popular Libraries such as Apache and Guava have StopWatch encapsulation class for the function of obtaining time overhead.

There is no common encapsulation for the capture call tree. A recommended method is to give each code block to be analyzed a unique mark in a call, which should reflect the nesting and order among the code blocks.

For example, we have the following call relationships.

 
  1. func1 
  2. +- func2 
  3. |  +- func3 
  4. |  /- func4 
  5. /- func5 

To reflect the nesting and order between calls, we mark 0 for func1, 0.1 for func2, 0.1.1 for func3, 0.1.2 for func4, and 0.2 for func5. In this way, we can easily reconstruct the call tree based on the tag.

We can encapsulate the function of capturing the call tree and recording the time overhead of each code block in a thread-safe way to encapsulate a name similar to Profiler. Profiler provides two static methods: enter is called before the code block is entered, and exit is called after the code block is completed.

When implementing Profiler, you need to maintain a call stack for each thread and analyze the result list. Basically, it can be implemented as "enter" to press the stack, exit the stack, and put the result into the result list. When the call stack is empty, the complete analysis result is output.

AOP and method interceptor

The Profiler has a strict execution Convention, that is, the enter and exit must be called in pairs, just as the new and delete values in C ++ must appear in pairs; otherwise, the memory will be directly cracked, this is far from the ease of Memory leakage.

If such a convention is written into the business code, it will be difficult to see it. Various try finally statements interrupt the business logic, and the original business code is quite disgusting, so it cannot be maintained.

Therefore, we need a more scientific method to call Profiler correctly without intrusion. AOP is a suitable tool.

Here we use Spring AOP as an example to implement a simple example.

First introduce the dependency of Spring AOP, or include the org. aopalliance. intercept. MethodInterceptor package.

 
  1. <dependency> 
  2.     <groupId>org.springframework</groupId> 
  3.     <artifactId>spring-aop</artifactId> 
  4.     <version>2.5.6</version> 
  5. </dependency> 

If you need code to run, you also need to introduce cglib dependencies.

 
  1. <dependency> 
  2.     <groupId>cglib</groupId> 
  3.     <artifactId>cglib-nodep</artifactId> 
  4.     <version>2.2</version> 
  5. </dependency> 

The reference implementation of the method interceptor is as follows. Use code pattern such as try finally to ensure that Profiler is correctly used.

 
  1. public class Interceptor implements MethodInterceptor { 
  2.     @Override 
  3.     public Object invoke(MethodInvocation invocation) throws Throwable { 
  4.         Class clazz = invocation.getMethod().getDeclaringClass(); 
  5.         String method = invocation.getMethod().getName(); 
  6.         String mark = clazz.getCanonicalName() + "#" + method; 
  7.         Profiler.enter(mark); 
  8.         try { 
  9.             return invocation.proceed(); 
  10.         } finally { 
  11.             String log = Profiler.exit(); 
  12.             if (log != null) { 
  13.                 System.out.println(log); 
  14.             } 
  15.         } 
  16.     } 
  17. } 

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.