001: package com.jamonapi;
002:
003: import java.util.*;
004:
005: /** Tracks statistics for Monitors that are currently running. The statistics tracked are how often this Monitor
006: * is called (hits), how many outstanding invocations of start() have been called without a stop() for this instance (active), what
007: * is the average number of active for this instance, and what was the maximum number of active for this instance.
008: **/
009:
010: public class ActiveStatsMonitor extends AccumulateMonitor {
011: // ANY VARIABLES SET HERE MUST ALSO BE SET IN resetThis()!!!!
012: private int max = Integer.MIN_VALUE;
013: private int active;
014: private int hits;
015: private int total; // avg is calculated when needed by avg=total/hits
016:
017: public ActiveStatsMonitor() {
018: super ();
019: }
020:
021: /** Constructor that uses the Gang Of 4's decorator pattern. **/
022: public ActiveStatsMonitor(AccumulateMonitorInterface childMonitor) {
023: super (childMonitor);
024: }
025:
026: /** Adjust the statistics being tracked whenever start() is called. For example whenever start() is called on this instance
027: * the active variable is increased by 1.
028: **/
029: synchronized protected void startThis() {
030: active++;
031: hits++;
032: total += active;
033:
034: if (active > max)
035: max = active;
036: }
037:
038: /** Adjust the statistics being tracked whenever stop() is called. For example whenever stop() is called on this instance
039: * the active variable is decreased by 1.
040: **/
041:
042: synchronized protected void stopThis() {
043: if (active > 0)
044: active--;
045: }
046:
047: protected void increaseThis(long increase) {
048: }
049:
050: /** Reset the variables for this instance to their default values **/
051: synchronized protected void resetThis() {
052: max = Integer.MIN_VALUE;
053: hits = active = total = 0;
054: }
055:
056: /** Convert this Monitor to its String value for display purposes **/
057: synchronized protected String toStringThis() {
058: return getDisplayString(ACTIVE, convertToString(active), NONE)
059: + getDisplayString(AVGACTIVE, convertToString(avg()),
060: NONE)
061: + getDisplayString(MAXACTIVE, convertToString(max),
062: NONE);
063: }
064:
065: /** Add this instances data to the ArrayList. This method is used to convert Monitor data to a tabular format **/
066: synchronized protected void getDataThis(ArrayList rowData) {
067: rowData.add(convertToString(active));
068: rowData.add(convertToString(avg()));
069: rowData.add(convertToString(max));
070: }
071:
072: /** Add this instances header to the ArrayList. This method is used to convert Monitor header data to a tabular format.
073: * The header will usually be used to display the Monitor in HTML format
074: **/
075: protected void getHeaderThis(ArrayList header) {
076: header.add(ACTIVE);
077: header.add(AVGACTIVE);
078: header.add(MAXACTIVE);
079: }
080:
081: /** Return the average number of Active monitors of this instance **/
082: final float avg() {
083: if (hits == 0)
084: return 0;
085: else
086: return (float) total / hits;
087: }
088:
089: /** Return the number of Active Monitors of this instance **/
090: synchronized public long getAccrued() {
091: return active;
092: }
093:
094: /** Method that contains test data for this class **/
095: public static void main(String[] args) throws Exception {
096: AccumulateMonitor mon = new ActiveStatsMonitor();
097: System.out.println("should not have 0 divide error=" + mon);
098: mon.start(); //active=1
099: mon.start(); //2
100: mon.start(); //3
101: mon.stop(); //2
102: mon.stop(); //1
103: mon.start(); //2
104:
105: System.out.println("should be active 2, avg 2, max active 3="
106: + mon);
107:
108: }
109: }
|