-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuestionGatlingTest.java
More file actions
105 lines (92 loc) · 4.47 KB
/
Copy pathQuestionGatlingTest.java
File metadata and controls
105 lines (92 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package gatling.simulations;
import static io.gatling.javaapi.core.CoreDsl.StringBody;
import static io.gatling.javaapi.core.CoreDsl.exec;
import static io.gatling.javaapi.core.CoreDsl.rampUsers;
import static io.gatling.javaapi.core.CoreDsl.scenario;
import static io.gatling.javaapi.http.HttpDsl.header;
import static io.gatling.javaapi.http.HttpDsl.headerRegex;
import static io.gatling.javaapi.http.HttpDsl.http;
import static io.gatling.javaapi.http.HttpDsl.status;
import ch.qos.logback.classic.LoggerContext;
import io.gatling.javaapi.core.ChainBuilder;
import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.http.HttpProtocolBuilder;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import org.slf4j.LoggerFactory;
/**
* Performance test for the Question entity.
*/
public class QuestionGatlingTest extends Simulation {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
{
// Log all HTTP requests
//context.getLogger("io.gatling.http").setLevel(Level.valueOf("TRACE"));
// Log failed HTTP requests
//context.getLogger("io.gatling.http").setLevel(Level.valueOf("DEBUG"));
}
String baseURL = Optional.ofNullable(System.getProperty("baseURL")).orElse("http://localhost:8080");
HttpProtocolBuilder httpConf = http
.baseUrl(baseURL)
.inferHtmlResources()
.acceptHeader("*/*")
.acceptEncodingHeader("gzip, deflate")
.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
Map<String, String> headers_http = Map.of("Accept", "application/json");
Map<String, String> headers_http_authentication = Map.of("Content-Type", "application/json", "Accept", "application/json");
Map<String, String> headers_http_authenticated = Map.of("Accept", "application/json", "Authorization", "${access_token}");
ChainBuilder scn = exec(http("First unauthenticated request").get("/api/account").headers(headers_http).check(status().is(401)))
.exitHereIfFailed()
.pause(10)
.exec(
http("Authentication")
.post("/api/authenticate")
.headers(headers_http_authentication)
.body(StringBody("{\"username\":\"admin\", \"password\":\"admin\"}"))
.asJson()
.check(header("Authorization").saveAs("access_token"))
)
.exitHereIfFailed()
.pause(2)
.exec(http("Authenticated request").get("/api/account").headers(headers_http_authenticated).check(status().is(200)))
.pause(10)
.repeat(2)
.on(
exec(http("Get all questions").get("/api/questions").headers(headers_http_authenticated).check(status().is(200)))
.pause(Duration.ofSeconds(10), Duration.ofSeconds(20))
.exec(
http("Create new question")
.post("/api/questions")
.headers(headers_http_authenticated)
.body(
StringBody(
"{" +
"\"questionText\": null" +
", \"codeSnippet\": null" +
", \"resources\": \"SAMPLE_TEXT\"" +
", \"points\": 0" +
"}"
)
)
.asJson()
.check(status().is(201))
.check(headerRegex("Location", "(.*)").saveAs("new_question_url"))
)
.exitHereIfFailed()
.pause(10)
.repeat(5)
.on(exec(http("Get created question").get("${new_question_url}").headers(headers_http_authenticated)).pause(10))
.exec(http("Delete created question").delete("${new_question_url}").headers(headers_http_authenticated))
.pause(10)
);
ScenarioBuilder users = scenario("Test the Question entity").exec(scn);
{
setUp(users.injectOpen(rampUsers(Integer.getInteger("users", 100)).during(Duration.ofMinutes(Integer.getInteger("ramp", 1)))))
.protocols(httpConf);
}
}