Tuesday, 2 September 2014

How to monitoring performance in Zk

By Sitansu S Swain

There are three steps to monitoring performance in zk.

(1)Register in zk.xml

<library-property><name>org.zkoss.bind.PhaseListener.class</name><value>com.demo.CommandPhaseListener</value></library-property>

<listener><description>Monitor the statistic</description><listener-class>org.zkoss.zk.ui.util.Statistic</listener-class></listener>

<listener><description>ZK performance monitor</description><listener-class>com.demo.PerformanceMeter</listener-class></listener>

(2)create a CommandPhaseListener.java  class for Set the command name to be used in PerformanceMeter for logging with slf4j.

package com.demo;

import org.zkoss.bind.BindContext;
import org.zkoss.bind.Phase;
import org.zkoss.bind.PhaseListener;
import org.zkoss.zk.ui.Executions;

public class CommandPhaseListener implements PhaseListener {

@Override
public void prePhase(Phase phase, BindContext ctx) {
if (phase == Phase.COMMAND) {
//Set the command name to be used in PerformanceMeter for logging.
Executions.getCurrent().setAttribute("command", ctx.getCommandName());
}
}

@Override
public void postPhase(Phase phase, BindContext ctx) {

}
}

(3)Create a DemoPerformanceMeter.java class for monitoring performance.


package com.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.util.PerformanceMeter;

public class DemoPerformanceMeter implements PerformanceMeter {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoPerformanceMeter.class);

private long timeStartAtClient;
private long timeStartAtServer;
private long timeCompleteAtServer;
private long timeRecieveAtClient;
private long timeCompleteAtClient;

@Override
public void requestCompleteAtClient(String requestId, Execution exec, long time) {
timeCompleteAtClient = time;
}

@Override
public void requestCompleteAtServer(String requestId, Execution exec, long time) {
timeCompleteAtServer = time;
Long serverExe = timeCompleteAtServer - timeStartAtServer;
Long clientExe = timeCompleteAtClient - timeRecieveAtClient;
Long networkExe = (timeCompleteAtServer - timeRecieveAtClient) + (timeStartAtServer - timeStartAtClient);
long totalExe = 0;
totalExe += serverExe != null ? serverExe : 0;
totalExe += clientExe != null ? clientExe : 0;
totalExe += networkExe != null ? networkExe : 0;
if (exec.getAttribute("command") != null) {
LOGGER.debug("Command name:{} ",exec.getAttribute("command"));
}
LOGGER.debug("Total browser execution time:{} milliseconds., Total server execution time:{} milliseconds., Total network execution time:{} milliseconds., Total execution time:{} milliseconds.", new Object[]{clientExe,serverExe,networkExe,totalExe});
}

@Override
public void requestReceiveAtClient(String requestId, Execution exec, long time) {
timeRecieveAtClient = time;
}

@Override
public void requestStartAtClient(String requestId, Execution exec, long time) {
timeStartAtClient = time;
}

@Override
public void requestStartAtServer(String requestId, Execution exec, long time) {
timeStartAtServer = time;
}
}

No comments:

Post a Comment