How to test and optimize Java EE Performance

Source: Internet
Author: User

Performance testing objectives

Performance testing is different from functional testing. It is not a right or error test, but a measure of speed and slowness. Before conducting a real performance test, you must first clarify the objectives:

1. Under the specified hardware conditions, the larger the number of concurrent requests, the better the response time. The product manager proposes how many concurrent requests are required and how fast the response time is.

2. After the maximum number of concurrent tasks and the corresponding response time are obtained by testing the hardware. If you increase your hardware investment, what kind of performance improvement will be paid off? Scalability)

The hardware conditions include cpu, memery, I/O, and network bandwidth.

Benchmark Test Benchmarking in Performance Testing

Similar to functional testing, you must design test cases for performance testing. The difference is that you must perform a benchmark test before you officially start your business test cases. Why? In fact, you need to measure your hardware capabilities first. Otherwise, if your test results are not good, how do you know whether the hardware is slow or your software is faulty. These hardware tests include:

1. network bandwidth test. You can copy large files to test the maximum bandwidth of your network.

2. cpu. You can use complicated algorithms to measure the cpu speed.

3. memery. You know the size of memery.

4. IO. You can also test it by copying large files.

In the subsequent optimization process, can these benchmark test cases be used to determine whether the changes have been completed after modification.

Design Your Business Test Cases

The ideal test case is to imitate the real world as much as possible, which is often not possible, especially for new products. You can first record some of the most commonly used cases as the starting point.

In addition, the concept of concurrency needs to be clarified. Concurrent users usually refer to users online at the same time. These users can use different functions of your system. Note that they are not saying that everyone is doing the same thing. A concurrent request to a transaction refers to the concurrent call of a request.

For the latter type of concurrency, you often need to calculate which of the following tasks is concentrated when the number of users is the largest. This request must be fast enough.

After these two test cases are designed, they will become a yardstick for measuring the effectiveness of your improvement in the subsequent optimization process.

Performance Tuning

Performance Tuning starts from the underlying layer, basically from the OS to the JVM, Cache, Buffer Pool, SQL, DB Schema, and algorithm.

Don't change too much at a time. change a bit and test it. This is a slow task and requires patience.

When executing the test, you should also pay attention to the same process. After the system is restarted, warm up before starting the real test. Otherwise, you will find that your test results are very different, not sure.

Also, pay attention to the capabilities of your client, such as JMeter, which requires a lot of memory. Do not use the client because it may be a problem with your system.

When testing and tuning, you need to use some monitoring tools such as JConsole to monitor the system status and find the System Bottleneck. The so-called bottleneck is the slowest part, it is also often seen that 100% is full. For example, your memory or cpu usage is exhausted. If the cpu and memory are not used up, they are waiting for a resource. In this case, you need to use the profile tool, such as JProfile and YourKit.

Use performance monitoring logs

Because the performance problem is not easy to reproduce, when the product environment encounters performance problems, if it is a data problem, maybe when you copy the product data to your testing environment, then we can reproduce and improve the slow query. However, if it is a problem of concurrent users or runtime environments such as the network, it is difficult for you to reproduce it. At this time, if you can see the key slow response methods through the log, it may help you quickly find the problem. The following code can help you do this for your reference only:

 
 
  1. import org.slf4j.Logger;   
  2.      
  3.   public class TraceUtil {   
  4.       final Logger logger;   
  5.       final long threshold = 1000;   
  6.       private long begin;   
  7.       private long offtime = 0;   
  8.       private String threadInfo;   
  9.       private String targetId;   
  10.      
  11.       public TraceUtil(Logger logger, Thread thread, String targetId, long begin) {   
  12.           this.logger = logger;   
  13.           this.threadInfo = thread.getId() + "-" + thread.toString();   
  14.           this.targetId = targetId;   
  15.           this.begin = begin;   
  16.       }   
  17.      
  18.       public void trace(String targetEvent) {   
  19.           long duration = System.currentTimeMillis() - begin;   
  20.           long increment = duration - offtime;   
  21.           offtime = duration;   
  22.           float percentage = (float) increment / (float) duration * 100;   
  23.           if (duration > threshold && percentage > 20) {   
  24.               logger.error(   
  25.                       "Response time is too large: [{}], {}/{} ({}), {}, {}",   
  26.                       new String[] { threadInfo + "", increment + "",   
  27.                               duration + "", percentage + "%", targetEvent,   
  28.                               targetId });   
  29.           }   
  30.      
  31.       }   
  32.      
  33.   }  

Use JVM MXBean to find the blocked point

When you find that the JVM occupies a high cpu and the response time is slow, it is very likely that it is dragged by slow devices such as IO or network. It may also be that a synchronization point synchronization method or object in your method has become a performance bottleneck. At this time, you can use the monitor API provided by JVM to monitor:

 
 
  1. <%@ page import="java.lang.management.*, java.util.*" %>   
  2.     <%!   
  3.         Map cpuTimes = new HashMap();   
  4.         Map cpuTimeFetch = new HashMap();   
  5.     %>   
  6.        
  7.     <%   
  8.     out.println("Threads Monitoring");   
  9.     long cpus = Runtime.getRuntime().availableProcessors();   
  10.     ThreadMXBean threads = ManagementFactory.getThreadMXBean();   
  11.     threads.setThreadContentionMonitoringEnabled(true);   
  12.     long now = System.currentTimeMillis();   
  13.     ThreadInfo[] t = threads.dumpAllThreads(false, false);   
  14.     for (int i = 0; i < t.length; i++) {   
  15.         long id = t[i].getThreadId();   
  16.         Long idObj = new Long(id);   
  17.         long current = 0;   
  18.         if (cpuTimes.get(idObj) != null) {   
  19.             long prev = ((Long) cpuTimes.get(idObj)).longValue();   
  20.             current = threads.getThreadCpuTime(t[i].getThreadId());   
  21.             long catchTime = ((Long) cpuTimeFetch.get(idObj)).longValue();   
  22.             double percent = (double)(current - prev) / (double)((now - catchTime) * cpus * 1000);   
  23.             if (percent > 0 && prev > 0) {  
  24.     out.println("<li>" + t[i].getThreadName()+"#"+t[i].getThreadId() + " Time: " + percent + " (" + prev + ", " + current + ") ");  
  25.     String locked = t[i].getLockInfo()==null?"":t[i].getLockInfo().getClassName();  
  26.     out.println(" Blocked: (" + t[i].getBlockedTime() + ", " + t[i].getBlockedCount() + ", " + locked + ")</li>");  
  27.     }  
  28.         }   
  29.         cpuTimes.put(idObj, new Long(current));     
  30.         cpuTimeFetch.put(idObj, new Long(now));   
  31.     }   
  32.     %> 

Synchronization is a major performance bottleneck

Through monitoring, it is found that a large number of thread blocks are in a synchronous method, so that the cpu is not busy. When you find that performance is not good, IO, network, and other slow devices are not a problem, you have to check whether synchronizae is used at a critical point ). Sometimes some method in the third-party jar of your application is synchronized. In this case, it is difficult to find the problem. You can only check whether the referenced method is synchronized when writing code.

Read more

1. Java run-time monitoring series articles, which systematically explain the jvm monitoring

2. Performance Tuning the JVM for Running Tomcat: This article lists several key jvm parameters related to tomcat Performance.

3. A book explaining Java Performance systematically: Java Performance

4. insideApps: A transaction-level open-source Java EE performance monitoring software. It is expected to be stored in the product environment and monitor and analyze the product performance without affecting the system performance. Good idea.

5. ManageEngine, a powerful monitoring software, has a free version.

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.