在user.csv上循环,然后在jmeter中的data.csv上循环

时间:2016-02-12 19:20:05

标签: loops csv jmeter

我需要做这样的事情:

  

    foreach user in a users.csv:
        foreach data in data.csv:
            send http request
    

我看了this postthis page

然而,到目前为止,我已经弄清楚了,它不是并行执行的。因此,模拟例如在10分钟内用户逐步加载数据是不可能的。

因此,我希望每个用户都是一个线程,然后每个用户执行一个HTTP请求,因为data.csv文件中有很多数据。通过这种方式,我发现在几台jmeter服务器上执行这些测试最简单。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

示例测试计划

  • Thread Group - 根据您的方案的用户数和迭代次数。如果您没有考虑具体的循环计数 - 请勾选Forever
    • CSV Data Set Config
      • 文件名:users.csv
      • 变量名称:user
      • 所有下拉菜单:False
      • 分享模式:All Threads
    • While Controller:条件${__javaScript("${data}" != "<EOF>",)}
      • CSV数据集配置
        • 文件名:data.csv
        • 变量名称:data
        • 允许引用数据&amp;在EOF上回收:False
        • 在EOF上停止线程:True
        • 分享模式:Current Thread
      • HTTP Request使用${user}${data}变量

演示:

2 CSV Files Demo

如果您想要示例测试计划:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
  <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>
          <intProp name="LoopController.loops">-1</intProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">3</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <longProp name="ThreadGroup.start_time">1455338605000</longProp>
        <longProp name="ThreadGroup.end_time">1455338605000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
          <stringProp name="delimiter">,</stringProp>
          <stringProp name="fileEncoding"></stringProp>
          <stringProp name="filename">users.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">user</stringProp>
        </CSVDataSet>
        <hashTree/>
        <WhileController guiclass="WhileControllerGui" testclass="WhileController" testname="While Controller" enabled="true">
          <stringProp name="WhileController.condition">${__javaScript(&quot;${data}&quot; != &quot;&lt;EOF&gt;&quot;,)}</stringProp>
        </WhileController>
        <hashTree>
          <CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
            <stringProp name="filename">data.csv</stringProp>
            <stringProp name="fileEncoding"></stringProp>
            <stringProp name="variableNames">data</stringProp>
            <stringProp name="delimiter">,</stringProp>
            <boolProp name="quotedData">false</boolProp>
            <boolProp name="recycle">false</boolProp>
            <boolProp name="stopThread">true</boolProp>
            <stringProp name="shareMode">shareMode.thread</stringProp>
          </CSVDataSet>
          <hashTree/>
          <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="${user} - ${data}" enabled="true">
            <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
              <collectionProp name="Arguments.arguments"/>
            </elementProp>
            <stringProp name="HTTPSampler.domain">example.com</stringProp>
            <stringProp name="HTTPSampler.port"></stringProp>
            <stringProp name="HTTPSampler.connect_timeout"></stringProp>
            <stringProp name="HTTPSampler.response_timeout"></stringProp>
            <stringProp name="HTTPSampler.protocol"></stringProp>
            <stringProp name="HTTPSampler.contentEncoding"></stringProp>
            <stringProp name="HTTPSampler.path"></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>
            <boolProp name="HTTPSampler.monitor">false</boolProp>
            <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          </HTTPSamplerProxy>
          <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>false</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <threadCounts>true</threadCounts>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

您可能还希望根据测试场景调整配置,我只概述了基本用法,在这种情况下,请查看Using CSV DATA SET CONFIG指南,了解所有解释的CSV数据集配置选项。

答案 1 :(得分:1)

That solution is almost there, all you need is to change 2 things:

  1. Change number of threads from 1 to number of lines in users.csv. Make sure that CSV Data Set Config for users.csv has "Sharing mode" set to "All threads". This way each thread will read one line from users.csv, and since number of lines == number of threads, each user will be unique. But all threads will run on parallel.
  2. Change CSV Data Set Config for data.csv to have "Sharing mode" set to "Current thread". Also make sure that "Loop Controller" loops as many times as the number of lines in data.csv. That way each thread loops through entire data.csv.