001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.glm.servlet;
028:
029: import java.io.IOException;
030: import java.io.Serializable;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.TreeSet;
035:
036: import org.cougaar.planning.servlet.data.xml.XMLWriter;
037: import org.cougaar.planning.servlet.data.xml.XMLable;
038:
039: /**
040: * <pre>
041: * Represents the returned data from a stimulator request
042: *
043: * Sample output is :
044: *
045: * <results totalTime="0:06:069">
046: * <task id="1" time="0:00:090"/>
047: * <task id="2" time="0:02:994"/>
048: * <task id="3" time="0:02:985"/>
049: * </results>
050: *
051: * </pre>
052: */
053: public class GLMStimulatorResponseData implements XMLable, Serializable {
054: public String totalTime;
055: public Map taskTimes = new HashMap();
056: private static double log10 = Math.log(10.0);
057: private static double log5 = Math.log(5.0);
058: private static double log2 = Math.log(2.0);
059: private static long[] pwr10 = new long[30];
060: private static final int NBUCKETS = pwr10.length * 3;
061: static {
062: long m = 1L;
063: for (int i = 0; i < pwr10.length; i++) {
064: pwr10[i] = m;
065: m *= 10L;
066: }
067: }
068: /** Response-time histogram buckets **/
069: public int[] buckets = new int[NBUCKETS];
070:
071: /** The maximum used bucket number **/
072: public int maxBucket = 0;
073: /** The minimum used bucket number **/
074: public int minBucket = NBUCKETS - 1;
075:
076: /**
077: * Gets the index of the histogram bucket for the given elapsed
078: * time. The buckets cover the range 0..1, 1..2, 2..5, 5..10, etc.
079: **/
080: private static int getBucket(long elapsed) {
081: if (elapsed < 1)
082: return 0;
083: int i = (int) Math.floor(Math.log(elapsed) / log10); // 1 or less should be 0
084: if (i >= pwr10.length)
085: return NBUCKETS - 1;
086: int b = (int) (elapsed / pwr10[i]);
087: switch (b) {
088: case 0:
089: case 1:
090: return i * 3 + 0;
091: case 2:
092: case 3:
093: case 4:
094: return i * 3 + 1;
095: case 5:
096: case 6:
097: case 7:
098: case 8:
099: case 9:
100: return i * 3 + 2;
101: }
102: return i * 3;
103: }
104:
105: /**
106: * Record the time to handle a task.
107: * @param uid the task UID
108: * @param time the time string to be printed
109: * @param elapsed the numeric elapsed time for histogram
110: **/
111: public void addTaskAndTime(String uid, String time, long elapsed) {
112: taskTimes.put(uid, time);
113: int bucket = getBucket(elapsed);
114: maxBucket = Math.max(maxBucket, bucket);
115: minBucket = Math.min(maxBucket, bucket);
116: buckets[bucket]++;
117: }
118:
119: /**
120: * Write this class out to the Writer in XML format
121: * @param w output Writer
122: **/
123: public void toXML(XMLWriter w) throws IOException {
124: w.optagln("response");
125: w.optagln("results", "totalTime", totalTime);
126: for (Iterator iter = new TreeSet(taskTimes.keySet()).iterator(); iter
127: .hasNext();) {
128: Object key = iter.next();
129: w.sitagln("task", "id", (String) key, "time",
130: (String) taskTimes.get(key));
131: }
132: w.cltagln("results");
133: w.optagln("histogram", "minBucket", String.valueOf(minBucket),
134: "maxBucket", String.valueOf(maxBucket));
135: long bucketMin = 0L;
136: for (int bucket = 0; bucket <= maxBucket; bucket++) {
137: long bucketMax = pwr10[bucket / 3];
138: switch (bucket % 3) {
139: case 0:
140: bucketMax *= 2L;
141: break;
142: case 1:
143: bucketMax *= 5L;
144: break;
145: case 2:
146: bucketMax *= 10L;
147: break;
148: }
149: if (bucket >= minBucket) {
150: w.sitagln("bucket", "elapsedTime", bucketMin + ".."
151: + bucketMax, "count", String
152: .valueOf(buckets[bucket]));
153: }
154: bucketMin = bucketMax;
155: }
156: w.cltagln("histogram");
157: w.cltagln("response");
158: }
159:
160: public String toString() {
161: StringBuffer buf = new StringBuffer();
162: buf
163: .append("\n************************* Testing Summary *************************\n");
164: buf.append(" Task UID Time \n");
165: for (Iterator iter = taskTimes.keySet().iterator(); iter
166: .hasNext();) {
167: Object key = iter.next();
168: buf.append(" " + key + " "
169: + (String) taskTimes.get(key) + "\n");
170: }
171: buf.append("\n Total Time: " + totalTime);
172: buf.append("\n Histogram");
173: long bucketMin = 0L;
174: for (int bucket = 0; bucket < NBUCKETS; bucket++) {
175: long bucketMax = pwr10[bucket / 3];
176: switch (bucket % 3) {
177: case 0:
178: bucketMax *= 2L;
179: break;
180: case 1:
181: bucketMax *= 5L;
182: break;
183: case 2:
184: bucketMax *= 10L;
185: break;
186: }
187: buf.append("\n").append(bucketMin).append("..").append(
188: bucketMax).append(':').append(buckets[bucket]);
189: bucketMin = bucketMax;
190: }
191: buf
192: .append("\n*******************************************************************\n");
193:
194: return buf.toString();
195: }
196:
197: public static void main(String[] args) {
198: for (int i = 0; i < args.length; i++) {
199: System.out.println(args[i] + ": "
200: + getBucket(new Long(args[i]).longValue()));
201: }
202: }
203: }
|