Java系列 | Linux系统中运行JMeter脚本


需求场景

在Windows环境下测试,并发数过大,整个主机直接崩掉

依赖说明

在Linux系统中运行JMeter脚本,需要有两个前提

1、Linux系统中配置好Java环境

2、Linux系统中配置好JMeter环境。(因为JMeter的运行需要Java环境)

环境说明

Java版本

java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.20.04.1)
OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.20.04.1, mixed mode, sharing)

Linux操作系统

Ubuntu 20.04.4 LTS 

下载JMeter

JMeter下载地址:http://jmeter.apache.org/download_jmeter.cgi

 参数解释

  • -n:表示non gui mode,就是非图形化模式。
  • -t:指定JMX脚本路径,即test plan(测试计划)。后面跟需要运行的JMeter 脚本的路径和脚本名称。
  • -l:指定结果文件路径。后面跟输出结果文件路径和结果文件名称。-e:设置测试完成后生成测试报表。-e:设置测试完成后生成测试报表。-m:设置测试完成后生成测试报表。
    • 1)若没有指定指定结果文件路径,则自动创建,可以生成.csv文件或者.jtl文件。
    • 2)若只写脚本名称,则默认是在当前目录查找或创建。
  • -e:设置测试完成后生成测试报表。
  • -o:指定测试报表生成文件夹。文件夹必须为空或不存在。

开始测试

/root/apache-jmeter-5.4.1/bin/jmeter -n -t /root/33.jmx -l 33-result.jtl

输出

Creating summariser 
Created the tree successfully using /root/33.jmx
Starting standalone test @ Sat May 07 16:31:56 CST 2022 (1651912316484)
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary +  18271 in 00:00:03 = 5247.3/s Avg:   123 Min:     3 Max:  3383 Err:     0 (0.00%) Active: 3471 Started: 3471 Finished: 0
summary + 176369 in 00:00:30 = 5879.0/s Avg:   442 Min:     7 Max: 31083 Err:     0 (0.00%) Active: 5000 Started: 5000 Finished: 0
summary = 194640 in 00:00:33 = 5813.3/s Avg:   412 Min:     3 Max: 31083 Err:     0 (0.00%)
summary + 162025 in 00:00:28 = 5703.1/s Avg:  1316 Min:     3 Max: 58295 Err:     0 (0.00%) Active: 0 Started: 5000 Finished: 5000
summary = 356665 in 00:01:02 = 5762.7/s Avg:   822 Min:     3 Max: 58295 Err:     0 (0.00%)
Tidying up ...    @ Sat May 07 16:32:58 CST 2022 (1651912378410)
... end of run

异常处理

并发数超过10000,提示Java虚拟机内存异常

Creating summariser 
Created the tree successfully using /root/33.jmx
Starting standalone test @ Sat May 07 16:35:05 CST 2022 (1651912505615)
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary +      1 in 00:00:00 =    5.6/s Avg:    76 Min:    76 Max:    76 Err:     0 (0.00%) Active: 189 Started: 189 Finished: 0
summary +  57074 in 00:00:24 = 2357.4/s Avg:  1096 Min:     2 Max: 11532 Err:     0 (0.00%) Active: 14671 Started: 14671 Finished: 0
summary =  57075 in 00:00:24 = 2340.1/s Avg:  1096 Min:     2 Max: 11532 Err:     0 (0.00%)
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid152251.hprof ...
Heap dump file created [1399681687 bytes in 2.987 secs]

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "线程组 1-14668"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-9350"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-14684"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-7834"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-8421"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-8792"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-7965"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-14692"

Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-8261"
^C^C
Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "线程组 1-9190"

翻阅官方文档 https://jmeter.apache.org/usermanual/get-started.html

启动 JMeter 时使用的 JVM 内存设置。默认值

-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m

通过设置变量 JVM_ARGS 将覆盖默认预定义的设置

JVM_ARGS="-Xms1024m -Xmx1024m" jmeter -t test.jmx [etc.]

重新改写命令,设置内存为4G

JVM_ARGS="-Xms4024m -Xmx4024m" /root/apache-jmeter-5.4.1/bin/jmeter -n -t /root/33.jmx -l 33-result.jtl

再次运行结果(异常问题已解决)

# JVM_ARGS="-Xms4024m -Xmx4024m" /root/apache-jmeter-5.4.1/bin/jmeter -n -t /root/33.jmx -l 33-result.jtl 
Creating summariser 
Created the tree successfully using /root/33.jmx
Starting standalone test @ Sat May 07 16:51:19 CST 2022 (1651913479599)
Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
summary +  52703 in 00:00:10 = 5084.7/s Avg:   228 Min:     3 Max:  9561 Err:     0 (0.00%) Active: 25654 Started: 25654 Finished: 0
summary + 169987 in 00:00:30 = 5666.2/s Avg:   549 Min:     6 Max: 39292 Err:     0 (0.00%) Active: 28000 Started: 28000 Finished: 0
summary = 222690 in 00:00:40 = 5516.9/s Avg:   473 Min:     3 Max: 39292 Err:     0 (0.00%)
summary + 165345 in 00:00:30 = 5451.9/s Avg:  9050 Min:     3 Max: 61988 Err:     0 (0.00%) Active: 3626 Started: 28000 Finished: 24374
summary = 388035 in 00:01:11 = 5489.0/s Avg:  4127 Min:     3 Max: 61988 Err:     0 (0.00%)
summary +   8713 in 00:00:01 = 7543.7/s Avg: 10948 Min:     3 Max: 60595 Err:     0 (0.00%) Active: 0 Started: 28000 Finished: 28000
summary = 396748 in 00:01:12 = 5522.0/s Avg:  4277 Min:     3 Max: 61988 Err:     0 (0.00%)
Tidying up ...    @ Sat May 07 16:52:31 CST 2022 (1651913551484)
... end of run

附 33.jmx 测试脚本配置

<?xml version="1.0" encoding="UTF-8"?>
"1.2" properties="5.0" jmeter="5.4.1">
  
    "TestPlanGui" testclass="TestPlan" testname="测试计划" enabled="true">
      "TestPlan.comments">
      "TestPlan.functional_mode">false
      "TestPlan.tearDown_on_shutdown">true
      "TestPlan.serialize_threadgroups">false
      "TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
        "Arguments.arguments"/>
      
      "TestPlan.user_define_classpath">
    
    
      "ThreadGroupGui" testclass="ThreadGroup" testname="线程组" enabled="true">
        "ThreadGroup.on_sample_error">continue
        "ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
          "LoopController.continue_forever">false
          "LoopController.loops">-1
        
        "ThreadGroup.num_threads">28000
        "ThreadGroup.ramp_time">10
        "ThreadGroup.scheduler">true
        "ThreadGroup.duration">60
        "ThreadGroup.delay">
        "ThreadGroup.same_user_on_next_iteration">true
      
      
        "CriticalSectionControllerGui" testclass="CriticalSectionController" testname="临界部分控制器" enabled="false">
          "CriticalSectionController.lockName">global_lock
        
        
        "ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
          "ResultCollector.error_logging">false
          
            saveConfig
            class="SampleSaveConfiguration">
              
              true
              true
              true
              
              true
              true
              true
              true
              false
              true
              true
              false
              false
              false
              true
              false
              false
              false
              true
              0
              true
              true
              true
              true
              true
              true
            
          
          "filename">
        
        
        "HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="1-频道详情" enabled="true">
          "HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            "Arguments.arguments">
              "user_id" elementType="HTTPArgument">
                "HTTPArgument.always_encode">false
                "Argument.value">${__Random(1000,8000,user_id)}
                "Argument.metadata">=
                "HTTPArgument.use_equals">true
                "Argument.name">user_id
              
            
          
          "HTTPSampler.domain">172.30.237.66
          "HTTPSampler.port">8788
          "HTTPSampler.protocol">http
          "HTTPSampler.contentEncoding">
          "HTTPSampler.path">/channel/watch/20220023/detail
          "HTTPSampler.method">GET
          "HTTPSampler.follow_redirects">true
          "HTTPSampler.auto_redirects">false
          "HTTPSampler.use_keepalive">true
          "HTTPSampler.DO_MULTIPART_POST">false
          "HTTPSampler.embedded_url_re">
          "HTTPSampler.connect_timeout">
          "HTTPSampler.response_timeout">
        
        
        "HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="2-白名单验证" enabled="true">
          "HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            "Arguments.arguments">
              "user_id" elementType="HTTPArgument">
                "HTTPArgument.always_encode">false
                "Argument.value">${__Random(1000,8000,user_id)}
                "Argument.metadata">=
                "HTTPArgument.use_equals">true
                "Argument.name">user_id
              
            
          
          "HTTPSampler.domain">172.30.237.66
          "HTTPSampler.port">8788
          "HTTPSampler.protocol">http
          "HTTPSampler.contentEncoding">
          "HTTPSampler.path">/channel/watch/20220023/validate-whitelist
          "HTTPSampler.method">GET
          "HTTPSampler.follow_redirects">true
          "HTTPSampler.auto_redirects">false
          "HTTPSampler.use_keepalive">true
          "HTTPSampler.DO_MULTIPART_POST">false
          "HTTPSampler.embedded_url_re">
          "HTTPSampler.connect_timeout">
          "HTTPSampler.response_timeout">
        
        
        "HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="3-频道详情" enabled="true">
          "HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            "Arguments.arguments">
              "user_id" elementType="HTTPArgument">
                "HTTPArgument.always_encode">false
                "Argument.value">${__Random(1000,8000,user_id)}
                "Argument.metadata">=
                "HTTPArgument.use_equals">true
                "Argument.name">user_id
              
            
          
          "HTTPSampler.domain">172.30.237.66
          "HTTPSampler.port">8788
          "HTTPSampler.protocol">http
          "HTTPSampler.contentEncoding">
          "HTTPSampler.path">/channel/watch/20220023/detail
          "HTTPSampler.method">GET
          "HTTPSampler.follow_redirects">true
          "HTTPSampler.auto_redirects">false
          "HTTPSampler.use_keepalive">true
          "HTTPSampler.DO_MULTIPART_POST">false
          "HTTPSampler.embedded_url_re">
          "HTTPSampler.connect_timeout">
          "HTTPSampler.response_timeout">
        
        
        "StatVisualizer" testclass="ResultCollector" testname="聚合报告" enabled="true">
          "ResultCollector.error_logging">false
          
            saveConfig
            class="SampleSaveConfiguration">
              
              true
              true
              true
              
              true
              true
              true
              true
              false
              true
              true
              false
              false
              false
              true
              false
              false
              false
              true
              0
              true
              true
              true
              true
              true
              true
            
          
          "filename">
        
        
      
    
  

 

Help

https://www.cnblogs.com/liuyuelinfighting/p/15519237.html