  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | performance problem with ServletOutputStream.write() | performance problem with ServletOutputStream.write() 2005-01-17 - By Jean-Christian Imbeault
Back I previously asked the list about a performance problem I had while reading files of disk and then streaming them to clients.
I didn't notice there was a 'problem' until I compare the time it takes our application to stream the file to our users vs. the time it takes out application server (acting a plain web server) to serve up the same file. Streaming takes about 5x longer!
Many people suggested that I read and write the file from the presentation layer servlet in order to save on objects, memory, etc ...
I did this and to my surprise I barely saw any improvements at all in the speed at which the pdf file is streamed. I added lots of 'debugging' code to find out where the bottleneck was and was shocked to see that almost all the time spent (about 90%) is when I sent the file back to the client by calling ServletOutputStream.write().
I'm pretty much out of ideas on how to 'fix' this since there really is no other to send something to the client than to write() to the OuputStream as far as I know.
The code basically boils down to:
read file off disk using BufferedInputStream write file using ServletOutputStream.write()
That's it. Nothing else. I can't see any way to improve this code.
Can someone shed light on why streaming is so much slower at transmitting files than a 'regular' web server?
Is there anything I can do to improve my situation?
Thanks!
Sample code and output from a test case is included below:
ServletOutputStream out = res.getOutputStream(); buff = new byte[fileLength];
startDate = new Date(); System.out.println("PERF: " + userIDForPDFPerformance + " :FiDServlet:doPost\t-BEGIN :" + startDate + " *** CASE 5 READ ***");
bis.read(buff, 0, buff.length); endDate = new Date(); System.out.println("PERF: " + userIDForPDFPerformance + " :FiDServlet:doPost\t-END :" + endDate + " Time Diff:" + (endDate.getTime() - startDate.getTime()) / 1000.0 + " *** CASE 5 READ ***");
startDate = new Date(); System.out.println("PERF: " + userIDForPDFPerformance + " :FiDServlet:doPost\t-BEGIN :" + startDate + " *** CASE 5 WRITE ***"); out.write(buff, 0, buff.length); endDate = new Date(); System.out.println("PERF: " + userIDForPDFPerformance + " :FiDServlet:doPost\t-END :" + endDate + " Time Diff:" + (endDate.getTime() - startDate.getTime()) / 1000.0 + " *** CASE 5 WRITE ***");
==SAMPLE OUTPUT==
PERF: -121*35*-3 :FiDServlet:doPost -BEGIN :Mon Jan 17 16:34:46 JST 2005 *** CASE 5 READ *** PERF: -121*35*-3 :FiDServlet:doPost -END :Mon Jan 17 16:34:46 JST 2005 Time Diff:0.0050 *** CASE 5 READ *** PERF: -121*35*-3 :FiDServlet:doPost -BEGIN :Mon Jan 17 16:34:46 JST 2005 *** CASE 5 WRITE *** PERF: -121*35*-3 :FiDServlet:doPost -END :Mon Jan 17 16:34:53 JST 2005 Time Diff:6.739 *** CASE 5 WRITE ***
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|
 |