Skip to content

Commit d69682c

Browse files
committed
Working
1 parent 8a22c0c commit d69682c

File tree

3 files changed

+125
-82
lines changed

3 files changed

+125
-82
lines changed

load-tests/self/src/main/java/io/fusionauth/http/load/LoadHandler.java

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Base64;
2323
import java.util.HashMap;
2424
import java.util.Map;
25-
import java.util.concurrent.ConcurrentHashMap;
2625

2726
import io.fusionauth.http.server.HTTPHandler;
2827
import io.fusionauth.http.server.HTTPRequest;
@@ -34,7 +33,8 @@ public class LoadHandler implements HTTPHandler {
3433
@Override
3534
public void handle(HTTPRequest req, HTTPResponse res) {
3635
switch (req.getPath()) {
37-
case "/" -> handleNoOp();
36+
case "/" -> handleNoOp(req, res);
37+
case "/no-read" -> handleNoRead(req, res);
3838
case "/hello" -> handleHello(req, res);
3939
case "/file" -> handleFile(req, res);
4040
case "/load" -> handleLoad(req, res);
@@ -56,51 +56,60 @@ private void handleFailure(HTTPRequest req, HTTPResponse res) {
5656
}
5757

5858
private void handleFile(HTTPRequest req, HTTPResponse res) {
59-
int size = 1024 * 1024;
60-
var requestedSize = req.getURLParameter("size");
61-
if (requestedSize != null) {
62-
size = Integer.parseInt(requestedSize);
63-
}
59+
try (InputStream is = req.getInputStream()) {
60+
// Empty the InputStream
61+
is.readAllBytes();
62+
63+
int size = 1024 * 1024;
64+
var requestedSize = req.getURLParameter("size");
65+
if (requestedSize != null) {
66+
size = Integer.parseInt(requestedSize);
67+
}
6468

65-
// Ensure we only build one file
66-
byte[] blob = Blobs.get(size);
67-
if (blob == null) {
68-
synchronized (Blobs) {
69-
blob = Blobs.get(size);
70-
if (blob == null) {
71-
System.out.println("Build file with size : " + size);
72-
String s = "Lorem ipsum dolor sit amet";
73-
String body = s.repeat(size / s.length() + (size % s.length()));
74-
assert body.length() == size;
75-
Blobs.put(size, body.getBytes(StandardCharsets.UTF_8));
69+
// Ensure we only build one file
70+
byte[] blob = Blobs.get(size);
71+
if (blob == null) {
72+
synchronized (Blobs) {
7673
blob = Blobs.get(size);
77-
assert blob != null;
74+
if (blob == null) {
75+
System.out.println("Build file with size : " + size);
76+
String s = "Lorem ipsum dolor sit amet";
77+
String body = s.repeat(size / s.length() + (size % s.length()));
78+
assert body.length() == size;
79+
Blobs.put(size, body.getBytes(StandardCharsets.UTF_8));
80+
blob = Blobs.get(size);
81+
assert blob != null;
82+
}
7883
}
7984
}
80-
}
8185

82-
res.setStatus(200);
83-
res.setContentType("application/octet-stream");
84-
res.setContentLength(blob.length);
86+
res.setStatus(200);
87+
res.setContentType("application/octet-stream");
88+
res.setContentLength(blob.length);
8589

86-
try (OutputStream os = res.getOutputStream()) {
87-
os.write(blob);
88-
os.flush();
90+
try (OutputStream os = res.getOutputStream()) {
91+
os.write(blob);
92+
}
8993
} catch (Exception e) {
9094
res.setStatus(500);
9195
}
9296
}
9397

9498
private void handleHello(HTTPRequest req, HTTPResponse res) {
95-
// Hello world
96-
res.setStatus(200);
97-
res.setContentType("text/plain");
98-
byte[] response = "Hello world".getBytes(StandardCharsets.UTF_8);
99-
res.setContentLength(response.length);
99+
// Note that it is intentionally that we are not reading the InputStream. This will cause the server to have to drain it.
100+
try (InputStream is = req.getInputStream()) {
101+
// Empty the InputStream
102+
is.readAllBytes();
100103

101-
try (OutputStream os = res.getOutputStream()) {
102-
os.write(response);
103-
os.flush();
104+
// Hello world
105+
res.setStatus(200);
106+
res.setContentType("text/plain");
107+
byte[] response = "Hello world".getBytes(StandardCharsets.UTF_8);
108+
res.setContentLength(response.length);
109+
110+
try (OutputStream os = res.getOutputStream()) {
111+
os.write(response);
112+
}
104113
} catch (Exception e) {
105114
res.setStatus(500);
106115
}
@@ -121,6 +130,20 @@ private void handleLoad(HTTPRequest req, HTTPResponse res) {
121130
}
122131
}
123132

124-
private void handleNoOp() {
133+
private void handleNoOp(HTTPRequest req, HTTPResponse res) {
134+
// Note that it is intentionally that we are not reading the InputStream. This will cause the server to have to drain it.
135+
try (InputStream is = req.getInputStream()) {
136+
// Just read the bytes from the InputStream and return. Do no other worker.
137+
is.readAllBytes();
138+
res.setStatus(200);
139+
} catch (Exception e) {
140+
res.setStatus(500);
141+
}
142+
}
143+
144+
@SuppressWarnings("unused")
145+
private void handleNoRead(HTTPRequest req, HTTPResponse res) {
146+
// Note that it is intentionally that we are not reading the InputStream. This will cause the server to have to drain it.
147+
res.setStatus(200);
125148
}
126149
}

load-tests/tomcat/src/main/java/io/fusionauth/http/load/LoadServlet.java

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,13 @@ protected void doPost(HttpServletRequest req, HttpServletResponse res) {
3939
// Note that this should be mostly the same between all load tests.
4040
// - See load-tests/self
4141
switch (req.getPathInfo()) {
42-
case "/" -> handleNoOp();
42+
case "/" -> handleNoOp(req, res);
43+
case "/no-read" -> handleNoRead(req, res);
4344
case "/hello" -> handleHello(req, res);
4445
case "/file" -> handleFile(req, res);
4546
case "/load" -> handleLoad(req, res);
4647
default -> handleFailure(req, res);
4748
}
48-
49-
50-
try (InputStream is = req.getInputStream()) {
51-
byte[] body = is.readAllBytes();
52-
byte[] result = Base64.getEncoder().encode(body);
53-
res.setStatus(200);
54-
res.setContentType("text/plain");
55-
res.setContentLength(result.length);
56-
res.getOutputStream().write(result);
57-
} catch (Exception e) {
58-
res.setStatus(500);
59-
}
6049
}
6150

6251
private void handleFailure(HttpServletRequest req, HttpServletResponse res) {
@@ -73,51 +62,60 @@ private void handleFailure(HttpServletRequest req, HttpServletResponse res) {
7362
}
7463

7564
private void handleFile(HttpServletRequest req, HttpServletResponse res) {
76-
int size = 1024 * 1024;
77-
var requestedSize = req.getParameter("size");
78-
if (requestedSize != null) {
79-
size = Integer.parseInt(requestedSize);
80-
}
65+
// Note that it is intentionally that we are not reading the InputStream. This will cause the server to have to drain it.
66+
try (InputStream is = req.getInputStream()) {
67+
// Empty the InputStream
68+
is.readAllBytes();
8169

82-
// Ensure we only build one file
83-
byte[] blob = Blobs.get(size);
84-
if (blob == null) {
85-
synchronized (Blobs) {
86-
blob = Blobs.get(size);
87-
if (blob == null) {
88-
System.out.println("Build file with size : " + size);
89-
String s = "Lorem ipsum dolor sit amet";
90-
String body = s.repeat(size / s.length() + (size % s.length()));
91-
assert body.length() == size;
92-
Blobs.put(size, body.getBytes(StandardCharsets.UTF_8));
70+
int size = 1024 * 1024;
71+
var requestedSize = req.getParameter("size");
72+
if (requestedSize != null) {
73+
size = Integer.parseInt(requestedSize);
74+
}
75+
76+
// Ensure we only build one file
77+
byte[] blob = Blobs.get(size);
78+
if (blob == null) {
79+
synchronized (Blobs) {
9380
blob = Blobs.get(size);
94-
assert blob != null;
81+
if (blob == null) {
82+
System.out.println("Build file with size : " + size);
83+
String s = "Lorem ipsum dolor sit amet";
84+
String body = s.repeat(size / s.length() + (size % s.length()));
85+
assert body.length() == size;
86+
Blobs.put(size, body.getBytes(StandardCharsets.UTF_8));
87+
blob = Blobs.get(size);
88+
assert blob != null;
89+
}
9590
}
9691
}
97-
}
9892

99-
res.setStatus(200);
100-
res.setContentType("application/octet-stream");
101-
res.setContentLength(blob.length);
93+
res.setStatus(200);
94+
res.setContentType("application/octet-stream");
95+
res.setContentLength(blob.length);
10296

103-
try (OutputStream os = res.getOutputStream()) {
104-
os.write(blob);
105-
os.flush();
97+
try (OutputStream os = res.getOutputStream()) {
98+
os.write(blob);
99+
}
106100
} catch (Exception e) {
107101
res.setStatus(500);
108102
}
109103
}
110104

111105
private void handleHello(HttpServletRequest req, HttpServletResponse res) {
112-
// Hello world
113-
res.setStatus(200);
114-
res.setContentType("text/plain");
115-
byte[] response = "Hello world".getBytes(StandardCharsets.UTF_8);
116-
res.setContentLength(response.length);
106+
try (InputStream is = req.getInputStream()) {
107+
// Empty the InputStream
108+
is.readAllBytes();
109+
110+
// Hello world
111+
res.setStatus(200);
112+
res.setContentType("text/plain");
113+
byte[] response = "Hello world".getBytes(StandardCharsets.UTF_8);
114+
res.setContentLength(response.length);
117115

118-
try (OutputStream os = res.getOutputStream()) {
119-
os.write(response);
120-
os.flush();
116+
try (OutputStream os = res.getOutputStream()) {
117+
os.write(response);
118+
}
121119
} catch (Exception e) {
122120
res.setStatus(500);
123121
}
@@ -138,6 +136,20 @@ private void handleLoad(HttpServletRequest req, HttpServletResponse res) {
138136
}
139137
}
140138

141-
private void handleNoOp() {
139+
private void handleNoOp(HttpServletRequest req, HttpServletResponse res) {
140+
// Note that it is intentionally that we are not reading the InputStream. This will cause the server to have to drain it.
141+
try (InputStream is = req.getInputStream()) {
142+
// Just read the bytes from the InputStream and return. Do no other worker.
143+
is.readAllBytes();
144+
res.setStatus(200);
145+
} catch (Exception e) {
146+
res.setStatus(500);
147+
}
148+
}
149+
150+
@SuppressWarnings("unused")
151+
private void handleNoRead(HttpServletRequest req, HttpServletResponse res) {
152+
// Note that it is intentionally that we are not reading the InputStream. This will cause the server to have to drain it.
153+
res.setStatus(200);
142154
}
143155
}

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@
7474
<dependency>
7575
<groupId>org.testng</groupId>
7676
<artifactId>testng</artifactId>
77-
<version>7.10.2</version>
77+
<version>7.11.0</version>
78+
<type>jar</type>
79+
<scope>test</scope>
80+
<optional>false</optional>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.slf4j</groupId>
84+
<artifactId>slf4j-nop</artifactId>
85+
<version>2.0.17</version>
7886
<type>jar</type>
7987
<scope>test</scope>
8088
<optional>false</optional>

0 commit comments

Comments
 (0)