2929import org .junit .Test ;
3030
3131import org .apache .catalina .Context ;
32- import org .apache .catalina .Wrapper ;
3332import org .apache .catalina .startup .Tomcat ;
3433import org .apache .catalina .startup .TomcatBaseTest ;
3534import org .apache .tomcat .util .buf .ByteChunk ;
@@ -41,45 +40,53 @@ public void testWriteSpeed() throws Exception {
4140 Tomcat tomcat = getTomcatInstance ();
4241
4342 Context root = tomcat .addContext ("" , TEMP_DIR );
44- SingleCharWritingServlet servlet = new SingleCharWritingServlet ();
45- Wrapper w =
46- Tomcat .addServlet (root , "singleCharWritingServlet" , servlet );
47- w .setAsyncSupported (true );
48- root .addServletMapping ("/" , "singleCharWritingServlet" );
43+
44+ for (int i = 1 ; i <= WritingServlet .EXPECTED_CONTENT_LENGTH ; i *=10 ) {
45+ WritingServlet servlet = new WritingServlet (i );
46+ Tomcat .addServlet (root , "servlet" + i , servlet );
47+ root .addServletMapping ("/servlet" + i , "servlet" + i );
48+ }
4949
5050 tomcat .start ();
5151
5252 ByteChunk bc = new ByteChunk ();
53- int rc = getUrl ("http://localhost:" + getPort () + "/" , bc , null , null );
54-
55- assertEquals (200 , rc );
56- assertEquals (SingleCharWritingServlet .ITERATIONS , bc .getLength ());
57-
58- long noBuffering = servlet .getLastRunNano ();
5953
60- System .out .println (noBuffering );
54+ for (int i = 1 ; i <= WritingServlet .EXPECTED_CONTENT_LENGTH ; i *=10 ) {
55+ int rc = getUrl ("http://localhost:" + getPort () +
56+ "/servlet" + i , bc , null , null );
57+ assertEquals (HttpServletResponse .SC_OK , rc );
58+ assertEquals (
59+ WritingServlet .EXPECTED_CONTENT_LENGTH , bc .getLength ());
6160
62- bc .recycle ();
61+ bc .recycle ();
6362
64- rc = getUrl ("http://localhost:" + getPort () + "/?useBuffering=y" , bc ,
65- null , null );
63+ rc = getUrl ("http://localhost:" + getPort () +
64+ "/servlet" + i + "?useBuffer=y" , bc , null , null );
65+ assertEquals (HttpServletResponse .SC_OK , rc );
66+ assertEquals (
67+ WritingServlet .EXPECTED_CONTENT_LENGTH , bc .getLength ());
6668
67- assertEquals (200 , rc );
68- assertEquals (SingleCharWritingServlet .ITERATIONS , bc .getLength ());
69-
70- long buffering = servlet .getLastRunNano ();
71-
72- System .out .println (buffering );
69+ bc .recycle ();
70+ }
7371 }
7472
75- private static final class SingleCharWritingServlet extends HttpServlet {
73+ private static class WritingServlet extends HttpServlet {
7674
7775 private static final long serialVersionUID = 1L ;
7876
79- protected static final int ITERATIONS = 100000 ;
77+ protected static final int EXPECTED_CONTENT_LENGTH = 100000 ;
8078
81- // Not thread safe but will only be used with single calls.
82- private volatile long lastRunNano = 0 ;
79+ private final String writeString ;
80+ private final int writeCount ;
81+
82+ public WritingServlet (int writeLength ) {
83+ StringBuilder sb = new StringBuilder ();
84+ for (int i = 0 ; i < writeLength ; i ++) {
85+ sb .append ('x' );
86+ }
87+ writeString = sb .toString ();
88+ writeCount = EXPECTED_CONTENT_LENGTH / writeLength ;
89+ }
8390
8491 @ Override
8592 protected void doGet (HttpServletRequest req , HttpServletResponse resp )
@@ -97,20 +104,17 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
97104 }
98105
99106 long start = System .nanoTime ();
100-
101- for (int i = 0 ; i < ITERATIONS ; i ++) {
102- w .write ('x' );
107+ for (int i = 0 ; i < writeCount ; i ++) {
108+ w .write (writeString );
103109 }
104-
105- lastRunNano = System .nanoTime () - start ;
106-
107110 if (useBufferStr != null ) {
108111 w .flush ();
109112 }
110- }
113+ long lastRunNano = System . nanoTime () - start ;
111114
112- public long getLastRunNano () {
113- return lastRunNano ;
115+ System .out .println ("Write length: " + writeString .length () +
116+ ", Buffered: " + (useBufferStr == null ? "n" : "y" ) +
117+ ", Time: " + lastRunNano + "ns" );
114118 }
115119 }
116120}
0 commit comments