BAEL-4672 added content for JMeter extract data to file tutorial
This commit is contained in:
		
							parent
							
								
									89eb93a46c
								
							
						
					
					
						commit
						60a19ec9f4
					
				| @ -7,7 +7,7 @@ It contains the code of a simple API for some CRUD operations built using Spring | ||||
| 
 | ||||
| - Maven | ||||
| - JDK 8 | ||||
| - MongoDB | ||||
| - MongoDB (Note: for the Write Extracted Data to a File Using JMeter example MongoDB is not required) | ||||
| 
 | ||||
| ### Running | ||||
| 
 | ||||
| @ -36,6 +36,14 @@ Or create a new one via a POST: | ||||
| $ curl -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Dassi", "lastName" : "Orleando", "phoneNumber": "+237 545454545", "email": "mymail@yahoo.fr" }' localhost:8080/students | ||||
| ``` | ||||
| 
 | ||||
| ### Available Test API | ||||
| 
 | ||||
| You can view the test response using curl: | ||||
| 
 | ||||
| ```bash | ||||
| $ curl localhost:8080/api/test | ||||
| ``` | ||||
| 
 | ||||
| Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080) | ||||
| 
 | ||||
| Enjoy it :) | ||||
| @ -44,3 +52,4 @@ Enjoy it :) | ||||
| 
 | ||||
| - [Intro to Performance Testing using JMeter](https://www.baeldung.com/jmeter) | ||||
| - [Configure Jenkins to Run and Show JMeter Tests](https://www.baeldung.com/jenkins-and-jmeter) | ||||
| - [Write Extracted Data to a File Using JMeter](https://www.baeldung.com/jmeter-file-extraction) | ||||
|  | ||||
| @ -0,0 +1,18 @@ | ||||
| package com.baeldung.controller; | ||||
| 
 | ||||
| import com.baeldung.model.Response; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| 
 | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| import static java.lang.String.format; | ||||
| 
 | ||||
| @RestController | ||||
| public class TestController { | ||||
| 
 | ||||
|     @GetMapping("/api/test") | ||||
|     public Response test() { | ||||
|         return new Response(format("Test message... %s.", UUID.randomUUID())); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										40
									
								
								jmeter/src/main/java/com/baeldung/model/Response.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								jmeter/src/main/java/com/baeldung/model/Response.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,40 @@ | ||||
| package com.baeldung.model; | ||||
| 
 | ||||
| import java.time.Instant; | ||||
| import java.util.UUID; | ||||
| 
 | ||||
| public class Response { | ||||
|     private Instant timestamp; | ||||
|     private UUID uuid; | ||||
|     private String message; | ||||
| 
 | ||||
|     public Response(String message) { | ||||
|         this.timestamp = Instant.now(); | ||||
|         this.uuid = UUID.randomUUID(); | ||||
|         this.message = message; | ||||
|     } | ||||
| 
 | ||||
|     public Instant getTimestamp() { | ||||
|         return timestamp; | ||||
|     } | ||||
| 
 | ||||
|     public void setTimestamp(Instant timestamp) { | ||||
|         this.timestamp = timestamp; | ||||
|     } | ||||
| 
 | ||||
|     public UUID getUuid() { | ||||
|         return uuid; | ||||
|     } | ||||
| 
 | ||||
|     public void setUuid(UUID uuid) { | ||||
|         this.uuid = uuid; | ||||
|     } | ||||
| 
 | ||||
|     public String getMessage() { | ||||
|         return message; | ||||
|     } | ||||
| 
 | ||||
|     public void setMessage(String message) { | ||||
|         this.message = message; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										124
									
								
								jmeter/src/main/resources/FileExtractionExample.jmx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								jmeter/src/main/resources/FileExtractionExample.jmx
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,124 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <jmeterTestPlan version="1.2" properties="5.0" jmeter="5.4.1"> | ||||
|   <hashTree> | ||||
|     <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true"> | ||||
|       <stringProp name="TestPlan.comments">To run this test plan you must also be running the Spring application "JmeterApplication" That can be found in this directory</stringProp> | ||||
|       <boolProp name="TestPlan.functional_mode">false</boolProp> | ||||
|       <boolProp name="TestPlan.tearDown_on_shutdown">true</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> | ||||
|           <stringProp name="LoopController.loops">1</stringProp> | ||||
|         </elementProp> | ||||
|         <stringProp name="ThreadGroup.num_threads">1</stringProp> | ||||
|         <stringProp name="ThreadGroup.ramp_time">1</stringProp> | ||||
|         <boolProp name="ThreadGroup.scheduler">false</boolProp> | ||||
|         <stringProp name="ThreadGroup.duration"></stringProp> | ||||
|         <stringProp name="ThreadGroup.delay"></stringProp> | ||||
|         <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp> | ||||
|       </ThreadGroup> | ||||
|       <hashTree> | ||||
|         <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="Call GET Test endpoint " 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">localhost</stringProp> | ||||
|           <stringProp name="HTTPSampler.port">8080</stringProp> | ||||
|           <stringProp name="HTTPSampler.protocol">http</stringProp> | ||||
|           <stringProp name="HTTPSampler.contentEncoding"></stringProp> | ||||
|           <stringProp name="HTTPSampler.path">/api/test</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> | ||||
|           <stringProp name="HTTPSampler.embedded_url_re"></stringProp> | ||||
|           <stringProp name="HTTPSampler.connect_timeout"></stringProp> | ||||
|           <stringProp name="HTTPSampler.response_timeout"></stringProp> | ||||
|         </HTTPSamplerProxy> | ||||
|         <hashTree> | ||||
|           <JSONPostProcessor guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON Extractor" enabled="true"> | ||||
|             <stringProp name="JSONPostProcessor.referenceNames">message</stringProp> | ||||
|             <stringProp name="JSONPostProcessor.jsonPathExprs">$.message</stringProp> | ||||
|             <stringProp name="JSONPostProcessor.match_numbers">1</stringProp> | ||||
|             <boolProp name="JSONPostProcessor.compute_concat">true</boolProp> | ||||
|             <stringProp name="JSONPostProcessor.defaultValues">NOT_FOUND</stringProp> | ||||
|           </JSONPostProcessor> | ||||
|           <hashTree/> | ||||
|           <BeanShellPostProcessor guiclass="TestBeanGUI" testclass="BeanShellPostProcessor" testname="Response to file using BeanShell PostProcessor" enabled="true"> | ||||
|             <stringProp name="filename"></stringProp> | ||||
|             <stringProp name="parameters"></stringProp> | ||||
|             <boolProp name="resetInterpreter">false</boolProp> | ||||
|             <stringProp name="script">FileWriter fWriter = new FileWriter("/Users/liamgarvie/Documents/result.txt", true);  | ||||
| BufferedWriter buff = new BufferedWriter(fWriter); | ||||
| 
 | ||||
| buff.write("Response Code : " + ctx.getPreviousResult().getResponseCode()); | ||||
| buff.write(System.getProperty("line.separator")); | ||||
| buff.write("Response Headers : " + ctx.getPreviousResult().getResponseHeaders()); | ||||
| buff.write(System.getProperty("line.separator")); | ||||
| buff.write("Response Body : " + new String(ctx.getPreviousResult().getResponseData())); | ||||
| 
 | ||||
| buff.write("More complex extraction : " + vars.get("message")); | ||||
| 
 | ||||
| buff.close(); | ||||
| fWriter.close();</stringProp> | ||||
|           </BeanShellPostProcessor> | ||||
|           <hashTree/> | ||||
|           <ResultSaver guiclass="ResultSaverGui" testclass="ResultSaver" testname="Response to file using file write Listener" enabled="true"> | ||||
|             <stringProp name="FileSaver.filename">response</stringProp> | ||||
|             <boolProp name="FileSaver.errorsonly">false</boolProp> | ||||
|             <boolProp name="FileSaver.successonly">false</boolProp> | ||||
|             <boolProp name="FileSaver.skipsuffix">false</boolProp> | ||||
|             <boolProp name="FileSaver.skipautonumber">false</boolProp> | ||||
|           </ResultSaver> | ||||
|           <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>true</fieldNames> | ||||
|               <responseHeaders>false</responseHeaders> | ||||
|               <requestHeaders>false</requestHeaders> | ||||
|               <responseDataOnError>false</responseDataOnError> | ||||
|               <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage> | ||||
|               <assertionsResultsToSave>0</assertionsResultsToSave> | ||||
|               <bytes>true</bytes> | ||||
|               <sentBytes>true</sentBytes> | ||||
|               <url>true</url> | ||||
|               <threadCounts>true</threadCounts> | ||||
|               <idleTime>true</idleTime> | ||||
|               <connectTime>true</connectTime> | ||||
|             </value> | ||||
|           </objProp> | ||||
|           <stringProp name="filename"></stringProp> | ||||
|         </ResultCollector> | ||||
|         <hashTree/> | ||||
|       </hashTree> | ||||
|     </hashTree> | ||||
|   </hashTree> | ||||
| </jmeterTestPlan> | ||||
							
								
								
									
										36
									
								
								jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | ||||
| package com.baeldung; | ||||
| 
 | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.mock.web.MockHttpServletResponse; | ||||
| import org.springframework.test.web.servlet.MockMvc; | ||||
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||||
| import org.springframework.web.context.WebApplicationContext; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||||
| import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||||
| 
 | ||||
| @SpringBootTest | ||||
| class JmeterIntegrationTest { | ||||
| 
 | ||||
| 	MockMvc mvc; | ||||
| 
 | ||||
| 	public JmeterIntegrationTest(WebApplicationContext wac) { | ||||
| 		this.mvc = MockMvcBuilders.webAppContextSetup(wac).build(); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	void whenCallingTestController_thenWeShouldRecieveRandomizedResponse() throws Exception { | ||||
| 		MockHttpServletResponse response = mvc.perform(get("/api/test")) | ||||
| 		  .andDo(print()) | ||||
| 		  .andExpect(status().isOk()) | ||||
| 		  .andReturn() | ||||
| 		  .getResponse(); | ||||
| 
 | ||||
| 		assertThat(response.getContentAsString()) | ||||
| 		  .contains("Test message..."); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user