The Source for Java Technology Collaboration
User: Password:



Jayson Falkner's Blog

January 2004 Archives


Blarg #7: How long does it takes to compress a response using a GZIP filter?

Posted by jfalkner on January 27, 2004 at 09:13 PM | Permalink | Comments (1)

Hi, just found out about your book the other day from java.net and was really grateful for the example of the Compression code. I've had to implement that in the past on the web server level and it was a real pain then, but this was relatively effortless. I'm adding this book to my to-buy list.

I do have a question. Perhaps if others are interested some sort of improvement could be integrated. Where would we add output messages that would help us with the timing of the compression?

i.e.If I put something like this in GZipFilter:

                 System.out.println("GZIP supported, compressing.");
                 long ini = System.currentTimeMillis();
                 GZIPResponseWrapper wrappedResponse = new
 GZIPResponseWrapper(response);
                 chain.doFilter(req, wrappedResponse);
                 wrappedResponse.finishResponse();
                 System.out.println("Compressed in " +
 (System.currentTimeMillis()-ini) + " ms");

I get the total time for the entire request, not just the time it takes to do the compression. >From testing I realize the compression time is pretty short, say in the order of tens of microseconds, but I'm still pretty interested. I was thinking that moving the time initializer from before the response constructor to before the finishResponse method call would work, but I wasn't sure if that would get the total compression time, or just the time for the last output to be flushed from the response buffer.

Thanks,
Ronald

Hi Ronald,

Awesome. I'm glad you found my book, and I hope you enjoy it.

The trick here is to split the request and compression in to two separate things. Buffer the response, then compress the buffer and time only the compression. You can more or less do this easily by editing the code in the com.jspbook.GZIPResponseStream class, e.g. here is the existing code in the close() method of GZIPResponseStream.

ByteArrayOutputStream baos = (ByteArrayOutputStream)bufferedOutput;
// prepare a gzip stream
ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
byte[] bytes = baos.toByteArray();
gzipstream.write(bytes);
gzipstream.finish();
// get the compressed content
byte[] compressedBytes = compressedContent.toByteArray();

You can see the response gets put in to a byte array, then the byte array is compressed and the results are saved to another byte array. You could put a time check in as follows (very similar to what you were doing).

ByteArrayOutputStream baos = (ByteArrayOutputStream)bufferedOutput;
// prepare a gzip stream
ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
long ini = System.currentTimeMillis(); // <------- starting timing here
GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
byte[] bytes = baos.toByteArray();
gzipstream.write(bytes);
gzipstream.finish();
// get the compressed content
byte[] compressedBytes = compressedContent.toByteArray();
System.out.println("Compressed in " + (System.currentTimeMillis()-ini) +
" ms"); // <-------------- stop timing here

And the result is you are only counting how long the compression took. Although fair warning needs to be given. The current GZIPResponseStream class tries to buffer content, as shown above (it allows you to set the content-length header properly), but when compressing really big files the filter streams content back to the client as not to use up all of your web application's memory. The above timing example won't work if you are testing it on files larger than the buffer -- the default buffer is 50k, you may need to increase it for your benchmark.

In general, the resources it takes to compress content aren't an issue. However, if you find that compression takes up too much processing power, remember you can combine compression with caching to try and reduce the overhead.

Cheers, Jayson Falkner jayson@jspinsider.com

A few links



Blarg #6: How do I become an expert Java developer?

Posted by jfalkner on January 27, 2004 at 08:11 AM | Permalink | Comments (4)

Hi,
I'm a young java programmer(for now) and on the brink of writing my SCJP1.4 exam I want to be a Java Developer(J2EE) more than anything else. I've picked up REALLY REALLY bad coding conventions and norms along my path to Java-enlightenment and I would like to know from YOU, what could I do to turn this around and become a precision Java Developer, Are there Rules and Guidelines (in ENGLISH) that I can follow that will set me apart as a "developer of note"?

Please Advise
Lloyd

Hi Lloyd, depending on who you ask there are many different answers to this question. However, a great way to get started is by reading the Java coding conventions Sun encourages -- most all Java code you see will roughly follow these. These conventions fall along the lines of standard Java documentation, which is the next place you should look when seeking advice about Java. As a rule of thumb, check Java documentation first and goggle second.

If you really want to practice coding or look at a bunch of "good" code, try hacking away at your favorite open-source project (I'd suggest Tomcat, http://jakarta.apache.org/tomcat, or JBoss, http://jboss.org). You'll find lots of code that will show how general Java code usually looks, and you will also be able to see lots of examples of clever coding tricks. Plus, if you can really understand what is going on in a decent sized open-source project, odds are your Java skills are up to snuff.

Some Links

Jayson Falkner
jayson@jspinsider.com
http://www.jspbook.com



Blarg #5: Are you aware that the source-code for www.jspbook.com isn't available?

Posted by jfalkner on January 25, 2004 at 09:22 PM | Permalink | Comments (0)

Hi Jayson,
Are you aware that the source-code for www.jspbook.com isn't available on-line? How can I get a copy?

I get asked the above question almost daily, and people are fairly asking it. The short answer is http://www.jspbook.com/jspbook_site.war. And if www.jspbook.com is replaced with www.jspinsider.com, the answer is http://www.jspinsider.com/showsource.jsp.

The reason I posted this on java.net is because I think it will help others find the code without needing the middle man (me), and I realized maybe other JSP/Servlet developers would like something in addition to the source-code.

Let me explain. As a JSP/Servlet developer all of the code I write I try to make freely available for others to use as they wish. The goal is to promote the use of helpful Servlet/JSP/Java code. I think it is silly to expect others to learn when all they have is the API documentation. I wrote a book with this in mind, and one of the book's examples is building a real website (http://www.jspbook.com), which also funtions as the book support site. Although sadly, roughly a person a day can't figure out where the link is to the source-code for the site, and that I assume is the reason for all the e-mail I get with the above two lines. Hopefull this post will help reduce the average to at least a person per week.

About the other bit. I recently realized that putting source-code on-line by itself isn't overly helpful to people who don't buy the book that goes with it. I've decided to try and write up some nice, free on-line guides to why the source-code I provide is helpful. The idea is that I can demonstrate some code, say a compression filter, give you the source-code, and leave you feeling good because you've got something cool that you can hack away at as you please. Give me some ideas; what would you like?

Please direct suggestions to the feedback for this blarg, and I'll take the most popular ideas and make something of them in the weeks to come.





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds