Skip to content

Commit

Permalink
Merge pull request #26 from ETS-TAF/gatling
Browse files Browse the repository at this point in the history
Gatling integration
  • Loading branch information
Soufiene-Houssem authored Nov 13, 2023
2 parents ca374f6 + 6848e9c commit 30eed85
Show file tree
Hide file tree
Showing 68 changed files with 5,624 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ca.etsmtl.taf.controller;

import ca.etsmtl.taf.entity.GatlingRequest;
import ca.etsmtl.taf.provider.GatlingJarPathProvider;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/gatling")
public class GatlingApiController {

@PostMapping("/runSimulation")
public String runSimulation(@RequestBody GatlingRequest gatlingRequest) {
try {
String gatlingJarPath = new GatlingJarPathProvider().getGatlingJarPath();

String testRequest = "{\\\"baseUrl\\\":\\\""+gatlingRequest.getTestBaseUrl()+"\\\",\\\"scenarioName\\\":\\\""+gatlingRequest.getTestScenarioName()+"\\\",\\\"requestName\\\":\\\""+gatlingRequest.getTestRequestName()+"\\\",\\\"uri\\\":\\\""+gatlingRequest.getTestUri()+"\\\",\\\"requestBody\\\":\\\""+gatlingRequest.getTestRequestBody()+"\\\",\\\"methodType\\\":\\\""+gatlingRequest.getTestMethodType()+"\\\",\\\"usersNumber\\\":\\\""+gatlingRequest.getTestUsersNumber()+"\\\"}";
//Construire une liste d'arguments de ligne de commande à transmettre à Gatling
List<String> commandArgs = new ArrayList<>();
commandArgs.add("java");
commandArgs.add("-jar");
commandArgs.add(gatlingJarPath);
commandArgs.add("-DrequestJson=" + testRequest);

// Exécuter la simulation Gatling en tant que processus distinct
ProcessBuilder processBuilder = new ProcessBuilder(commandArgs);
Process process = processBuilder.start();
// Lire le résultat du processus
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line).append('\n');
}

int exitCode = process.waitFor();
return "Exit Code: " + exitCode + "\nOutput:\n" + output.toString();
} catch (IOException e) {
return "Error: " + e.getMessage();
} catch (URISyntaxException e) {
return "Error: " + e.getMessage();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return "Error: " + e.getMessage();
}
}
}
67 changes: 67 additions & 0 deletions backend/src/main/java/ca/etsmtl/taf/entity/GatlingRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ca.etsmtl.taf.entity;

public class GatlingRequest {
private String testBaseUrl;
private String testScenarioName;
private String testRequestName;
private String testUri;
private String testRequestBody;
private String testMethodType;
private int testUsersNumber;

public String getTestBaseUrl() {
return testBaseUrl;
}

public void setTestBaseUrl(String testBaseUrl) {
this.testBaseUrl = testBaseUrl;
}

public String getTestScenarioName() {
return testScenarioName;
}

public void setTestScenarioName(String testScenarioName) {
this.testScenarioName = testScenarioName;
}

public String getTestRequestName() {
return testRequestName;
}

public void setTestRequestName(String testRequestName) {
this.testRequestName = testRequestName;
}

public String getTestUri() {
return testUri;
}

public void setTestUri(String testUri) {
this.testUri = testUri;
}

public String getTestRequestBody() {
return testRequestBody;
}

public void setTestRequestBody(String testRequestBody) {
this.testRequestBody = testRequestBody;
}

public String getTestMethodType() {
return testMethodType;
}

public void setTestMethodType(String testMethodType) {
this.testMethodType = testMethodType;
}

public int getTestUsersNumber() {
return testUsersNumber;
}

public void setTestUsersNumber(int testUsersNumber) {
this.testUsersNumber = testUsersNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ca.etsmtl.taf.provider;

import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class GatlingJarPathProvider {
public String getGatlingJarPath() throws URISyntaxException {

Path backendDirectory = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent().getParent();
Path parentDirectory = backendDirectory.getParent();

//Construire le chemin vers le Gatling JAR
File gatlingJar = parentDirectory.resolve("gatling").resolve("target").resolve("gatling-1.0-SNAPSHOT-jar-with-dependencies.jar").toFile();
if (gatlingJar.exists()) {
return gatlingJar.getAbsolutePath();
} else {
return "Gatling JAR not found!";
}
}
}
Binary file added frontend/Test-Automation-Framework - Shortcut.lnk
Binary file not shown.
2 changes: 2 additions & 0 deletions frontend/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.less"
],
"scripts": []
Expand Down Expand Up @@ -99,6 +100,7 @@
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.less"
],
"scripts": []
Expand Down
66 changes: 66 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"private": true,
"dependencies": {
"@angular/animations": "~13.3.0",
"@angular/cdk": "^13.3.9",
"@angular/common": "~13.3.0",
"@angular/compiler": "~13.3.0",
"@angular/core": "~13.3.0",
"@angular/forms": "~13.3.0",
"@angular/material": "^13.3.9",
"@angular/platform-browser": "~13.3.0",
"@angular/platform-browser-dynamic": "~13.3.0",
"@angular/router": "~13.3.0",
Expand All @@ -36,4 +38,4 @@
"karma-jasmine-html-reporter": "~1.7.0",
"typescript": "~4.6.2"
}
}
}
16 changes: 16 additions & 0 deletions frontend/src/app/_services/performance-test-api.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* tslint:disable:no-unused-variable */

import { TestBed, async, inject } from '@angular/core/testing';
import { PerformanceTestApiService } from './performance-test-api.service';

describe('Service: PerformanceTestApi', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [PerformanceTestApiService]
});
});

it('should ...', inject([PerformanceTestApiService], (service: PerformanceTestApiService) => {
expect(service).toBeTruthy();
}));
});
30 changes: 30 additions & 0 deletions frontend/src/app/_services/performance-test-api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { GatlingRequest } from '../performance-test-api/gatling-api/gatling-request';


const GATLING_API = `${environment.apiUrl}/api/gatling/runSimulation`;

const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({
providedIn: 'root'
})
export class PerformanceTestApiService {


constructor(private http: HttpClient) { }

sendGatlingRequest(request: GatlingRequest): Observable<any> {
const url = `${GATLING_API}`;
const headers = new HttpHeaders({
'Content-Type': 'application/json'
});
return this.http.post(url, request, httpOptions);
}

}
2 changes: 2 additions & 0 deletions frontend/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BoardUserComponent } from './board-user/board-user.component';
import { BoardAdminComponent } from './board-admin/board-admin.component';
import { TestApiComponent } from './interface-test-api/test-api/test-api.component';
import { TestSeleniumComponent } from './selenium/test-selenium.component';
import { PerformanceTestApiComponent } from './performance-test-api/performance-test-api.component';

const routes: Routes = [
{ path: 'home', component: HomeComponent },
Expand All @@ -21,6 +22,7 @@ const routes: Routes = [
{ path: 'admin', component: BoardAdminComponent },
{ path: 'test-api', component: TestApiComponent },
{ path: 'test-selenium', component: TestSeleniumComponent },
{ path: 'performance-test-api', component: PerformanceTestApiComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' }
];

Expand Down
5 changes: 5 additions & 0 deletions frontend/src/app/app.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#app, .container{
height: 100%;
max-width: 100%;
padding: 0;
}
3 changes: 3 additions & 0 deletions frontend/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<li class="nav-item">
<a href="/test-selenium" class="nav-link" routerLink="test-selenium">Test Selenium </a>
</li>
<li class="nav-item">
<a href="/test-api" class="nav-link" routerLink="performance-test-api">Test de performance </a>
</li>
<li class="nav-item" *ngIf="showAdminBoard">
<a href="/admin" class="nav-link" routerLink="admin">Admin Board</a>
</li>
Expand Down
Loading

0 comments on commit 30eed85

Please sign in to comment.