{"id":295,"date":"2009-06-16T22:55:04","date_gmt":"2009-06-17T03:55:04","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=295"},"modified":"2010-07-27T22:32:05","modified_gmt":"2010-07-28T03:32:05","slug":"timing-jsf-requests-using-a-phase-listener","status":"publish","type":"post","link":"http:\/\/www.andygibson.net\/blog\/tutorial\/timing-jsf-requests-using-a-phase-listener\/","title":{"rendered":"Timing JSF Requests Using a Phase Listener"},"content":{"rendered":"<p>One easy way of determining how long it takes to return a page from Seam, and\/or JSF is to use a phase listener. This phase listener below  logs the end of each phase of the JSF lifecycle and measures the time from the start of the <code>RESTORE_VIEW<\/code> phase (the first phase in the lifecycle and the start of the request) to the end of the <code>RENDER_RESPONSE<\/code> phase which is the last one. Logging the end of each stage of the cycle lets you see what else is going on during each phase of the cycle. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LogPhaseListener implements PhaseListener {\r\n\r\n\tpublic long startTime;\r\n\r\n\tprivate static final LogProvider log = Logging\r\n\t\t\t.getLogProvider(LogPhaseListener.class);\r\n\r\n\tpublic void afterPhase(PhaseEvent event) {\r\n\t\tif (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {\r\n\t\t\tlong endTime = System.nanoTime();\r\n\t\t\tlong diffMs = (long) ((endTime - startTime) * 0.000001);\r\n\t\t\tif (log.isDebugEnabled()) {\r\n\t\t\t\tlog.debug(&quot;Execution Time = &quot; + diffMs + &quot;ms&quot;);\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (log.isDebugEnabled()) {\r\n\t\t\tlog.debug(&quot;Executed Phase &quot; + event.getPhaseId());\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void beforePhase(PhaseEvent event) {\r\n\r\n\t\tif (event.getPhaseId() == PhaseId.RESTORE_VIEW) {\r\n\t\t\tstartTime = System.nanoTime();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic PhaseId getPhaseId() {\r\n\t\treturn PhaseId.ANY_PHASE;\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n<p>To use this, simply add the class to your project and insert a new phase listener in the <code>faces-config.xml<\/code> file.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n  &lt;lifecycle&gt;\r\n    &lt;phase-listener&gt;package.name.LogPhaseListener&lt;\/phase-listener&gt;\r\n  &lt;\/lifecycle&gt;\r\n<\/pre>\n<p>While it may not be totally accurate, it at least gives you an idea of the scale of the duration of a request (i.e. 68ms versus 394ms). I&#8217;ve used this fairly effectively in a few projects to cut out some bottlenecks as well as comparing and contrasting different JSF frameworks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One easy way of determining how long it takes to return a page from Seam, and\/or JSF is to use a phase listener. This phase listener below logs the end of each phase of the JSF lifecycle and measures the time from the start of the RESTORE_VIEW phase (the first phase in the lifecycle and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[64],"tags":[6,32,8],"_links":{"self":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/295"}],"collection":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":10,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":1714,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/295\/revisions\/1714"}],"wp:attachment":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}