2015-10-06 91 views
0

我必須使用JMeter創建負載測試。最簡單的測試版本應該創建帶有特定內容的HTTP POST請求。該網址應該會像:在Jmeter中生成會話ID

/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="[email protected] - 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

無論你放置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的變量名稱。如果會話ID是變量名,這將是

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 
+0

謝謝您的回覆,但它對於每個命中不是整個會話創建獨特的UUID。怎麼了? – AlexR

+0

請嘗試第二種方法。 '「$ {user.defined.UUID}」+(ctx.getThreadNum()+ 1);' - 它應該工作。或者請張貼您的測試計劃。 – vins

+0

''$ {__ UUID}「+(ctx.getThreadNum()+ 1)'與'」{{__ UUID}「'完全一樣。嘗試使用'user.defined.UUID'完全不起作用。雖然Jmeter不會抱怨,但服務器不會收到任何請求。我試圖在腳本中使用'System.out.println()'來打印生成的ID,但沒有打印出來。有什麼方法可以調試測試嗎? – AlexR