Skip to content

Commit

Permalink
Refactoring code for bacend---Make controller return JSON object Inse…
Browse files Browse the repository at this point in the history
…tad of String
  • Loading branch information
safouenaouadi committed Nov 13, 2023
1 parent f5e016e commit 7717f15
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 100 deletions.
7 changes: 7 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.5</version> <!-- Use the latest version available -->
</dependency>

</dependencies>

<build>
Expand Down
42 changes: 36 additions & 6 deletions backend/src/main/java/ca/etsmtl/taf/jmeter/JMeterRunner.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package ca.etsmtl.taf.jmeter;

import com.fasterxml.jackson.databind.MappingIterator;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class JMeterRunner {

Expand All @@ -34,10 +39,10 @@ public static String runJMeter( ) {
String timestamp = dateFormat.format(new Date());

// Add the timestamp to the file name as the salt
String resultsFilePath = "backend/src/main/resources/jmeter/results/result_"+timestamp +".jtl";
String resultsFilePath = "backend/src/main/resources/jmeter/results/result_"+timestamp +".csv";
//Please change to your JMETER Executable local path
String jmeterExecutable = "\"C:\\Users\\safou\\Downloads\\apache-jmeter-5.5\\bin\\jmeter.bat\"";
String htmlReportPath="\"C:\\Users\\safou\\OneDrive\\Desktop\\result\"";
//String htmlReportPath="\"C:\\Users\\safou\\OneDrive\\Desktop\\result\"";
try {
String jmeterCommand = jmeterExecutable + " -n -t " + jmxFilePath + " -l " + resultsFilePath /*+" -e -o " + htmlReportPath */;
// Run the command
Expand All @@ -59,4 +64,29 @@ public static String runJMeter( ) {
}
return resultsFilePath;
}


public static List<Map<String, String>> convertCSVtoJSON(String csvFilePath) throws IOException, CsvException, CsvException {
try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) {
List<String[]> csvData = reader.readAll();

// Assuming the first row contains headers
String[] headers = csvData.get(0);

return csvData.stream()
.skip(1) // Skip the header row
.map(row -> {
Map<String, String> jsonMap = createJsonMap(headers, row);
return jsonMap;
})
.collect(Collectors.toList());
}
}

private static Map<String, String> createJsonMap(String[] headers, String[] values) {
return IntStream.range(0, headers.length)
.boxed()
.collect(Collectors.toMap(i -> headers[i], i -> values[i]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@

import ca.etsmtl.taf.jmeter.JMeterRunner;
import ca.etsmtl.taf.jmeter.model.JmeterTestPlan;
import com.opencsv.exceptions.CsvException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

@RestController
//@RequestMapping("/jmeter")
public class JmeterController {

@PostMapping("/jmeter")
public ResponseEntity<String> getJmeterTestPlan(@RequestBody JmeterTestPlan jmeterTestPlan){
public ResponseEntity<?> getJmeterTestPlan(@RequestBody JmeterTestPlan jmeterTestPlan) throws IOException, CsvException {

jmeterTestPlan.generateTestPlan();
ExecutorService executorService = Executors.newSingleThreadExecutor();

// Submit the task to the ExecutorService
AtomicReference<String> resultPathRef = new AtomicReference<>();
Future<?> future = executorService.submit(() -> {
JMeterRunner.runJMeter();
String result = JMeterRunner.runJMeter();
resultPathRef.set(result);
});

try {
// Wait for the task to finish
future.get();
Expand All @@ -39,9 +45,22 @@ public ResponseEntity<String> getJmeterTestPlan(@RequestBody JmeterTestPlan jmet
} finally {
// Shutdown the ExecutorService to release resources
executorService.shutdown();
}
List<Map<String, String>> result=null;
String resultPath = resultPathRef.get();
System.out.println("Result Path: " + resultPath);
if (resultPath !="" && resultPath!=null){
try {
result =JMeterRunner.convertCSVtoJSON(resultPath);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (CsvException e) {
throw new RuntimeException(e);
}

}

return ResponseEntity.ok("Task completed successfully.");
return (ResponseEntity<?>) ResponseEntity.ok(result);
}

}
2 changes: 1 addition & 1 deletion backend/src/main/resources/jmeter/TestPlan.jmx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
guiclass="LoopControlPanel" testclass="LoopController"
testname="Loop Controller"
enabled="true">
<stringProp name="LoopController.loops">2</stringProp>
<stringProp name="LoopController.loops">1</stringProp>
<boolProp name="LoopController.continue_forever">false</boolProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">10</stringProp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1699835866090,290,HTTP Request,200,OK,Thread Group 1-2,text,true,,11115,119,2,2,https://octoperf.com/blog/,283,0,73
1699835865734,645,HTTP Request,200,OK,Thread Group 1-1,text,true,,11116,119,2,2,https://octoperf.com/blog/,638,0,387
1699835866382,123,HTTP Request,200,OK,Thread Group 1-1,text,true,,11116,119,2,2,https://octoperf.com/blog/,122,0,0
1699835866382,125,HTTP Request,200,OK,Thread Group 1-2,text,true,,11115,119,1,1,https://octoperf.com/blog/,125,0,0
1699835866589,185,HTTP Request,200,OK,Thread Group 1-3,text,true,,11116,119,1,1,https://octoperf.com/blog/,185,0,65
1699835866775,244,HTTP Request,200,OK,Thread Group 1-3,text,true,,11116,119,1,1,https://octoperf.com/blog/,244,0,0
1699835867088,304,HTTP Request,200,OK,Thread Group 1-4,text,true,,11116,119,1,1,https://octoperf.com/blog/,304,0,65
1699835867393,124,HTTP Request,200,OK,Thread Group 1-4,text,true,,11116,119,1,1,https://octoperf.com/blog/,122,0,0
1699835867591,181,HTTP Request,200,OK,Thread Group 1-5,text,true,,11116,119,1,1,https://octoperf.com/blog/,180,0,60
1699835867772,258,HTTP Request,200,OK,Thread Group 1-5,text,true,,11116,119,1,1,https://octoperf.com/blog/,207,0,0
1699835868087,357,HTTP Request,200,OK,Thread Group 1-6,text,true,,11116,119,1,1,https://octoperf.com/blog/,292,0,71
1699835868445,124,HTTP Request,200,OK,Thread Group 1-6,text,true,,11116,119,1,1,https://octoperf.com/blog/,123,0,0
1699835868586,527,HTTP Request,200,OK,Thread Group 1-7,text,true,,11116,119,2,2,https://octoperf.com/blog/,527,0,410
1699835869085,153,HTTP Request,200,OK,Thread Group 1-8,text,true,,11115,119,2,2,https://octoperf.com/blog/,152,0,56
1699835869114,128,HTTP Request,200,OK,Thread Group 1-7,text,true,,11116,119,2,2,https://octoperf.com/blog/,128,0,0
1699835869238,136,HTTP Request,200,OK,Thread Group 1-8,text,true,,11116,119,1,1,https://octoperf.com/blog/,136,0,0
1699835869586,283,HTTP Request,200,OK,Thread Group 1-9,text,true,,11116,119,1,1,https://octoperf.com/blog/,268,0,76
1699835869869,128,HTTP Request,200,OK,Thread Group 1-9,text,true,,11116,119,1,1,https://octoperf.com/blog/,127,0,0
1699835870086,297,HTTP Request,200,OK,Thread Group 1-10,text,true,,11116,119,1,1,https://octoperf.com/blog/,297,0,85
1699835870384,120,HTTP Request,200,OK,Thread Group 1-10,text,true,,11116,119,1,1,https://octoperf.com/blog/,120,0,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1699836052118,317,HTTP Request,200,OK,Thread Group 1-2,text,true,,11115,119,2,2,https://octoperf.com/blog/,309,0,112
1699836051708,726,HTTP Request,200,OK,Thread Group 1-1,text,true,,11116,119,2,2,https://octoperf.com/blog/,719,0,501
1699836052437,202,HTTP Request,200,OK,Thread Group 1-1,text,true,,11116,119,3,3,https://octoperf.com/blog/,201,0,0
1699836052437,203,HTTP Request,200,OK,Thread Group 1-2,text,true,,11115,119,2,2,https://octoperf.com/blog/,203,0,0
1699836052617,191,HTTP Request,200,OK,Thread Group 1-3,text,true,,11116,119,1,1,https://octoperf.com/blog/,190,0,42
1699836052808,157,HTTP Request,200,OK,Thread Group 1-3,text,true,,11116,119,1,1,https://octoperf.com/blog/,157,0,0
1699836053117,301,HTTP Request,200,OK,Thread Group 1-4,text,true,,11116,119,1,1,https://octoperf.com/blog/,289,0,76
1699836053418,203,HTTP Request,200,OK,Thread Group 1-4,text,true,,11116,119,2,2,https://octoperf.com/blog/,202,0,0
1699836053618,179,HTTP Request,200,OK,Thread Group 1-5,text,true,,11116,119,1,1,https://octoperf.com/blog/,179,0,62
1699836053798,151,HTTP Request,200,OK,Thread Group 1-5,text,true,,11116,119,1,1,https://octoperf.com/blog/,151,0,0
1699836054115,283,HTTP Request,200,OK,Thread Group 1-6,text,true,,11116,119,1,1,https://octoperf.com/blog/,274,0,72
1699836054398,122,HTTP Request,200,OK,Thread Group 1-6,text,true,,11116,119,1,1,https://octoperf.com/blog/,121,0,0
1699836054615,172,HTTP Request,200,OK,Thread Group 1-7,text,true,,11116,119,1,1,https://octoperf.com/blog/,172,0,59
1699836054788,121,HTTP Request,200,OK,Thread Group 1-7,text,true,,11116,119,1,1,https://octoperf.com/blog/,120,0,0
1699836055116,253,HTTP Request,200,OK,Thread Group 1-8,text,true,,11116,119,1,1,https://octoperf.com/blog/,253,0,56
1699836055370,199,HTTP Request,200,OK,Thread Group 1-8,text,true,,11116,119,1,1,https://octoperf.com/blog/,199,0,0
1699836055618,343,HTTP Request,200,OK,Thread Group 1-9,text,true,,11116,119,1,1,https://octoperf.com/blog/,343,0,60
1699836055961,127,HTTP Request,200,OK,Thread Group 1-9,text,true,,11116,119,1,1,https://octoperf.com/blog/,126,0,0
1699836056115,278,HTTP Request,200,OK,Thread Group 1-10,text,true,,11116,119,1,1,https://octoperf.com/blog/,264,0,65
1699836056394,260,HTTP Request,200,OK,Thread Group 1-10,text,true,,11116,119,1,1,https://octoperf.com/blog/,193,0,0
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1699836254122,601,HTTP Request,200,OK,Thread Group 1-1,text,true,,11116,119,2,2,https://octoperf.com/blog/,598,0,358
1699836254511,212,HTTP Request,200,OK,Thread Group 1-2,text,true,,11115,119,2,2,https://octoperf.com/blog/,209,0,51
1699836255011,186,HTTP Request,200,OK,Thread Group 1-3,text,true,,11116,119,1,1,https://octoperf.com/blog/,185,0,62
1699836255510,180,HTTP Request,200,OK,Thread Group 1-4,text,true,,11116,119,1,1,https://octoperf.com/blog/,180,0,64
1699836256009,267,HTTP Request,200,OK,Thread Group 1-5,text,true,,11116,119,1,1,https://octoperf.com/blog/,264,0,57
1699836256513,257,HTTP Request,200,OK,Thread Group 1-6,text,true,,11116,119,1,1,https://octoperf.com/blog/,257,0,56
1699836257008,221,HTTP Request,200,OK,Thread Group 1-7,text,true,,11116,119,1,1,https://octoperf.com/blog/,220,0,79
1699836257507,259,HTTP Request,200,OK,Thread Group 1-8,text,true,,11116,119,1,1,https://octoperf.com/blog/,259,0,56
1699836258008,258,HTTP Request,200,OK,Thread Group 1-9,text,true,,11116,119,1,1,https://octoperf.com/blog/,257,0,53
1699836258507,281,HTTP Request,200,OK,Thread Group 1-10,text,true,,11116,119,1,1,https://octoperf.com/blog/,264,0,60
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1699839314268,219,HTTP Request,200,OK,Thread Group 1-2,text,true,,11115,119,2,2,https://octoperf.com/blog/,213,0,62
1699839313885,602,HTTP Request,200,OK,Thread Group 1-1,text,true,,11116,119,2,2,https://octoperf.com/blog/,596,0,345
1699839314767,255,HTTP Request,200,OK,Thread Group 1-3,text,true,,11116,119,1,1,https://octoperf.com/blog/,254,0,95
1699839315267,269,HTTP Request,200,OK,Thread Group 1-4,text,true,,11116,119,1,1,https://octoperf.com/blog/,269,0,60
1699839315765,175,HTTP Request,200,OK,Thread Group 1-5,text,true,,11116,119,1,1,https://octoperf.com/blog/,174,0,39
1699839316264,194,HTTP Request,200,OK,Thread Group 1-6,text,true,,11116,119,1,1,https://octoperf.com/blog/,194,0,49
1699839316765,185,HTTP Request,200,OK,Thread Group 1-7,text,true,,11116,119,1,1,https://octoperf.com/blog/,185,0,72
1699839317267,167,HTTP Request,200,OK,Thread Group 1-8,text,true,,11116,119,1,1,https://octoperf.com/blog/,167,0,40
1699839317762,240,HTTP Request,200,OK,Thread Group 1-9,text,true,,11116,119,1,1,https://octoperf.com/blog/,240,0,33
1699839318262,177,HTTP Request,200,OK,Thread Group 1-10,text,true,,11116,119,1,1,https://octoperf.com/blog/,177,0,50
Loading

0 comments on commit 7717f15

Please sign in to comment.