001: package com.jamonapi;
002:
003: import com.jamonapi.utils.*;
004: import java.util.*;
005:
006: /** This is the Monitor class that starts the decorator chain of Monitors. It tracks the time for this monitor and passes the time
007: * down the chain to the other Monitors, so they can track the other statistics.
008: **/
009: public class TimingMonitor extends BaseMonitor implements LeafNode {
010: private long startTime = 0;
011: AccumulateMonitorInterface childMonitor;
012: boolean isRunningFlag = true;
013:
014: long accrued;
015: boolean hasChildren; // used to determine how to execute toString()
016:
017: TimingMonitor() {
018: this (NullAccumulateMonitor.createInstance());
019: hasChildren = false;
020: }
021:
022: TimingMonitor(AccumulateMonitorInterface childMonitor) {
023: this .childMonitor = childMonitor;
024: hasChildren = true;
025: }
026:
027: /** Returns the time that the Monitor has been running **/
028: public long getAccrued() {
029: return accrued + timeElapsedSinceLastStart();
030: }
031:
032: private long timeElapsedSinceLastStart() {
033: if (isRunning()) {
034: return System.currentTimeMillis() - startTime;
035: } else {
036: return 0;
037: }
038: }
039:
040: /** Resets the accrued time and restarts the Monitor **/
041: public void reset() {
042: accrued = 0;
043: resetStartTime();
044: }
045:
046: /** increase the time by the specified ammount of milliseconds. Typically this will not be called. **/
047: public void increase(long increaseValue) {
048: if (isRunning()) {
049: childMonitor.increase(increaseValue);
050: accrued += increaseValue;
051: }
052: }
053:
054: // This class doesn't need to be thread safe as whenever start() is called a new TimingMonitor is returned
055:
056: /** Start timing for the Monitor **/
057: public Monitor start() {
058: TimingMonitor mon = new TimingMonitor(childMonitor);
059:
060: mon.resetStartTime();
061: mon.hasChildren = hasChildren;
062: mon.childMonitor.start();
063:
064: return mon;
065: }
066:
067: public void getData(ArrayList rowData) {
068: childMonitor.getData(rowData);
069: }
070:
071: public void getHeader(ArrayList header) {
072: childMonitor.getHeader(header);
073: }
074:
075: /** Stop the Monitor and keep track of how long it was running. Call the next Monitor in the Monitor decorator chain **/
076: public Monitor stop() {
077: increase(timeElapsedSinceLastStart());
078:
079: if (isRunning())
080: childMonitor.stop();
081:
082: setRunning(false);
083:
084: return this ;
085:
086: }
087:
088: private boolean primary = false;
089:
090: public boolean isPrimary() {
091: return primary;
092: }
093:
094: public void setPrimary(boolean primary) {
095: this .primary = primary;
096: childMonitor.setPrimary(primary);
097: }
098:
099: private boolean isRunning() {
100: return isRunningFlag;
101: }
102:
103: private void resetStartTime() {
104: startTime = System.currentTimeMillis();
105: }
106:
107: public String toString() {
108: String message = getAccruedString() + " ms.";
109:
110: if (hasChildren)
111: message += " (" + childMonitor.toString() + ")";
112:
113: return message;
114: }
115:
116: private void setRunning(boolean flag) {
117: isRunningFlag = flag;
118: }
119:
120: /** Method with the classes test code **/
121: public static void main(String[] args) throws Exception {
122: Monitor m = new TimingMonitor();
123: m = m.start();
124: Thread.sleep(1000);
125: m.stop();
126: System.out.println("should be about 1000=" + m.getAccrued());
127: System.out.println("should be same as above " + m.getAccrued());
128: Thread.sleep(1000);
129: System.out.println("should be same as above " + m.getAccrued());
130: m.reset();
131: Thread.sleep(10);
132: System.out.println("should be 0=" + m.getAccrued());
133: m = m.start();
134: Thread.sleep(100);
135: System.out.println("should be about 100=" + m.getAccrued());
136:
137: m = m.start();
138: Thread.sleep(500);
139: m.stop();
140: m = m.start();
141: Thread.sleep(250);
142: System.out.println("should be about 250=" + m.getAccrued());
143:
144: m = m.start();
145: Thread.sleep(500);
146: //m.pause();
147: //System.out.println("should be about 500="+m.getAccrued()); // pause doesn't reset the accumulator
148: //m=m.start();
149: Thread.sleep(250);
150: System.out.println("should be about 750=" + m.getAccrued());
151: System.out.println("toString should be about 750=" + m.stop());
152: Thread.sleep(250);
153:
154: System.out.println("toString should be about 750=" + m.stop());
155:
156: for (int i = 1; i <= 5; i++) {
157: m = m.start();
158: Thread.sleep(i * 10);
159: m.stop();
160: }
161:
162: System.out
163: .println("\ntesting start() and stop() in loop. should be about 50="
164: + m);
165:
166: m.reset();
167: System.out.println("\nreset to 0's: " + m);
168:
169: }
170: }
|