[转帖]Java系列 | Linux系统中运行JMeter脚本

java,系列,linux,系统,运行,jmeter,脚本 · 浏览次数 : 0

小编点评

归纳总结以上内容,生成内容时需要带简单的排版,包括以下内容: 1. **排版符号**:使用简单的排版符号,例如 *** **、****、***** 等,可以帮助生成内容时进行排版。 2. ****分组**:将多个内容*******分组起来,可以帮助生成内容时进行排版。 3. ****缩进**:缩进内容的名称,可以帮助生成内容时进行排版。 4. ****格式**:使用简单的格式,例如 ****、**、***** 等,可以帮助生成内容时进行排版。 5. ****标题**:标题可以帮助生成内容时进行排版,可以帮助生成内容时进行排版。 6. ****注释**:注释可以帮助生成内容时进行排版,可以帮助生成内容时进行排版。

正文

https://www.cnblogs.com/tinywan/p/16243391.html

 

 

需求场景

在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 <summary>
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 <summary>
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 <summary>
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"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.4.1">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="测试计划" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="线程组" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <intProp name="LoopController.loops">-1</intProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">28000</stringProp>
        <stringProp name="ThreadGroup.ramp_time">10</stringProp>
        <boolProp name="ThreadGroup.scheduler">true</boolProp>
        <stringProp name="ThreadGroup.duration">60</stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
      </ThreadGroup>
      <hashTree>
        <CriticalSectionController guiclass="CriticalSectionControllerGui" testclass="CriticalSectionController" testname="临界部分控制器" enabled="false">
          <stringProp name="CriticalSectionController.lockName">global_lock</stringProp>
        </CriticalSectionController>
        <hashTree/>
        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <sentBytes>true</sentBytes>
              <url>true</url>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
              <connectTime>true</connectTime>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="1-频道详情" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            <collectionProp name="Arguments.arguments">
              <elementProp name="user_id" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.value">${__Random(1000,8000,user_id)}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
                <boolProp name="HTTPArgument.use_equals">true</boolProp>
                <stringProp name="Argument.name">user_id</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="HTTPSampler.domain">172.30.237.66</stringProp>
          <stringProp name="HTTPSampler.port">8788</stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
          <stringProp name="HTTPSampler.path">/channel/watch/20220023/detail</stringProp>
          <stringProp name="HTTPSampler.method">GET</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree/>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="2-白名单验证" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            <collectionProp name="Arguments.arguments">
              <elementProp name="user_id" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.value">${__Random(1000,8000,user_id)}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
                <boolProp name="HTTPArgument.use_equals">true</boolProp>
                <stringProp name="Argument.name">user_id</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="HTTPSampler.domain">172.30.237.66</stringProp>
          <stringProp name="HTTPSampler.port">8788</stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
          <stringProp name="HTTPSampler.path">/channel/watch/20220023/validate-whitelist</stringProp>
          <stringProp name="HTTPSampler.method">GET</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree/>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="3-频道详情" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
            <collectionProp name="Arguments.arguments">
              <elementProp name="user_id" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.value">${__Random(1000,8000,user_id)}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
                <boolProp name="HTTPArgument.use_equals">true</boolProp>
                <stringProp name="Argument.name">user_id</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="HTTPSampler.domain">172.30.237.66</stringProp>
          <stringProp name="HTTPSampler.port">8788</stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
          <stringProp name="HTTPSampler.path">/channel/watch/20220023/detail</stringProp>
          <stringProp name="HTTPSampler.method">GET</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree/>
        <ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="聚合报告" enabled="true">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <sentBytes>true</sentBytes>
              <url>true</url>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
              <connectTime>true</connectTime>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>
复制代码

 

Help

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

与[转帖]Java系列 | Linux系统中运行JMeter脚本相似的内容:

[转帖]Java系列 | Linux系统中运行JMeter脚本

https://www.cnblogs.com/tinywan/p/16243391.html 需求场景 在Windows环境下测试,并发数过大,整个主机直接崩掉 依赖说明 在Linux系统中运行JMeter脚本,需要有两个前提 1、Linux系统中配置好Java环境 2、Linux系统中配置好JM

[转帖]Java调优系列之工具篇之btrace、gperftools

https://github.com/landon30/Bulls/wiki/java-profiling-tools landon 网络游戏资深服务器架构师 2018-06-14 线上遇到了问题? 服务上线出问题,想增加打印日志怎么办? 线上怀疑某个接口慢,想打印接口耗时怎么办? 线上某个接口报错

[转帖]Java命令学习系列(二)——Jstack

2015-04-18 分类:Java 阅读(42792) 评论(3) GitHub 24k Star 的Java工程师成神之路,不来了解一下吗! jstack是java虚拟机自带的一种堆栈跟踪工具。 功能 jstack用于生成java虚拟机当前时刻的线程快照。线程快照是当前java虚拟机内每一条线程

[转帖]如何优化Java垃圾回收机制简单讨论?

https://www.cnblogs.com/lizexiong/p/15594187.html 本文是成为Java GC专家系列文章的第三篇。在第一篇《成为JavaGC专家Part I — 深入浅出Java垃圾回收机制》中我们学习了不同GC算法的执行过程,GC是如何工作的,什么是新生代和老年代,

[转帖]JVM系列之:你知道Java有多少种内存溢出吗

本文为《深入学习 JVM 系列》第二十五篇文章 Java内存区域 关于这部分内容大多来源于《深入理解Java虚拟机》一书。 Java 运行时数据区域(JDK8)如下图所示: 关于上述提到的线程共享和线程隔离区域,下图做详细讲解: 程序计数器 程序计数器是一块较小的内存空间,可以看作是当前线程所执行的

[转帖]【JVM系列JKD8】参数参考表

JVM是Java底层核心要素,可以说Java生态的东西都是运行在JVM里面的。From:https://docs.oracle.com/javase/8/docs/ JVM参数的含义 JVM参数说明 参数名称含义默认值-Xms初始堆大小物理内存的1/64(<1GB)默认(MinHeapFreeRat

[转帖]JVM系列之:关于即时编译器的那些事

本文为《深入学习 JVM 系列》第十六篇文章 我们在前文学习 Java 是如何执行的这篇文章中有提及即时编译器,这是一项用来提升应用程序运行效率的技术。通常而言,代码会先被 Java 虚拟机解释执行,之后反复执行的热点代码则会被即时编译成为机器码,直接运行在底层硬件之上。 那么问题来了,既然在 Ho

[转帖]JVM 系列 - 内存区域

一、对象在JVM中的表示: OOP-Klass模型 https://www.jianshu.com/p/424a920771a3 写的很赞。 注意:OOP-Klass是hotspot的JVM实现原理,其他JVM的实现可能不一样。、 OOP表示java实例,Klass表示class。 Klass: 包

[转帖]5、kafka监控工具Kafka-Eagle介绍及使用

https://zhuanlan.zhihu.com/p/628039102 # Apache Kafka系列文章 1、kafka(2.12-3.0.0)介绍、部署及验证、基准测试 2、java调用kafka api 3、kafka重要概念介紹及示例 4、kafka分区、副本介绍及示例 5、kafk

[转帖]java -d 参数(系统属性) 和 环境变量

https://www.cnblogs.com/limeiyang/p/16565920.html 1. -d 参数说明 通过 java -h 查看可知: 注意:-D= : set a system property 设置系统属性。如果value是一个包含空格的字符串,则必须将该字符串括在双引号中。