Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8009548

HttpServer and URLConnection introduce a 38 ms delay when using keep-alive

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version " 1.7.0_15 "
      Java(TM) SE Runtime Environment (build 1.7.0_15-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

      or

      java version " 1.6.0_27 "
      OpenJDK Runtime Environment (IcedTea6 1.12.3) (6b27-1.12.3-0ubuntu1~12.04.1)
      OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux i5 3.2.0-38-generic #61-Ubuntu SMP Tue Feb 19 12:18:21 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      HttpServer and URLConnection introduce a 38 ms delay when using keep-alive.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /**
       * @test
       * @bug
       * @summary pipelining delay on Ubuntu 12.04.01 LTS / amd64
       */

      import com.sun.net.httpserver.*;

      import java.util.*;
      import java.util.concurrent.*;
      import java.io.*;
      import java.net.*;

      public class Bug {

          static int iterations = 20;
          static long requiredMinimumDelay = 10L;
          
          public static void main (String[] args) throws Exception {
              Handler handler = new Handler();
              InetSocketAddress addr = new InetSocketAddress (8075);
              HttpServer server = HttpServer.create (addr, 0);
              HttpContext ctx = server.createContext ( " /test " , handler);
              ExecutorService executor = Executors.newCachedThreadPool();
              server.setExecutor (executor);
              server.start ();

              long minDelay = requiredMinimumDelay * 1000L;
              
              try {
                  for(int i = 0; i < iterations; i++) {
                      URL url = new URL ( " http://localhost: " +server.getAddress().getPort()+ " /test/foo.html " );
                      HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
                      InputStream is = urlc.getInputStream();
                      InputStreamReader isr = new InputStreamReader(is);
                      BufferedReader br = new BufferedReader(isr);
                      String res = br.readLine();
                      br.close();
                      
                      // skip first few
                      if(i < iterations/2) {
                          continue;
                      }
                      
                      long delay = System.currentTimeMillis() - Long.parseLong(res);
                      System.out.println( " delay: " +delay+ " ms " );
                      if(delay < minDelay) {
                          minDelay = delay;
                      }
                  }
              } catch (Exception ex) {}
              
              server.stop(2);
              executor.shutdown();
              
              if(minDelay > requiredMinimumDelay) {
                  throw new Exception( " minimum delay too large: " +minDelay);
              }
          }

          static class Handler implements HttpHandler {
              public void handle (HttpExchange t)
                  throws IOException
              {
                  InputStream is = t.getRequestBody();
                  Headers map = t.getRequestHeaders();
                  Headers rmap = t.getResponseHeaders();
                  while (is.read () != -1) ;
                  is.close();
                  String response = Long.toString(System.currentTimeMillis())+ "
       " ;
                  t.sendResponseHeaders (200, response.length());
                  OutputStream os = t.getResponseBody();
                  os.write (response.getBytes());
                  t.close();
              }
          }
      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Connection: close

            khazra Kurchi Subhra Hazra
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: