1. 程式人生 > 其它 >Java系列 | Linux系統中執行JMeter指令碼

Java系列 | Linux系統中執行JMeter指令碼

需求場景

在Windows環境下測試,併發數過大,整個主機直接崩掉

依賴說明

在Linux系統中執行JMeter指令碼,需要有兩個前提

1、Linux系統中配置好Java環境

2、Linux系統中配置好JMeter環境。(因為JMeter的執行需要Java環境)

下載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