001: /* Statistic.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Mar 14 23:32:51 2006, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zk.ui.util;
020:
021: import java.util.List;
022:
023: import org.zkoss.zk.ui.Session;
024: import org.zkoss.zk.ui.Desktop;
025:
026: /**
027: * An implementation of {@link Monitor} to accumulate statistic data
028: * in memory.
029: *
030: * <p>It has no effect until you specify it in WEB-INF/zk.xml.
031: *
032: * @author tomyeh
033: */
034: public class Statistic implements Monitor {
035: private final long _startTime;
036: private int _nsess, _actsess, _ndt, _actdt, _nupd, _actupd;
037:
038: public Statistic() {
039: _startTime = System.currentTimeMillis();
040: }
041:
042: /** Returns when the server (actually, this monitor) started.
043: */
044: public long getStartTime() {
045: return _startTime;
046: }
047:
048: /** Returns the total number of sessions that have been created
049: * since the server started.
050: */
051: public int getTotalSessionCount() {
052: return _nsess;
053: }
054:
055: /** Returns the number of active sessions.
056: */
057: public int getActiveSessionCount() {
058: return _actsess;
059: }
060:
061: /** Returns the average number of sessions being created in an hour.
062: */
063: public double getAverageSessionCount() {
064: return _nsess / getEscapedHours();
065: }
066:
067: /** Returns the total number of desktops that have been created
068: * since the server started.
069: */
070: public int getTotalDesktopCount() {
071: return _ndt;
072: }
073:
074: /** Returns the number of active desktops.
075: */
076: public int getActiveDesktopCount() {
077: return _actdt;
078: }
079:
080: /** Returns the average number of desktops being created in an hour.
081: */
082: public double getAverageDesktopCount() {
083: return _ndt / getEscapedHours();
084: }
085:
086: /** Returns the total number of asynchronous updates that have been received
087: * since the server started.
088: */
089: public int getTotalUpdateCount() {
090: return _nupd;
091: }
092:
093: /** Returns the number of active asynchronous updates.
094: */
095: public int getActiveUpdateCount() {
096: return _actupd;
097: }
098:
099: /** Returns the average number of asynchronous updates being created
100: * in an hour.
101: */
102: public double getAverageUpdateCount() {
103: return _nupd / getEscapedHours();
104: }
105:
106: /** Returns how many hours escaped since the server started.
107: */
108: private double getEscapedHours() {
109: long v = System.currentTimeMillis() - _startTime;
110: return ((double) v) / 3600000;
111: }
112:
113: //-- Monitor --//
114: synchronized public void sessionCreated(Session sess) {
115: ++_nsess;
116: ++_actsess;
117: }
118:
119: synchronized public void sessionDestroyed(Session sess) {
120: --_actsess;
121: }
122:
123: synchronized public void desktopCreated(Desktop desktop) {
124: ++_ndt;
125: ++_actdt;
126: }
127:
128: synchronized public void desktopDestroyed(Desktop desktop) {
129: --_actdt;
130: }
131:
132: synchronized public void beforeUpdate(Desktop desktop, List requests) {
133: ++_nupd;
134: ++_actupd;
135: }
136:
137: synchronized public void afterUpdate(Desktop desktop) {
138: --_actupd;
139: }
140: }
|