AlexR
AlexR

Reputation: 115328

Generate session ID in Jmeter

I have to create load test using JMeter. The simplest version of test should create HTTP POST request with certain content. The URL should look like:

/myapp/SESSION-ID/SEQIENCE

Where SESSION-ID is custom UUID SEQUENCE - sequence number (1, 2, 3, ...) into the session.

I have defined HTTP request in JMeter like following:

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

My problem is to generate the session ID. I want to use different session for each user, but it should be the same for all hits of specific user. Sounds simple.

I added User defined variable to my thread group: sessionid=${__UUID}. This generates global UUID for all requests and threads. I tried to wrap User defined variables configuration element together with HTTP request sampler with simple controller to reduce the scope of variable visibility but it did not help.

What am I doing wrong?

Here is my test plan. Some elements on the top show my attempts to achieve the aim described above and are disabled.

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

Upvotes: 0

Views: 3482

Answers (1)

vins
vins

Reputation: 15370

It does not matter where you place the User Defined Variables config element. They get initialized first. The value will be same for all the threads.

To create thread/user specific UUID, add a beanshell preprocessor under the first HTTP request with below code.

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 is the variable name you had used for UUID in your user defined variable. If the sessionid is the variable name, It will be

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

Use ${UUID} throughout the test to access the unique sessionID created for the user.


After getting some idea on your test plan,

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

Upvotes: 1

Related Questions