From 60a19ec9f48b2c25cbfff229466fb469e06765ad Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Sat, 13 Feb 2021 11:38:55 +0000 Subject: [PATCH 1/7] BAEL-4672 added content for JMeter extract data to file tutorial --- jmeter/README.md | 11 +- .../baeldung/controller/TestController.java | 18 +++ .../java/com/baeldung/model/Response.java | 40 ++++++ .../main/resources/FileExtractionExample.jmx | 124 ++++++++++++++++++ .../com/baeldung/JmeterIntegrationTest.java | 36 +++++ 5 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 jmeter/src/main/java/com/baeldung/controller/TestController.java create mode 100644 jmeter/src/main/java/com/baeldung/model/Response.java create mode 100644 jmeter/src/main/resources/FileExtractionExample.jmx create mode 100644 jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java diff --git a/jmeter/README.md b/jmeter/README.md index 81300afe7c..84677ad618 100644 --- a/jmeter/README.md +++ b/jmeter/README.md @@ -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) diff --git a/jmeter/src/main/java/com/baeldung/controller/TestController.java b/jmeter/src/main/java/com/baeldung/controller/TestController.java new file mode 100644 index 0000000000..6e286265d3 --- /dev/null +++ b/jmeter/src/main/java/com/baeldung/controller/TestController.java @@ -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())); + } +} diff --git a/jmeter/src/main/java/com/baeldung/model/Response.java b/jmeter/src/main/java/com/baeldung/model/Response.java new file mode 100644 index 0000000000..547e5b536e --- /dev/null +++ b/jmeter/src/main/java/com/baeldung/model/Response.java @@ -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; + } +} diff --git a/jmeter/src/main/resources/FileExtractionExample.jmx b/jmeter/src/main/resources/FileExtractionExample.jmx new file mode 100644 index 0000000000..e2047d0528 --- /dev/null +++ b/jmeter/src/main/resources/FileExtractionExample.jmx @@ -0,0 +1,124 @@ + + + + + To run this test plan you must also be running the Spring application "JmeterApplication" That can be found in this directory + false + true + false + + + + + + + + continue + + false + 1 + + 1 + 1 + false + + + true + + + + + + + localhost + 8080 + http + + /api/test + GET + true + false + true + false + + + + + + + message + $.message + 1 + true + NOT_FOUND + + + + + + false + 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(); + + + + response + false + false + false + false + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + + diff --git a/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java b/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java new file mode 100644 index 0000000000..5ccbeb875b --- /dev/null +++ b/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java @@ -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..."); + } + +} From 7630f9dd91c8b8ce252455380d990802dbc8eaa7 Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Mon, 15 Feb 2021 16:25:11 +0000 Subject: [PATCH 2/7] BAEL-4672 changed file path to be placeholder for JMEter file axtraction script --- jmeter/src/main/resources/FileExtractionExample.jmx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jmeter/src/main/resources/FileExtractionExample.jmx b/jmeter/src/main/resources/FileExtractionExample.jmx index e2047d0528..961b6f143f 100644 --- a/jmeter/src/main/resources/FileExtractionExample.jmx +++ b/jmeter/src/main/resources/FileExtractionExample.jmx @@ -57,7 +57,7 @@ false - FileWriter fWriter = new FileWriter("/Users/liamgarvie/Documents/result.txt", true); + FileWriter fWriter = new FileWriter("/result.txt", true); BufferedWriter buff = new BufferedWriter(fWriter); buff.write("Response Code : " + ctx.getPreviousResult().getResponseCode()); From af040e54c6aad2082ba7aaa3c1ae3a86e11313ac Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Wed, 17 Feb 2021 15:21:11 +0000 Subject: [PATCH 3/7] BAEL-4672 removed link from README file for JMeter file extraction article --- jmeter/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/jmeter/README.md b/jmeter/README.md index 84677ad618..6963aebb7c 100644 --- a/jmeter/README.md +++ b/jmeter/README.md @@ -52,4 +52,3 @@ 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) From 0fabae2d65f6a6303a1aee9b0444df28e2a77b8d Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Sat, 20 Feb 2021 11:13:45 +0000 Subject: [PATCH 4/7] changed name of JMeter test controller to remove 'test' language from production code --- .../{TestController.java => RetrieveUuidController.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename jmeter/src/main/java/com/baeldung/controller/{TestController.java => RetrieveUuidController.java} (85%) diff --git a/jmeter/src/main/java/com/baeldung/controller/TestController.java b/jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java similarity index 85% rename from jmeter/src/main/java/com/baeldung/controller/TestController.java rename to jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java index 6e286265d3..3a2c14fc4b 100644 --- a/jmeter/src/main/java/com/baeldung/controller/TestController.java +++ b/jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java @@ -9,10 +9,10 @@ import java.util.UUID; import static java.lang.String.format; @RestController -public class TestController { +public class RetrieveUuidController { @GetMapping("/api/test") - public Response test() { + public Response uuid() { return new Response(format("Test message... %s.", UUID.randomUUID())); } } From 745f15f7f2c8623714ba0b79029b6903894fcdd6 Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Sat, 20 Feb 2021 11:18:09 +0000 Subject: [PATCH 5/7] changed uri for jmeter UUID controller --- .../java/com/baeldung/controller/RetrieveUuidController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java b/jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java index 3a2c14fc4b..32265c0170 100644 --- a/jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java +++ b/jmeter/src/main/java/com/baeldung/controller/RetrieveUuidController.java @@ -11,7 +11,7 @@ import static java.lang.String.format; @RestController public class RetrieveUuidController { - @GetMapping("/api/test") + @GetMapping("/api/uuid") public Response uuid() { return new Response(format("Test message... %s.", UUID.randomUUID())); } From 04475d1555db0827455c9aa74c2e7c10434b13f4 Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Sat, 20 Feb 2021 11:19:12 +0000 Subject: [PATCH 6/7] Updated JMeter tests to reflect UUID controller change --- jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java b/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java index 5ccbeb875b..537e27dc55 100644 --- a/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java +++ b/jmeter/src/test/java/com/baeldung/JmeterIntegrationTest.java @@ -22,8 +22,8 @@ class JmeterIntegrationTest { } @Test - void whenCallingTestController_thenWeShouldRecieveRandomizedResponse() throws Exception { - MockHttpServletResponse response = mvc.perform(get("/api/test")) + void whenCallingUUIDController_thenWeShouldRecieveRandomizedResponse() throws Exception { + MockHttpServletResponse response = mvc.perform(get("/api/uuid")) .andDo(print()) .andExpect(status().isOk()) .andReturn() From 4e18d98258a7271c0af728db34dfcdbd10d670a2 Mon Sep 17 00:00:00 2001 From: Liam Garvie Date: Sun, 21 Feb 2021 20:05:30 +0000 Subject: [PATCH 7/7] BAEL-4672 updated JMeter readme to reflect changes in API format --- jmeter/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jmeter/README.md b/jmeter/README.md index 6963aebb7c..11351ffdda 100644 --- a/jmeter/README.md +++ b/jmeter/README.md @@ -36,12 +36,12 @@ 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 +### Available UUID API You can view the test response using curl: ```bash -$ curl localhost:8080/api/test +$ curl localhost:8080/api/uuid ``` Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080)