I realize it's a chicken and egg problem and that it's not possible to accurately resolve the time it took to render a page (or the size of response) and insert that number into the page itself without affecting either measure. Nevertheless, I'm looking for a way to insert either number partially in a page of a JSF/Facelets/Seam application.
E.g., at the bottom of a .jsf page somewhere:
<!-- page size: 10.3Kb -->
<!-- render time: 0.2s -->
I've come across JSFUnit's JSFTimer, which is really handy. However, the phase listener approach doesn't allow the results of RENDER_RESPONSE phase to be inserted into the page. Not sure how to access the size of the response encoded so far either.
Is there a quick and dirty way to hook up to some sort of post-processing event at or after the end of RENDER_RESPONSE and to inject both numbers into the page about to be rendered? One way of approaching this is perhaps through servlet filters, but I'm looking for something simpler; perhaps a trick with Seam or Facelets...
Thanks,
-A
Or have an asynch Javascript call that gets the response time and size from the server after its ready? Treat it like a callback to be performed after the page is done loading and the values are ready to insert.
I wrote a blog post explaining how you could create an Interceptor that would measure each method call your seam compontents where using.
You can find the blog post here. You need to scroll down to the second part.
Basically, all you need to do is annotate the method you want to measure with
@MeasureCalls
and it will automatically be picked up by the interceptorAn output would be something like this, showing the time it took in milliseconds and how many times each method was called:
This is a perfect use case for the Apache Commons IO
CountingOutputStream
. You need to create aFilter
which usesHttpServletResponseWrapper
to replace theOutputStream
of the response with this one and replaces theWriter
as well which should wrap the wrappedOutputStream
. Then get hold of theHttpServletResponseWrapper
instance in the request scope so that you can get thegetByteCount()
from theCountingOutputStream
.Here's a kickoff example of the
CountingFilter
:The
CountingServletResponse
:The
CountingServletOutputStream
:You can use it in any (even non-JSF) page as follows: