首页 文章

如何使用Apache Jmeter循环excel两次?

提问于
浏览
1

使用Apache Jmeter,我想遍历excel twice 中的所有行 . 这不能使用普通的CSV Data Set Config来完成,因为您可以在EOF上回收或停止,但不能定义通过文件的迭代次数 .

这是我试过的:

  • 登录

  • 使用CSV数据集配置中的数据搜索文档

  • 退出

1 回答

  • 1

    有多种方法可以做到这一点,请使用建议的设计作为参考:

    org.apache.jmeter.services.FileServer.getFileServer().closeFile("file.csv");
    vars.put("varFromCsv", "");
    
    • While Controller,条件: ${__javaScript("${varFromCsv}" != "<EOF>",)}

    • CSV数据集配置(文件名: file.csv ,变量名称: varFromCsv ,一切设置为 False ,共享模式: All threads

    • If Controller,条件 "${varFromCsv}" != "<EOF>"

    • 你的测试逻辑就在这里

    演示:

    JMeter CSV Loop Twice

    示例测试计划以防万一:

    <?xml version="1.0" encoding="UTF-8"?>
    <jmeterTestPlan version="1.2" properties="3.1" jmeter="3.1 r1770033">
      <hashTree>
        <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
          <stringProp name="TestPlan.comments"></stringProp>
          <boolProp name="TestPlan.functional_mode">false</boolProp>
          <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
          <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
            <collectionProp name="Arguments.arguments"/>
          </elementProp>
          <stringProp name="TestPlan.user_define_classpath"></stringProp>
        </TestPlan>
        <hashTree>
          <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
            <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
            <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
              <boolProp name="LoopController.continue_forever">false</boolProp>
              <stringProp name="LoopController.loops">2</stringProp>
            </elementProp>
            <stringProp name="ThreadGroup.num_threads">1</stringProp>
            <stringProp name="ThreadGroup.ramp_time">1</stringProp>
            <longProp name="ThreadGroup.start_time">1487666779000</longProp>
            <longProp name="ThreadGroup.end_time">1487666779000</longProp>
            <boolProp name="ThreadGroup.scheduler">false</boolProp>
            <stringProp name="ThreadGroup.duration"></stringProp>
            <stringProp name="ThreadGroup.delay"></stringProp>
          </ThreadGroup>
          <hashTree>
            <BeanShellSampler guiclass="BeanShellSamplerGui" testclass="BeanShellSampler" testname="Beanshell Sampler" enabled="true">
              <stringProp name="BeanShellSampler.query">org.apache.jmeter.services.FileServer.getFileServer().closeFile(&quot;file.csv&quot;);
    vars.put(&quot;varFromCsv&quot;, &quot;&quot;);</stringProp>
              <stringProp name="BeanShellSampler.filename"></stringProp>
              <stringProp name="BeanShellSampler.parameters"></stringProp>
              <boolProp name="BeanShellSampler.resetInterpreter">false</boolProp>
            </BeanShellSampler>
            <hashTree/>
            <WhileController guiclass="WhileControllerGui" testclass="WhileController" testname="While Controller" enabled="true">
              <stringProp name="WhileController.condition">${__javaScript(&quot;${varFromCsv}&quot; != &quot;&lt;EOF&gt;&quot;,)}</stringProp>
            </WhileController>
            <hashTree>
              <CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
                <stringProp name="delimiter">,</stringProp>
                <stringProp name="fileEncoding"></stringProp>
                <stringProp name="filename">file.csv</stringProp>
                <boolProp name="quotedData">false</boolProp>
                <boolProp name="recycle">false</boolProp>
                <stringProp name="shareMode">shareMode.all</stringProp>
                <boolProp name="stopThread">false</boolProp>
                <stringProp name="variableNames">varFromCsv</stringProp>
              </CSVDataSet>
              <hashTree/>
              <IfController guiclass="IfControllerPanel" testclass="IfController" testname="If Controller" enabled="true">
                <stringProp name="IfController.condition">&quot;${varFromCsv}&quot; != &quot;&lt;EOF&gt;&quot;</stringProp>
                <boolProp name="IfController.evaluateAll">false</boolProp>
              </IfController>
              <hashTree>
                <BeanShellSampler guiclass="BeanShellSamplerGui" testclass="BeanShellSampler" testname="${varFromCsv}" enabled="true">
                  <stringProp name="BeanShellSampler.query">Thread.sleep(100);</stringProp>
                  <stringProp name="BeanShellSampler.filename"></stringProp>
                  <stringProp name="BeanShellSampler.parameters"></stringProp>
                  <boolProp name="BeanShellSampler.resetInterpreter">false</boolProp>
                </BeanShellSampler>
                <hashTree/>
              </hashTree>
            </hashTree>
            <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" 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>
                  <threadCounts>true</threadCounts>
                  <idleTime>true</idleTime>
                  <connectTime>true</connectTime>
                </value>
              </objProp>
              <stringProp name="filename"></stringProp>
            </ResultCollector>
            <hashTree/>
          </hashTree>
        </hashTree>
      </hashTree>
    </jmeterTestPlan>
    

    参考资料:Using the While Controller in JMeter

相关问题