loop on user.csv and then loop on data.csv in jmeter

半城伤御伤魂 提交于 2021-01-28 18:16:14

问题


I need to do something like this:

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

I've took a look on this post and on this page

However, as far I've been to figure out, it's NOT performed in parallel. So, it's not posible to simulate for example that during 10 minutes the users are loading data progressively.

So, I'd like each user is a thread and then each user perform an HTTP request as many data is in data.csv file. By this way, I figure out it's easiest to perform these kind of tests on several jmeter servers.

Any ideas?


回答1:


Example Test Plan

  • Thread Group - Number of users and iterations as per your scenario. If you don't have specific loop count in mind - tick Forever
    • CSV Data Set Config
      • Filename: users.csv
      • Variable Names: user
      • All dropdowns: False
      • Sharing Mode: All Threads
    • While Controller: Condition ${__javaScript("${data}" != "<EOF>",)}
      • CSV Data Set Config
        • Filename: data.csv
        • Variable Names: data
        • Allow Quoted Data & Recycle on EOF: False
        • Stop Thread on EOF: True
        • Sharing Mode: Current Thread
      • HTTP Request which uses ${user} and ${data} variables

Demo:

If you want example test plan:

<?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>

You may also wish to adjust config according to your test scenario, I outlined only basic usage, in that case check out Using CSV DATA SET CONFIG guide for all CSV Data Set configuration options explained.




回答2:


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.


来源:https://stackoverflow.com/questions/35370585/loop-on-user-csv-and-then-loop-on-data-csv-in-jmeter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!