-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestControllerTest.java
More file actions
132 lines (120 loc) · 5.64 KB
/
RestControllerTest.java
File metadata and controls
132 lines (120 loc) · 5.64 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package echo;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RestControllerTest {
private static final String BODY = "5mb.bin";
private static final String BODY_SHA1 = "ef13a5226129bcaf68d3456c8ec0bd0f38894c53";
private static final int REQUESTS = 100;
private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(REQUESTS);
private static final HttpClient CLIENT = HttpClient.newHttpClient();
@Value("${server.port}")
private int port;
@Value("${spring.application.name}")
private String appName;
private static String url;
@BeforeAll
void setup() {
url = String.format("http://localhost:%d%s", port, "/rest/echo");
}
@Test
void get() throws Exception {
String contentType = "application/json";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", contentType)
.build();
sequential(request, contentType);
parallel(request, contentType);
}
@Test
void postBinary() throws Exception {
String contentType = "application/octet-stream";
byte[] body = RestControllerTest.class.getClassLoader().getResourceAsStream(BODY).readAllBytes();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", contentType)
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
.build();
sequential(request, contentType);
parallel(request, contentType);
}
private void sequential(HttpRequest request, String contentType) throws Exception {
AtomicLongArray statsMicro = new AtomicLongArray(REQUESTS);
for (int i = 0; i < REQUESTS; i++) {
long start = System.nanoTime();
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
long end = System.nanoTime();
assertEquals(200, response.statusCode());
if (contentType.equals("application/octet-stream")) {
assertTrue(response.body().contains(BODY_SHA1));
}
statsMicro.set(i, (end - start) / 1_000);
}
report(statsMicro, request, contentType, "Sequential");
}
private void parallel(HttpRequest request, String contentType) throws Exception {
AtomicLongArray statsMicro = new AtomicLongArray(REQUESTS);
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (int i = 0; i < REQUESTS; i++) {
int index = i;
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
long start = System.nanoTime();
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
long end = System.nanoTime();
assertEquals(200, response.statusCode());
statsMicro.set(index, (end - start) / 1_000);
} catch (Exception e) {
e.printStackTrace();
}
}, EXECUTOR);
futures.add(future);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
report(statsMicro, request, contentType, "Parallel");
}
private void report(AtomicLongArray statsMicro, HttpRequest request, String contentType, String title) {
double longestMicro = IntStream.range(0, statsMicro.length())
.mapToLong(statsMicro::get)
.max()
.orElseThrow();
double shortestMicro = IntStream.range(0, statsMicro.length())
.mapToLong(statsMicro::get)
.min()
.orElseThrow();
double averageMicro = IntStream.range(0, statsMicro.length())
.mapToLong(statsMicro::get)
.average()
.orElseThrow();
long bodySize = request.bodyPublisher()
.map(HttpRequest.BodyPublisher::contentLength)
.orElse(0L);
StringBuilder result = new StringBuilder();
result.append(String.format("### %s requests: %s%n%n", title, appName));
result.append(String.format("`%s %s %s %dMb` x %d%n%n", request.method(), request.uri().toString(), contentType, bodySize / 1_000_000, statsMicro.length()));
result.append("| Metric | Value |\n");
result.append("|----------------------|---------------|\n");
result.append(String.format("| Longest response | %.2f ms |%n", longestMicro / 1_000));
result.append(String.format("| Shortest response | %.2f ms |%n", shortestMicro / 1_000));
result.append(String.format("| Average response | %.2f ms |%n", averageMicro / 1_000));
System.out.println(result);
}
}