Creating a JMeter .jmx file programmatically via text-based instructions is tricky because the JMX file format is XML-based and highly specific to the JMeter functionality being implemented. Here, I'll guide you through the steps, and then I'll provide the structure of what it looks like.
Steps to Follow:
- Download and Install JMeter: If you haven't already, download and install Apache JMeter.
- Start JMeter: Open JMeter by running
jmeter.bat (Windows) or jmeter.sh (Linux).
- Create a Test Plan: In JMeter, create a new Test Plan.
- Set Up Thread Groups:
- Add a Thread Group to the Test Plan.
- Set Number of Threads (Users) to 30 (to accommodate 30 requests per second).
- Set Ramp-Up Period to 1 second (to spread 30 users over 1 second).
- Set Loop Count or Duration to 1 minute (for initial tests). Change it for subsequent tests of 2 minutes and 3 minutes.
- Add HTTP Request for OAuth Token:
- Add an HTTP Request sampler to get the OAuth token.
- Configure it with the POST method and the URL provided.
- Add necessary parameters (client_id, client_secret).
- Save OAuth Token in a Variable:
- Extract the token using JSON Extractor or Regular Expression Extractor.
- Store it in a JMeter variable.
- Add HTTP Header Manager: Add a Header Manager to include the Authorization header with the token for the subsequent GET request.
- Add HTTP Request for API:
- Add an HTTP Request sampler for the GET request.
- Configure it with the URL provided.
- Add a Loop Controller: Add a Loop Controller to repeat the sequence for 1 minute, 2 minutes, and 3 minutes as needed.
- Assertions and Listeners:
- Add assertions to validate responses.
- Add View Results Tree, Summary Report, and Graph Result listeners.
- Save Test Plan: Save the test plan as a .jmx file.
Example Structure in XML Format
Below is a simplified example of what the resulting JMX file might look like. This is a basic template and needs further customization within JMeter itself.
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.num_threads">30</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<longProp name="ThreadGroup.duration">60</longProp>
<stringProp name="ThreadGroup.scheduler">true</stringProp>
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller">
<boolProp name="LoopController.continue_forever">false</boolProp>
<intProp name="LoopController.loops">1</intProp>
</elementProp>
</ThreadGroup>
<hashTree>
<ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain"/>
<stringProp name="HTTPSampler.port"/>
<stringProp name="HTTPSampler.protocol">https</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path"/>
<stringProp name="HTTPSampler.concurrentPool">6</stringProp>
</ConfigTestElement>
<!-- HTTP Request for OAuth Token -->
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="Get OAuth Token" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments">
<elementProp name="" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">true</boolProp>
<stringProp name="Argument.value">ecc-pcm--pcm-trex--ecc-product-catalog-management-services</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
<stringProp name="Argument.name">client_id</stringProp>
</elementProp>
<elementProp name="" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">true</boolProp>
<stringProp name="Argument.value">8c0793b9-c226-4834-a2a3-1b4dae42f6cc</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
<stringProp name="Argument.name">client_secret</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp name="HTTPSampler.domain">iris-playground.live.dhei.telekom.de</stringProp>
<stringProp name="HTTPSampler.path">/auth/realms/default/protocol/openid-connect/token</stringProp>
<stringProp name="HTTPSampler.method">POST</stringProp>
</HTTPSamplerProxy>
<hashTree>
<!-- Extract the Token -->
<JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="Extract Token" enabled="true">
<stringProp name="JSONPostProcessor.referenceNames">oauth_token</stringProp>
<stringProp name="JSONPostProcessor.jsonPathExprs">$.access_token</stringProp>
<stringProp name="JSONPostProcessor.match_numbers">0</stringProp>
</JSONPostProcessor>
</hashTree>
<!-- HTTP Request for API -->
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="API Request" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">stargate-playground.live.dhei.telekom.de</stringProp>
<stringProp name="HTTPSampler.path">/ecc-pcm/tmf-api/productCatalogManagementB2Bsb/v2/productOffering</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
</HTTPSamplerProxy>
<hashTree>
<!-- Adding Authorization Header -->
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="" elementType="Header">
<stringProp name="Header.name">Authorization</stringProp>
<stringProp name="Header.value">Bearer ${oauth_token}</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<!-- Add your assertions here -->
</hashTree>
<!-- Adding Listeners -->
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<value class="SampleSaveConfiguration">
<longProp name="time">4000</longProp>
<boolProp name="latency">true</boolProp>
<boolProp name="timestamp">true</boolProp>
<boolProp name="success">true</boolProp>
<boolProp name="label">true</boolProp>
<boolProp name="code">true</boolProp>
<boolProp name="message">true</boolProp>
<boolProp name="threadName">true</boolProp>
<boolProp name="dataType">true</boolProp>
<boolProp name="encoding">false</boolProp>
<boolProp name="assertions">true</boolProp>
<boolProp name="subresults">true</boolProp>
<boolProp name="responseData">false</boolProp>
<boolProp name="samplerData">true</boolProp>
<boolProp name="xml">false</boolProp>
<boolProp name="fieldNames">true</boolProp>
<boolProp name="responseHeaders">true</boolProp>
<boolProp name="requestHeaders">true</boolProp>
<boolProp name="responseDataOnError">false</boolProp>
<boolProp name="saveAssertionResultsFailureMessage">true</boolProp>
<boolProp name="assertionsResultsToSave">0</boolProp>
<boolProp name="bytes">true</boolProp>
<boolProp name="sentBytes">true</boolProp>
<boolProp name="url">true</boolProp>
<boolProp name="hostname">true</boolProp>
<boolProp name="idleTime">true</boolProp>
<boolProp name="connectTime">true</boolProp>
</value>
</objProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</TestPlan>
Explanation:
- Thread Group: Configured to have 30 users with a 1-second ramp-up to spread the load.
- HTTP Request Defaults: Set the default protocol, which is HTTPS.
- HTTP Request for OAuth Token: Configured the POST request to get the token.
- JSON Extractor: Extracts the token from the JSON response and saves it as a JMeter variable.
- HTTP Request for API: Configured the GET request.
- HTTP Header Manager: Adds the Authorization header with the token.
- Listeners: Added View Results Tree for debugging and Summary Report for performance metrics.
You can save this XML structure in a .jmx file and then load it in JMeter for execution. You will need to customize it as per your specific validation and reporting needs.