在Jmeter

时间:2015-10-06 14:28:31

标签: java jmeter

我必须使用JMeter创建负载测试。最简单的测试版本应该创建具有特定内容的HTTP POST请求。该网址应如下所示:

/myapp/SESSION-ID/SEQIENCE

其中 SESSION-ID是自定义UUID SEQUENCE - 序列号(1,2,3,...)进入会话。

我在JMeter中定义了HTTP请求,如下所示:

/myapp/${sessionid}/${__counter(true)}

我的问题是生成会话ID。我想为每个用户使用不同的会话,但对于特定用户的所有匹配应该是相同的。听起来很简单。

我将用户定义的变量添加到我的线程组:sessionid=${__UUID}。 这会为所有请求和线程生成全局UUID。我试图将用户定义的变量配置元素与HTTP请求采样器一起用简单的控制器包装,以减少变量可见性的范围,但它没有帮助。

我做错了什么?

这是我的测试计划。顶部的一些元素显示我尝试实现上述目标并被禁用。

<?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="Cligate1" enabled="true">
      <stringProp name="TestPlan.comments">My first attempt to run simple test again cligate</stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">true</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments">
          <elementProp name="host" elementType="Argument">
            <stringProp name="Argument.name">host</stringProp>
            <stringProp name="Argument.value">localhost</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
          <elementProp name="port" elementType="Argument">
            <stringProp name="Argument.name">port</stringProp>
            <stringProp name="Argument.value">8080</stringProp>
            <stringProp name="Argument.metadata">=</stringProp>
          </elementProp>
        </collectionProp>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thick client POST screenshot thread pool" 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">10</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <longProp name="ThreadGroup.start_time">1443949496000</longProp>
        <longProp name="ThreadGroup.end_time">1443949496000</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="SingleSessionId per file" enabled="false">
          <stringProp name="delimiter">,</stringProp>
          <stringProp name="fileEncoding"></stringProp>
          <stringProp name="filename">sessionid${__threadNum}.csv</stringProp>
          <boolProp name="quotedData">false</boolProp>
          <boolProp name="recycle">true</boolProp>
          <stringProp name="shareMode">shareMode.all</stringProp>
          <boolProp name="stopThread">false</boolProp>
          <stringProp name="variableNames">sessionid</stringProp>
        </CSVDataSet>
        <hashTree/>
        <CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="File with all session IDs" enabled="false">
          <stringProp name="delimiter">,</stringProp>
          <stringProp name="fileEncoding"></stringProp>
          <stringProp name="filename">sessionids.csv</stringProp>
          <boolProp name="quotedData">false</boolProp>
          <boolProp name="recycle">true</boolProp>
          <stringProp name="shareMode">shareMode.all</stringProp>
          <boolProp name="stopThread">false</boolProp>
          <stringProp name="variableNames">sessionid</stringProp>
        </CSVDataSet>
        <hashTree/>
        <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="false">
          <collectionProp name="Arguments.arguments">
            <elementProp name="sessionid" elementType="Argument">
              <stringProp name="Argument.name">sessionid</stringProp>
              <stringProp name="Argument.value">${__UUID}</stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
            </elementProp>
          </collectionProp>
        </Arguments>
        <hashTree/>
        <UserParameters guiclass="UserParametersGui" testclass="UserParameters" testname="User Parameters" enabled="true">
          <collectionProp name="UserParameters.names">
            <stringProp name="607797809">sessionid</stringProp>
          </collectionProp>
          <collectionProp name="UserParameters.thread_values">
            <collectionProp name="-1753070663">
              <stringProp name="187832203">${__UUID}</stringProp>
            </collectionProp>
            <collectionProp name="2010322626">
              <stringProp name="97314">bbb</stringProp>
            </collectionProp>
          </collectionProp>
          <boolProp name="UserParameters.per_iteration">false</boolProp>
        </UserParameters>
        <hashTree/>
        <BeanShellPreProcessor guiclass="TestBeanGUI" testclass="BeanShellPreProcessor" testname="BeanShell PreProcessor" enabled="false">
          <stringProp name="filename"></stringProp>
          <stringProp name="parameters"></stringProp>
          <boolProp name="resetInterpreter">false</boolProp>
          <stringProp name="script">String uuid = &quot;${__UUID}&quot;; //assuming you have removed the user defined variables
//String uuid = &quot;${user.defined.UUID}&quot; + (ctx.getThreadNum() + 1);
String sessionid = uuid + (ctx.getThreadNum() + 1);
vars.put(&quot;sessionid&quot;, uuid);</stringProp>
        </BeanShellPreProcessor>
        <hashTree/>
        <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
          <collectionProp name="HeaderManager.headers">
            <elementProp name="" elementType="Header">
              <stringProp name="Header.name">content-type</stringProp>
              <stringProp name="Header.value">image/jpeg</stringProp>
            </elementProp>
          </collectionProp>
        </HeaderManager>
        <hashTree/>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="Metadata" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
            <collectionProp name="Arguments.arguments"/>
          </elementProp>
          <stringProp name="HTTPSampler.domain">${host}</stringProp>
          <stringProp name="HTTPSampler.port">${port}</stringProp>
          <stringProp name="HTTPSampler.connect_timeout">100</stringProp>
          <stringProp name="HTTPSampler.response_timeout">1000</stringProp>
          <stringProp name="HTTPSampler.protocol">http</stringProp>
          <stringProp name="HTTPSampler.contentEncoding">UTF-8</stringProp>
          <stringProp name="HTTPSampler.path">/thickclient/screenshot/3F2504E0-4F89-41D3-9A0C-0305E82C3301/${sessionid}/${__counter(true)}</stringProp>
          <stringProp name="HTTPSampler.method">POST</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.implementation">HttpClient4</stringProp>
          <elementProp name="HTTPsampler.Files" elementType="HTTPFileArgs">
            <collectionProp name="HTTPFileArgs.files">
              <elementProp name="C:\progs\apache-jmeter\apache-jmeter-2.13\bin\clarisite\screenshot.jpg" elementType="HTTPFileArg">
                <stringProp name="File.path">C:\progs\apache-jmeter\apache-jmeter-2.13\bin\clarisite\screenshot.jpg</stringProp>
                <stringProp name="File.paramname"></stringProp>
                <stringProp name="File.mimetype">image/jpeg</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <boolProp name="HTTPSampler.monitor">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="TestPlan.comments">Empty valid JSON</stringProp>
        </HTTPSamplerProxy>
        <hashTree/>
        <kg.apc.jmeter.vizualizers.CorrectedResultCollector guiclass="kg.apc.jmeter.vizualizers.HitsPerSecondGui" testclass="kg.apc.jmeter.vizualizers.CorrectedResultCollector" testname="jp@gc - Hits per Second" 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>
          <longProp name="interval_grouping">1000</longProp>
          <boolProp name="graph_aggregated">false</boolProp>
          <stringProp name="include_sample_labels"></stringProp>
          <stringProp name="exclude_sample_labels"></stringProp>
          <stringProp name="start_offset"></stringProp>
          <stringProp name="end_offset"></stringProp>
          <boolProp name="include_checkbox_state">false</boolProp>
          <boolProp name="exclude_checkbox_state">false</boolProp>
        </kg.apc.jmeter.vizualizers.CorrectedResultCollector>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

1 个答案:

答案 0 :(得分:1)

放置User Defined Variables配置元素的位置无关紧要。它们首先被初始化。所有线程的值都相同。

要创建特定于线程/用户的UUID,请使用以下代码在第一个HTTP请求下添加beanshell预处理器。

String uuid = "${user.defined.UUID}" + (ctx.getThreadNum() + 1); //appending thread number to make it unique for the user.
vars.put("UUID", uuid);

user.defined.UUID是您在用户定义变量中用于UUID的变量名称。如果sessionid是变量名,那么它将是

String uuid = "${sessionid}" + (ctx.getThreadNum() + 1); //appending thread number to make it unique for the user.
vars.put("UUID", uuid);

在整个测试过程中使用${UUID}来访问为用户创建的唯一会话ID。

在对您的测试计划有所了解后,

Thread Group
      Once Only Controller
          Beanshell Sampler
                 String uuid = "${__UUID}";
                 vars.put("UUID", uuid);
      HTTP Request 1
      HTTP Request 2
      .. etc