Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<&_ if (fragment.testingSection) { -&>
### Gatling

Performance tests are run by [Gatling](https://gatling.io/) and written in Scala. They're located in [src/test/java/gatling/simulations](src/test/java/gatling/simulations).
Performance tests are run by [Gatling](https://gatling.io/) and written in Java. They're located in [src/test/java/gatling/simulations](src/test/java/gatling/simulations).

You can execute all Gatling tests with
You can execute all Gatling tests with:

```bash
<%_ if (buildToolMaven) { _%>
Expand All @@ -13,8 +13,47 @@ You can execute all Gatling tests with
<%_ } _%>
```

<%_ if (applicationTypeMicroservice) { _%>
> **Note for microservice architectures:** Gatling tests in this microservice are designed to run via the **gateway**,
> which handles authentication and routes requests to the microservice using the `/services/<%= baseName.toLowerCase() %>/` prefix.
> Make sure the gateway is running before executing these tests.
>
> For **OAuth2/OIDC** architectures, the gateway is the only application that can authenticate users.
> Run Gatling tests from the gateway project instead of this microservice directly.
>
> To override the target URL (defaults to the gateway at `http://localhost:<%= gatewayServerPort || 8080 %>`):
> ```bash
<%_ if (buildToolMaven) { _%>
> ./mvnw gatling:test -DbaseURL=http://localhost:<%= gatewayServerPort || 8080 %>
<%_ } else { _%>
> ./gradlew gatlingRun -DbaseURL=http://localhost:<%= gatewayServerPort || 8080 %>
<%_ } _%>
> ```
<%_ } else { _%>
To override the target URL (defaults to `http://localhost:<%= serverPort %>`):
```bash
<%_ if (buildToolMaven) { _%>
./mvnw gatling:test -DbaseURL=http://localhost:<%= serverPort %>
<%_ } else { _%>
./gradlew gatlingRun -DbaseURL=http://localhost:<%= serverPort %>
<%_ } _%>
```
<%_ } _%>

You can also control the number of concurrent users and ramp-up duration:

```bash
<%_ if (buildToolMaven) { _%>
./mvnw gatling:test -Dusers=10 -Dramp=30
<%_ } else { _%>
./gradlew gatlingRun -Dusers=10 -Dramp=30
<%_ } _%>
```

<&_ } -&>

<&_ if (fragment.referenceSection) { -&>
- [Gatling](https://gatling.io/)
- [Gatling Maven Plugin](https://docs.gatling.io/reference/integrations/build-tools/maven-plugin/)
- [Gatling Gradle Plugin](https://docs.gatling.io/reference/integrations/build-tools/gradle-plugin/)
<&_ } -&>
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,47 @@ import io.gatling.javaapi.http.HttpProtocolBuilder;
/**
* Performance test for the <%- entityClass %> entity.
*
<%_ if (applicationTypeMicroservice) { _%>
* <p>This simulation targets the <strong>gateway</strong> (default:
* {@code http://localhost:<%= gatewayServerPort || 8080 %>}) so that authentication works
* correctly. Microservices are OAuth2 resource-servers only — they cannot issue tokens
* themselves. All API calls are routed via the gateway's
* {@code /services/<%= baseName.toLowerCase() %>/} prefix.
*
* <p>Override the gateway URL at runtime:
* <pre>{@code
* ./mvnw gatling:test -DbaseURL=http://gateway-host:8080
* }</pre>
<%_ } else { _%>
* <p>By default this simulation targets {@code http://localhost:<%= serverPort %>}.
* Override at runtime:
* <pre>{@code
* ./mvnw gatling:test -DbaseURL=http://localhost:<%= serverPort %>
* }</pre>
<%_ } _%>
*
* <p>Control load:
* <pre>{@code
* ./mvnw gatling:test -Dusers=50 -Dramp=60
* }</pre>
*
* @see <a href="https://github.com/jhipster/generator-jhipster/tree/v<%- jhipsterVersion %>/generators/gatling#logging-tips">Logging tips</a>
*/
public class <%- entityClass %>GatlingTest extends Simulation {

<%_ if (applicationTypeMicroservice) { _%>
// For microservices with OAuth2, tests run against the gateway which handles authentication
// and routes requests to the microservice via /services/<%- baseName.toLowerCase() %>/
String baseURL = Optional.ofNullable(System.getProperty("baseURL")).orElse("http://localhost:<%- gatewayServerPort || 8080 %>");

// API path prefix for this microservice when accessed through the gateway
String apiPrefix = "/services/<%- baseName.toLowerCase() %>";
<%_ } else { _%>
String baseURL = Optional.ofNullable(System.getProperty("baseURL")).orElse("http://localhost:<%- serverPort %>");

String apiPrefix = "";
<%_ } _%>

HttpProtocolBuilder httpConf = http
.baseUrl(baseURL)
.inferHtmlResources()
Expand All @@ -53,7 +88,7 @@ public class <%- entityClass %>GatlingTest extends Simulation {
.acceptLanguageHeader("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3")
.connectionHeader("keep-alive")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:33.0) Gecko/20100101 Firefox/33.0")
.silentResources() // Silence all resources like css or css so they don't clutter the results
.silentResources() // Silence all resources like css or js so they don't clutter the results
<%_ if (authenticationTypeOauth2) { _%>
.disableFollowRedirect() // We must follow redirects manually to get the xsrf token from the keycloak redirect
.disableAutoReferer()
Expand Down Expand Up @@ -155,12 +190,12 @@ public class <%- entityClass %>GatlingTest extends Simulation {
.pause(10)
.repeat(2).on(
exec(http("Get all <%- entityInstancePlural %>")
.get("<% if (applicationTypeMicroservice) { %>/services/<%- baseName.toLowerCase() %><% } %>/api/<%- entityApiUrl %>")
.get(apiPrefix + "/api/<%- entityApiUrl %>")
.headers(headersHttpAuthenticated)
.check(status().is(200)))
.pause(Duration.ofSeconds(10), Duration.ofSeconds(20))
.exec(http("Create new <%- entityInstance %>")
.post("<% if (applicationTypeMicroservice) { %>/services/<%- baseName.toLowerCase() %><% } %>/api/<%- entityApiUrl %>")
.post(apiPrefix + "/api/<%- entityApiUrl %>")
.headers(headersHttpAuthenticated)
.body(StringBody("{" +
<%_ for (const [idx, field] of fields.filter(f => !(f.id && f.autoGenerate)).entries()) { _%>
Expand All @@ -172,12 +207,12 @@ public class <%- entityClass %>GatlingTest extends Simulation {
.pause(10)
.repeat(5).on(
exec(http("Get created <%- entityInstance %>")
.get("<% if (applicationTypeMicroservice) { %>/services/<%- baseName.toLowerCase() %><% } %>${new_<%- entityInstance %>_url}")
.get(apiPrefix + "${new_<%- entityInstance %>_url}")
.headers(headersHttpAuthenticated))
.pause(10)
)
.exec(http("Delete created <%- entityInstance %>")
.delete("<% if (applicationTypeMicroservice) { %>/services/<%- baseName.toLowerCase() %><% } %>${new_<%- entityInstance %>_url}")
.delete(apiPrefix + "${new_<%- entityInstance %>_url}")
.headers(headersHttpAuthenticated))
.pause(10)
);
Expand Down