@@ -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}
0 commit comments