Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to make performance tuning Strategy

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use Java to make performance tuning strategy". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Test-analyze-tune performance testing strategy

Performance testing is a necessary measure to find the performance bottleneck in advance and ensure the stability of the system performance.

1. Microbenchmark performance test

Microbenchmark performance testing can accurately locate the performance problems of a module or method, which is especially suitable for comparing the performance of a functional module or method in different implementation ways. For example, compare the performance of a method using synchronous and asynchronous implementations.

two。 Macro quasi-performance test

Macro-based quasi-performance testing is a comprehensive test, which needs to take into account the test environment, test scenarios and test goals.

First of all, look at the test environment, we need to simulate the real environment online.

Then watch the test scenario. We need to determine whether there are other business interfaces running in parallel when testing an interface, causing interference. If so, please pay attention, because once you ignore this kind of interference, the test results will be biased.

Finally, look at the test target. Our performance testing should be targeted, and whether the system is up to standard can be measured by throughput and response time. If it does not reach the standard, it will be optimized; if it reaches the standard, it will continue to increase the number of concurrency of the test and probe into the TPS (maximum transactions per second) of the interface. By doing so, you can gain an in-depth understanding of the performance of the interface. In addition to testing the throughput and response time of the interface, we also need to cycle through the interfaces that can cause performance problems, and observe the changes in CPU, memory, and Imax O usage of each server.

The above is a detailed explanation of the two testing methods. It is worth noting that there are interference factors in the performance test, which will make the test results inaccurate. Therefore, we also need to pay attention to some problems when doing performance testing.

1. Warm-up problem

When we do performance tests, our system runs faster and faster, and the subsequent access speed is several times faster than our first visit. What's going on?

In the Java programming language and environment, after the. Java file is compiled into a .class file, the machine still cannot run the bytecode in the .class file directly, and the bytecode needs to be converted into local machine code by an interpreter. In order to save memory and execution efficiency, the interpreter is the first to execute the code when it is first executed.

With the increase in the number of times the code is executed, when the virtual machine finds that a method or block of code is running particularly frequently, it will identify the code as hot code. In order to improve the execution efficiency of the hot code, at run time, the virtual machine will compile the code into the machine code related to the local platform through the real-time compiler, optimize it at all levels, and then store it in memory. after that, every time the code is run, it can be obtained directly from the memory summary.

So at the beginning of the run, the virtual machine takes a long time to fully optimize the code, and then it can be executed at maximum performance.

This is the warm-up process. If the warm-up time is too long during the performance test, the speed of the first visit will be too slow. You can consider optimizing before testing.

2. The performance test results are unstable.

When we do the performance test, we find that the data set processed by each test is the same, but the test results are different. This is because testing is accompanied by a lot of instability because of the impact of other processes on the machine, network fluctuations, and differences in JVM garbage collection at each stage, and so on.

We can average the test results through many tests, or count a graph, as long as we ensure that our average is within a reasonable range and does not fluctuate very much, in this case, the performance test is passed.

3. Influence in the case of multi-JVM

If our server has multiple Java application services deployed under different Tomcat, this means that our server will have multiple JVM. Any JVM has the right to use the resources of the whole system. If only a single JVM is deployed on a machine, the performance test results are good, or your tuning results are good, but not necessarily in the case of multiple JVM on a single machine. Therefore, we should try to avoid the deployment of multiple JVM on one machine in an online environment.

Reasonable analysis of the results and formulation of tuning strategy

After we finish the performance test, we need to output a performance test report to help us analyze the performance test of the system. The test results need to include the average, maximum and minimum throughput of the test interface, response time, server CPU, memory, Imax O, network IO utilization, GC frequency of JVM, and so on.

By observing these tuning criteria, we can find performance bottlenecks, and then we analyze and find problems in a bottom-up manner. First of all, from the operating system level, check whether there are anomalies in the CPU, memory, Imax O and network utilization of the system, then find the exception log through the command, and finally find the cause of the bottleneck by analyzing the log; you can also check whether there are anomalies in the garbage collection frequency and memory allocation of JVM from the JVM level of Java application, analyze the log and find the cause of the bottleneck.

If there are no anomalies at the system and JVM level, we can check whether there are performance bottlenecks in the application service business layer, such as Java programming problems, read and write data bottlenecks, and so on.

Analyzing and finding a problem is a complex and meticulous process, and a performance problem may be caused by one reason or a combination of several reasons. We can use a bottom-up approach to analyze and find problems, while we can use a top-down approach to optimize the system performance problems. Let me introduce several tuning strategies from the application layer to the operating system layer.

1. Optimize the code

The problem code in the application layer is often exposed due to the depletion of system resources. For example, when a certain piece of code causes a memory overflow, it often uses up the memory in the JVM. At this time, the system's memory resources are exhausted. At the same time, it also causes frequent garbage collection in JVM, resulting in a high level of CPU above 100%. At this time, the system's CPU resources are consumed.

There are also some performance problems caused by non-problem code, which are often difficult to find and need to be optimized by our experience. For example, if we use the LinkedList collection that we often use to iterate through the container using for loops, it will greatly reduce the efficiency of reading, but this reduction in efficiency will hardly lead to abnormal system performance parameters.

At this time, experienced students will use Iterator (iterator) to iterate through the collection, this is because LinkedList is a linked list, if you use the for loop to get elements, each time the loop gets elements, it will traverse the List once, which will reduce the efficiency of reading.

2. Optimize the design

There are many object-oriented design patterns that can help us optimize the code design of the business layer and the middleware layer. After optimization, you can not only simplify the code, but also improve the overall performance. For example, the singleton pattern can share a creation object in a scene where the creation object is frequently called, which reduces the performance consumption caused by the frequent creation and destruction of the object.

3. Optimization algorithm

A good algorithm can help us greatly improve the performance of the system. For example, in different scenarios, using the appropriate search algorithm can reduce the time complexity.

4. Time for space

Sometimes the system does not have high requirements for query speed, but demanding storage space. At this time, we can consider using time for space.

For example, I will explain in detail in 03 that using the intern method of String objects, you can store datasets with high repetition rates in a constant pool and reuse the same object, which can greatly save memory storage space. But because the constant pool uses the HashMap data structure type, query performance degrades if we store too much data. Therefore, in this kind of scenario where the storage capacity is demanding, but the query speed is not required, we can consider using the time to exchange space.

5. Space for time

This approach is to use storage space to improve access speed. At present, many systems use MySQL database, and the more common sub-table and sub-database is a typical case of using space for time.

Because the read and write performance of a single MySQL table will significantly decline when storing tens of millions of data, at this time, we need to split the table data by a field Hash value or other means. When the system queries data, it will find the corresponding table according to the conditional Hash value, because the amount of table data is reduced, and the query performance is improved.

6. Parameter tuning

The above is the optimization of the business layer code, in addition, the optimization of JVM, Web container and operating system is also very critical.

According to your own business scenario, reasonably setting the memory space of JVM and garbage collection algorithm can improve system performance. For example, if we create a large number of large objects in our business, we can set them up and put them directly into the old days. This can reduce the frequent occurrence of small garbage collection (Minor GC) in the younger generation, reduce the time occupied by CPU, and improve system performance.

The setting of Web container thread pool and the unreasonable setting of kernel parameters of Linux operating system may also lead to system performance bottleneck. Optimizing these two parts according to your own business scenarios can improve system performance.

Backing strategy to ensure the stability of the system

First, current restriction, setting maximum access restrictions on the entry of the system. You can refer to the TPS of the bottoming interface in the performance test. At the same time, circuit breakers are taken and unsuccessful requests are returned amicably.

Second, realize intelligent horizontal expansion. Intelligent horizontal expansion can ensure that when the number of visitors exceeds a certain threshold, the system can automatically add services horizontally according to the demand.

Third, expand capacity ahead of time. This method is usually applied to high concurrency systems, such as instant snap-up business systems. This is because horizontal expansion cannot meet a large number of requests that occur in an instant, and even if it succeeds, the rush to buy is over.

Currently, many companies use Docker containers to deploy application services. This is because the Docker container uses Kubernetes as the container management system, while Kubernetes can achieve intelligent horizontal expansion and advance expansion of Docker services.

This is the end of "how to use Java to develop performance tuning strategies". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report