Monitor Wicket page request using JAMon

For the Wicket webapplication I am currently working on, we build in JAMon for real time performance monitoring. As usual we intercept all calls to objects like Repositories and Services to monitor their performance. Because we use Spring we can easily intercept these Spring managed objects using Spring AOP. Besides these statistics we are also interested how long a certain page request takes. Since the Wicket components are not Spring managed we can not use Spring AOP. In this blog I´ll explain how you can put in JAMon monitoring for these page requests.

When monitoring these page requests I want to know which page was actually requested. I initially looked at the WicketFilter class as that is the entry point of your Wicket application. However I could not find a way to determine which page was actually requested there. So after a bit of investigation and confirmation from the wicket-user-group (very excellent and active user-group btw) it turned out the that the WebRequestCycle is the class we need to monitor. Wicket provides hooks which you need to override in your custom WebRequestCycle, namely onBeginRequest and onEndRequest. There is one tricky thing though: in the onBeginRequest the name of the requested Page is not yet available, so we need to store the start time of the request instead of the Monitor. In the onEndRequest we can determine which page is requested and can add the measured time to JAMon.

Here is the code of the custom WebRequestCycle:

public class JAMonMonitoredWebRequestCycle extends WebRequestCycle {

    static final String UNIT = "ms.";
    
    private long startTime;
    
    public JAMonMonitoredWebRequestCycle(WebApplication application, WebRequest request
                                                          , Response response) {
        super(application, request, response);
        this.startTime = 0;
    }
    
    @Override
    protected void onBeginRequest() {
        super.onBeginRequest();
        startTime = System.currentTimeMillis();
    }
    
    @Override
    protected void onEndRequest() {
        super.onEndRequest();
        calculateDurationAndAddToMonitor();
    }

    
    private void calculateDurationAndAddToMonitor() {
        if(startTime != 0) {
            Class pageClass = null;
            if(getWebResponse().isAjax() && getWebRequest().getPage() != null) {
                pageClass = getWebRequest().getPage().getClass();
            } else {
                pageClass = getResponsePageClass();
            }
            
            if(pageClass != null) {
                MonitorFactory.add(pageClass.toString(), UNIT, 
                                          System.currentTimeMillis() - startTime);
            }
        }
    }
    
}

Comments (2)

  1. Erik van Oosten - Reply

    September 26, 2008 at 4:47 pm

    Nice!
    Just one small remark. With this little change you also measure the duration of super.onBeginRequest:

    @Override
    protected void onBeginRequest() {
    super.onBeginRequest();
    startTime = System.currentTimeMillis();
    }

  2. PavanM - Reply

    April 20, 2009 at 3:39 pm

    Hello Jeroen,
    I was reading your article on JAMon and Jarep (http://java.dzone.com/articles/case-study-performance-tuning--0) I was quite impressed by the overall solution. Are you still using JAMon in your apps with Java5 ? Investigating on the right balance between JAMON and JMX on Java 5. Any comments ?
    Thanks,
    Pavan

Add a Comment