/
HttpServerWorkloadBenchmark.java
195 lines (159 loc) · 5.47 KB
/
HttpServerWorkloadBenchmark.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package io.activej.http;
import io.activej.async.callback.Callback;
import io.activej.config.Config;
import io.activej.config.ConfigModule;
import io.activej.eventloop.Eventloop;
import io.activej.inject.annotation.Inject;
import io.activej.inject.annotation.Named;
import io.activej.inject.annotation.Provides;
import io.activej.inject.module.Module;
import io.activej.launcher.Launcher;
import io.activej.promise.Promise;
import io.activej.promise.SettablePromise;
import io.activej.service.ServiceGraphModule;
import org.jetbrains.annotations.Nullable;
import java.time.Duration;
import static io.activej.config.converter.ConfigConverters.*;
import static io.activej.inject.module.Modules.combine;
import static java.lang.Math.min;
public class HttpServerWorkloadBenchmark extends Launcher {
private static final int KEEP_ALIVE = 30;
private static final int TOTAL_REQUESTS = 1_000_000;
private static final int WARMUP_ROUNDS = 3;
private static final int BENCHMARK_ROUNDS = 5;
private static final int ACTIVE_REQUESTS_MAX = 300;
private static final int ACTIVE_REQUESTS_MIN = 200;
private String address;
private int totalRequests;
private int warmupRounds;
private int measureRounds;
private int activeRequestsMax;
private int activeRequestsMin;
@Provides
@Named("server")
Eventloop serverEventloop() { return Eventloop.create(); }
@Provides
@Named("client")
Eventloop clientEventloop() { return Eventloop.create(); }
@Inject
@Named("server")
Eventloop serverEventloop;
@Inject
@Named("client")
Eventloop clientEventloop;
@Inject
Config config;
@Inject
AsyncHttpServer server;
@Inject
AsyncHttpClient client;
@Provides
AsyncHttpServer server() {
return AsyncHttpServer.create(serverEventloop,
request ->
HttpResponse.ok200().withPlainText("Response!!"))
.withListenAddresses(config.get(ofList(ofInetSocketAddress()), "address"));
}
@Provides
AsyncHttpClient client() {
return AsyncHttpClient.create(clientEventloop)
.withKeepAliveTimeout(Duration.ofSeconds(config.get(ofInteger(),
"client.keepAlive", KEEP_ALIVE)));
}
@Provides
Config config() {
return Config.create()
.with("address", "0.0.0.0:9001")
.with("client.address", "http://127.0.0.1:9001/")
.overrideWith(Config.ofSystemProperties("config"));
}
@Override
protected Module getModule() {
return combine(
ServiceGraphModule.create(),
ConfigModule.create()
.withEffectiveConfigLogger());
}
@Override
protected void onStart() {
this.address = config.get("client.address");
this.totalRequests = config.get(ofInteger(), "benchmark.totalRequests", TOTAL_REQUESTS);
this.warmupRounds = config.get(ofInteger(), "benchmark.warmupRounds", WARMUP_ROUNDS);
this.measureRounds = config.get(ofInteger(), "benchmark.measureRounds", BENCHMARK_ROUNDS);
this.activeRequestsMax = config.get(ofInteger(), "benchmark.activeRequestsMax", ACTIVE_REQUESTS_MAX);
this.activeRequestsMin = config.get(ofInteger(), "benchmark.activeRequestsMin", ACTIVE_REQUESTS_MIN);
}
@Override
protected void run() throws Exception {
long timeAllRounds = 0;
long bestTime = -1;
long worstTime = -1;
System.out.println("Warming up ...");
for (int i = 0; i < warmupRounds; i++) {
long roundTime = round();
long rps = totalRequests * 1000L / roundTime;
System.out.println("Round: " + (i + 1) + "; Round time: " + roundTime + "ms; RPS : " + rps);
}
System.out.println("Start benchmarking GET Request");
for (int i = 0; i < measureRounds; i++) {
long roundTime = round();
timeAllRounds += roundTime;
if (bestTime == -1 || roundTime < bestTime) {
bestTime = roundTime;
}
if (worstTime == -1 || roundTime > worstTime) {
worstTime = roundTime;
}
long rps = totalRequests * 1000L / roundTime;
System.out.println("Round: " + (i + 1) + "; Round time: " + roundTime + " ms; RPS : " + rps);
}
double avgTime = (double) timeAllRounds / measureRounds;
long requestsPerSecond = (long) (totalRequests / avgTime * 1000);
System.out.println("Time: " + timeAllRounds + "ms; Average time: " + avgTime + " ms; Best time: " +
bestTime + "ms; Worst time: " + worstTime + "ms; Requests per second: " + requestsPerSecond);
}
private long round() throws Exception {
return clientEventloop.submit(this::roundGet).get();
}
int sent;
int completed;
private Promise<Long> roundGet() {
SettablePromise<Long> promise = new SettablePromise<>();
Callback<HttpResponse> callback = new Callback<HttpResponse>() {
@Override
public void accept(HttpResponse result, @Nullable Throwable e) {
completed++;
int active = sent - completed;
if (e != null) {
promise.setException(new FailedRequestException());
return;
}
if (completed == totalRequests) {
promise.set(null);
return;
}
if (active <= activeRequestsMin) {
for (int i = 0; i < min(activeRequestsMax - active, totalRequests - sent); i++, sent++) {
doGet(this);
}
}
}
};
sent = 0;
completed = 0;
long start = System.currentTimeMillis();
for (int i = 0; i < min(activeRequestsMin, totalRequests); i++) {
doGet(callback);
sent++;
}
return promise.map($ -> System.currentTimeMillis() - start);
}
private void doGet(Callback<HttpResponse> callback) {
client.request(HttpRequest.get(address)).whenComplete(callback);
}
public static void main(String[] args) throws Exception {
Launcher benchmark = new HttpServerWorkloadBenchmark();
benchmark.launch(args);
}
private static class FailedRequestException extends Exception {}
}