[Business Functions Chapter 86] Microservices-springcloud-system performance stress test-jmeter-performance optimization-JVM parameter tuning

System performance stress testing

1. Stress test

  Stress testing is to continuously put pressure on the software, force it to run under extreme conditions, and observe to what extent it can run, so as to discover performance defects. It builds a test environment similar to the actual environment and tests the program at the same time. Or within a certain period of time, send an expected number of transaction requests to the system, test the efficiency of the system under different pressure conditions, and the pressure conditions that the system can withstand. Then conduct targeted testing and analysis to find bottlenecks that affect system performance, evaluate the efficiency of the system in the actual use environment, evaluate system performance, and determine whether the application system needs to be optimized or structurally adjusted. And optimize system resources.

  In the stress test we will involve some related performance indicators:

  1. Response Time (RT): The total time from the client sending the request to getting the response result from the server
  2. HPS (Hits Per Second): Number of clicks per second
  3. TPS (Transaction Per Second): The number of transactions processed by the system per second, also called the number of sessions
  4. QPS (Query Per Second): The number of queries the system processes per second

  In Internet enterprises, if a business has one and only one request connection, then TPS=QPS=HPS. In general, TPS is used to measure the entire business process, QPS is used to measure the number of interface queries, and HPS is used to measure it. The server clicks the request.

  When we test, we will measure the system through the data of these indicators (HPS, TPS, QPS). The higher the indicator, the better the system performance. In general, the range of indicators in various industries has relatively large differences. The following is a brief list for reference only.

  • Financial industry: 1000TPS~50000TPS
  • Insurance industry: 100TPS~100000TPS
  • Manufacturing: 10TPS~5000TPS
  • Large Internet websites: 10000TPS~1000000TPS
  • Internet others: 1000TPS~50000TPS

  Of course we will also involve some other nouns, as follows:

noun illustrate
Maximum response time The maximum time between the user making a request and the system responding
Minimum response time The minimum time between the user making a request and the system responding
90% response time Refers to the response time of all users sorted, the 90th percentile response time

  When we look at it from the outside, performance testing mainly focuses on these three performance indicators.

index illustrate
Throughput The number of requests and tasks that the system can handle per second
Response time The time it takes for a service to process a request or a task
Error rate The proportion of requests in a batch that result in errors

2. JMeter

1. Install JMeter

Official website address: https://jmeter.apache.org/download_jmeter.cgi After downloading, unzip it, then go to the bin directory and double-click the JMeter.bat file to start

image.png

The tool supports Chinese

image.png

page after Chinese

image.png

2. Basic operation of JMeter

2.1 Add thread group

  The function of the thread group is to define the relevant attributes of the task, such as how many threads are executed per second and how many times the operation is repeated.

image.png

2.2 Sampler

  After defining the thread group, we must continue to define the operating behavior of each thread, that is, create a corresponding sampler. In the sampler, we define the protocol and address information of the service to be accessed.

image.png

  Then we need to define the service information in the sampler

image.png

2.3 Monitor

  In the sampler, we define the service information to be accessed, and then we need to consider the relevant indicator information of the task that we need to obtain after the request. This is where the monitor is used.

image.png

The corresponding result data includes viewing the result tree summary report, aggregation report, and viewing the corresponding graphical summary chart...

image.png

2.4 Testing Baidu

  After writing the sampler, start testing.

image.png

After starting, we can query the test result data

image.png

image.png

image.png

image.png

2.5 Test mall homepage

image.png

image.png

Check the corresponding results after starting

image.png

image.png

image.png

image.png

2.6 JMeter Address occupation problem

image.png

After searching, I found that the registry key MaxUserPort needs to be added to regedit, and TcpTimedWaitDelay can be solved by restarting it.

Solution:

Open the registry: ctrl+r and enter regedit
to enter the registry. The path is: \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters.
Create a new DWORD value and set it (in decimal) to 30 seconds. Name: TcpTimedWaitDe, value: 30
Create a new DWORD value, (decimal) the maximum number of connections is 65534. Name: MaxUserPort, value: 65534

Restart after the modification is completed to take effect

image.png

3. Performance optimization

1. Consider factors affecting service performance

Database, application, middleware (Tomcat, Nginx), network and operating system, etc.

We also have to consider that the current service belongs to

  • CPU-intensive: Computing affects performance -> add CPU, add machine
  • IO intensive: network IO, disk IO, database read and write IO, Redis read and write IO --> cache, add solid state drive, add network card

2.JVM review

Setting JVM parameters and appropriately adjusting the memory size of the corresponding heap space is also a key action to improve efficiency. In the local environment idea, configure the startup environment, and the VM Options parameters can be -Xmx512m -Xmn512m. To run the production environment, then configure the jvm parameters
, Via java -Xmx512 -Xmn512m xxx.jar

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-s4RvI9f0-1693209241788)(./images/1693************** **208858173_image.png)]
JVM memory structure

image.png

Storage and GC of objects in JVM

image.png

image.png

1. The space involved in the JVM: Heap: including the young generation and the old generation + string constant pool. The young generation consists of one Eden and two Survivor areas. Method area: The persistent generation and metaspace are both implementations of the method area, and JDK1.8 changed it to metaspace.
2. JVM parameter settings, server configuration parameters:

  • -Xms: Initial heap memory size, set the memory size occupied when the program starts, the default physical memory is 1/64 -Xms = -XX:InitialHeapSiz
  • -Xmx: Maximum heap memory, sets the maximum memory size that can be occupied during program running. If the program needs to take up more memory than this setting value, an OutOfMemory exception will be thrown, the default physical memory is 1/4, -Xmx = -XX:MaxHeapSize. The size of -Xms and -Xmx settings in the above picture are the same 6000M
  • -Xss: Set the stack size of a single thread, generally defaults to 512~1024kb. The size of a single thread stack is related to the operating system and JDK version, -Xss = -XX:ThreadStackSize
  • -Xmn: Set the young generation size. The entire heap size = young generation size + old generation size + constant pool. The persistent generation generally has a fixed size of 64m, so increasing the young generation will reduce the size of the old generation. This value has a greater impact on system performance. Sun officially recommends configuring it to 3/8 of the entire heap.
  • -XX:MetaspaceSize: The size of the metaspace. The nature of the metaspace is similar to the permanent generation. They are both implementations of the method area in the JVM specification. However, the biggest difference between the metaspace and the permanent generation is that the metaspace is not in the virtual machine, but uses local memory and is controlled by the operating system. Therefore, the metaspace size is limited only by local memory.
  • -XX:+PrintGCDetails: Print GC detailed log information
  • -XX:SurvivorRatio: Survivor ratio setting, set the size ratio of Eden area and Survivor area in the young generation. Set to 8, the ratio of two Survivor areas to one Eden area is 2:8, and one Survivor area accounts for 1/10 of the entire young generation.
  • -XX:NewRatio: The ratio of the new generation ratio setting (including Eden and two Survivor areas) to the old generation (excluding the persistent generation). If set to 1, the ratio between the young generation and the old generation is 1:1, and the young generation accounts for 1/2 of the entire stack.
  • -XX:MaxTenuringThreshold: Enter the old generation threshold setting
  • -XX:PermSize=128m: The initial value of the persistent generation memory is allocated 128M; -XX:MaxPermSize=512m: Set the maximum permanent generation value to 512m

3.jconsole and jvisualvm

  jconsole and jvisualvm are JDK's own monitoring tools. It can help us better view the relevant monitoring information of the service, and the jvisualvm function will be more powerful.

3.1 jconsole

image.png

image.png

Find the corresponding process

image.png

3.2 jvisualvm

Because it comes with jdk6.0, we can also find it in cmd or search box.

image.png

open main page

image.png

Find the corresponding process, double-click to enter

image.png

View the corresponding monitoring information

image.png

Add plugins. If the plugin is not available then it needs to be updated

image.png

https://visualvm.github.io/pluginscenters.html You need to select the corresponding plug-in version based on your jdk version.

image.png

After installation, restart jvisualvm

%image.png

4. Performance of middleware

  The following is a complete request link

image.png

  Then let’s test the performance of related components

  • For Nginx testing, you can directly use the default HTML page, place an index.html file in the nginx configuration file, and the browser accesses the default address of nginx for stress testing. The port is 80 by default.

  • Preliminarily obtained a situation after the test, throughput and other quantitative indicators

  • Then we can open the docker stats command for the status of the container in docke, and then perform the pressure test again to observe the performance changes of the Nginx service in real time and judge whether the CPU-intensive type is IO-intensive. Through the pressure measurement process, we can see that the CPU soars to full, and we can judge whether it is CPU intensive can be optimized by adding more machines or CPUs

  • In fact, it is understandable because Nginx does not do business processing, but mainly distributes more requests and creates more sub-threads for maintenance, so it requires more CPU support.

  • Gateway test: access the server address and the corresponding port for direct access. No specific interface address is used here. Just like Nginx, the request is a direct method address:port, which can achieve the test effect.

  • Separate test service: Here you can write a simple request interface to directly return a string without going through the database data, which is a separate test performance for the application service.

  • Gateway + service: Just configure the request through the gateway and forward it. The request is the port of the gateway service, and then it will be routed to the specific service to respond to the request.

  • Full data on the home page: This involves access to some static resources, so you need to enable the loading and access to html resources in the advanced settings in the jmeter tool, so that you can access the pictures, html, css, js, etc. of those static resources on the home page

Stress test content Number of threads for stress testing Throughput/s 90% response time 99% response time
Nginx 50 7,385 10 70
Gateway 50 23,170 3 14
Test services individually 50 23,160 3 7
Gateway+Service 50 8,461 12 46
Nginx+Gateway 50
Nginx+Gateway+service 50 2,816 27 42
A menu 50 1,321 48 74
Three-level classification pressure test 50 12 4000 4000
Full data on the home page 50 2

image.png

The more middleware there is, the greater the performance loss. Most of the loss is in the interaction of data.

Simple optimization:

Middleware: Individual efficiency is very high. The more middleware is connected in series, the greater the impact, but it is actually relatively weak in front of the business.

business:

  • DB (MySQL, optimized)
  • Template page rendering
Stress test content Number of threads for stress testing Throughput/s 90% response time 99% response time
Nginx 50 7,385 10 70
Gateway 50 23,170 3 14
Test services individually 50 23,160 3 7
Gateway+Service 50 8,461 12 46
Nginx+Gateway 50
Nginx+Gateway+service 50 2,816 27 42
A menu 50 1,321 48 74
Three-level classification pressure test 50 12 4000 4000
Home page full data (DB-Themleaf) 50 2
First-level menu (DB- index ) 50 1900 40 70
Three-level classification stress test ( index ) 50 34 1599 1700
Full data on home page ( DB-Themleaf-release cache ) 50 30 。。。 。。。

5.Nginx realizes dynamic and static separation

  Through the above stress test, we can find that if the backend service handles both dynamic requests and static requests, its throughput is very limited. At this time, we can store static resources in Nginx.

image.png

5.1 Static resource storage

  Upload the static resources in the service to the Nginx service, package the static resource files into a zip package, then drag and drop it to Linux, and then we pass

unzip index.zip

to unzip

image.png

Then replace the resource access path in the template file

image.png

5.2 Nginx configuration

  Then we specify how to handle requests starting with static in the Nginx configuration file.

image.png

  After saving, restart the Nginx service and then you can access it.

image.png

6. Three-level classification optimization

  When we obtain three-level classification data, we will frequently operate the database. We can optimize this code.

image.png

  Here we can query all the classified data at one time, and then obtain the corresponding information from this data each time, so as to reduce the number of database operations and improve the performance of the service .

/**
     * 跟进父编号获取对应的子菜单信息
     * @param list
     * @param parentCid
     * @return
     */
    private List<CategoryEntity> queryByParenCid(List<CategoryEntity> list,Long parentCid){
    
    
        List<CategoryEntity> collect = list.stream().filter(item -> {
    
    
            return item.getParentCid().equals(parentCid);
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询出所有的二级和三级分类的数据
     * 并封装为Map<String, Catalog2VO>对象
     * @return
     */
    @Override
    public Map<String, List<Catalog2VO>> getCatelog2JSON() {
    
    
        // 获取所有的分类数据
        List<CategoryEntity> list = baseMapper.selectList(new QueryWrapper<CategoryEntity>());
        // 获取所有的一级分类的数据
        List<CategoryEntity> leve1Category = this.queryByParenCid(list,0l);
        // 把一级分类的数据转换为Map容器 key就是一级分类的编号, value就是一级分类对应的二级分类的数据
        Map<String, List<Catalog2VO>> map = leve1Category.stream().collect(Collectors.toMap(
                key -> key.getCatId().toString()
                , value -> {
    
    
                    // 根据一级分类的编号,查询出对应的二级分类的数据
                    List<CategoryEntity> l2Catalogs = this.queryByParenCid(list,value.getCatId());
                    List<Catalog2VO> Catalog2VOs =null;
                    if(l2Catalogs != null){
    
    
                        Catalog2VOs = l2Catalogs.stream().map(l2 -> {
    
    
                            // 需要把查询出来的二级分类的数据填充到对应的Catelog2VO中
                            Catalog2VO catalog2VO = new Catalog2VO(l2.getParentCid().toString(), null, l2.getCatId().toString(), l2.getName());
                            // 根据二级分类的数据找到对应的三级分类的信息
                            List<CategoryEntity> l3Catelogs = this.queryByParenCid(list,l2.getCatId());
                            if(l3Catelogs != null){
    
    
                                // 获取到的二级分类对应的三级分类的数据
                                List<Catalog2VO.Catalog3VO> catalog3VOS = l3Catelogs.stream().map(l3 -> {
    
    
                                    Catalog2VO.Catalog3VO catalog3VO = new Catalog2VO.Catalog3VO(l3.getParentCid().toString(), l3.getCatId().toString(), l3.getName());
                                    return catalog3VO;
                                }).collect(Collectors.toList());
                                // 三级分类关联二级分类
                                catalog2VO.setCatalog3List(catalog3VOS);
                            }
                            return catalog2VO;
                        }).collect(Collectors.toList());
                    }

                    return Catalog2VOs;
                }
        ));
        return map;
    }

Optimized stress test performance

Stress test content Number of threads for stress testing Throughput/s 90% response time 99% response time
Nginx 50 7,385 10 70
Gateway 50 23,170 3 14
Test services individually 50 23,160 3 7
Gateway+Service 50 8,461 12 46
Nginx+Gateway 50
Nginx+Gateway+service 50 2,816 27 42
A menu 50 1,321 48 74
Three-level classification pressure test 50 12 4000 4000
Three-level classification stress test (after business optimization) 50 448 113 227

image.png

It can be seen that the improvement of system performance is still very obvious.

Guess you like

Origin blog.csdn.net/studyday1/article/details/132540827