001: /*
002: * $Id: StatUnit.java,v 1.4 2002/04/26 03:38:54 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.servlet;
052:
053: import java.io.*;
054: import java.net.*;
055: import java.util.*;
056: import java.text.*;
057:
058: public class StatUnit implements Serializable {
059:
060: public static final long serialVersionUID = -6212560619436466903L;
061:
062: private long startTime;
063: private long endTime;
064: private long totalSize = 0;
065: private long totalProcessTime = 0;
066: private long totalTotalTime = 0;
067: private int totalRequests = 0;
068: private int maxSize = Integer.MIN_VALUE;
069: private String maxFile = "";
070: private int minSize = Integer.MAX_VALUE;
071: private String minFile = "";
072:
073: public StatUnit() {
074: }
075:
076: public StatUnit(long startTime) {
077: this .startTime = startTime;
078: }
079:
080: public void print(PrintWriter p) {
081: SimpleDateFormat formatter = new SimpleDateFormat(
082: "MM/dd/yyyy hh:mm:ss z");
083: String startStr = formatter.format(new Date(startTime));
084: String endStr = endTime == 0 ? "present" : formatter
085: .format(new Date(endTime));
086: p.print("Statistics for period from " + startStr + " to "
087: + endStr + "<br>");
088: p.print(" Number of requests " + totalRequests + "<br>");
089: p.print(" Total size " + totalSize + " bytes<br>");
090: p.print(" Total time " + totalTotalTime + "ms<br>");
091: p
092: .print(" Max size/file " + maxSize + "/'" + maxFile
093: + "'<br>");
094: p
095: .print(" Min size/file " + minSize + "/'" + minFile
096: + "'<br>");
097: p.print(" Average size " + getAverageSize() + " bytes<br>");
098: p.print(" Average processing time " + getAverageProcessTime()
099: + "ms<br>");
100: p.print(" Average total processing time "
101: + getAverageTotalTime() + "ms<br>");
102: }
103:
104: public void printVars(PrintWriter p) {
105: SimpleDateFormat formatter = new SimpleDateFormat(
106: "MM/dd/yyyy hh:mm:ss z");
107: String startStr = formatter.format(new Date(startTime));
108: long endTm = endTime == 0 ? System.currentTimeMillis()
109: : endTime;
110: String endStr = formatter.format(new Date(endTm));
111: p.print("&startTime=" + URLEncoder.encode(startStr));
112: p.print("&endTime=" + URLEncoder.encode(endStr));
113: p.print("&numRequests=" + totalRequests);
114: p.print("&totalSize=" + totalSize);
115: p.print("&totalTime=" + totalTotalTime);
116: p.print("&maxSize=" + maxSize);
117: p.print("&maxFile=" + URLEncoder.encode(maxFile));
118: p.print("&minSize=" + minSize);
119: p.print("&minFile=" + URLEncoder.encode(minFile));
120: p.print("&averageSize=" + getAverageSize());
121: p.print("&averageProcessTime=" + getAverageProcessTime());
122: p.print("&averageTotalTime=" + getAverageTotalTime());
123: }
124:
125: public void printXML(PrintWriter p, boolean current) {
126: if (current) {
127: p.println("<stat-block current=\"true\">");
128: } else {
129: p.println("<stat-block>");
130: }
131: SimpleDateFormat formatter = new SimpleDateFormat(
132: "MM/dd/yyyy hh:mm:ss z");
133: String startStr = formatter.format(new Date(startTime));
134: long endTm = endTime == 0 ? System.currentTimeMillis()
135: : endTime;
136: String endStr = formatter.format(new Date(endTm));
137: p.println("<start-time>" + startStr + "</start-time>");
138: p.println("<end-time>" + endStr + "</end-time>");
139: p.println("<requests-num>" + totalRequests + "</requests-num>");
140: p.println("<total-size>" + totalSize + "</total-size>");
141: p.println("<total-time>" + totalTotalTime + "</total-time>");
142: p.println("<max-size>" + maxSize + "</max-size>");
143: p.println("<max-file>" + maxFile + "</max-file>");
144: p.println("<min-size>" + minSize + "</min-size>");
145: p.println("<min-file>" + minFile + "</min-file>");
146: p.println("<average-time>" + getAverageProcessTime()
147: + "</average-time>");
148: p.println("<average-size>" + getAverageSize()
149: + "</average-size>");
150: p.println("</stat-block>");
151: }
152:
153: public synchronized void addRequest(String fileName, int size,
154: long processTime, long totalTime) {
155: totalSize += size;
156: totalProcessTime += processTime;
157: totalTotalTime += totalTime;
158: totalRequests++;
159: if (size > maxSize) {
160: maxSize = size;
161: maxFile = fileName;
162: }
163: if (size < minSize) {
164: minSize = size;
165: minFile = fileName;
166: }
167: }
168:
169: public void setStartTime(long time) {
170: startTime = time;
171: }
172:
173: public long getStartTime() {
174: return startTime;
175: }
176:
177: public void setEndTime(long time) {
178: endTime = time;
179: }
180:
181: public long getEndTime() {
182: return endTime;
183: }
184:
185: public int getAverageSize() {
186: if (totalRequests == 0)
187: return 0;
188: return (int) (totalSize / totalRequests);
189: }
190:
191: public long getAverageProcessTime() {
192: if (totalRequests == 0)
193: return 0;
194: return totalProcessTime / totalRequests;
195: }
196:
197: public long getAverageTotalTime() {
198: if (totalRequests == 0)
199: return 0;
200: return totalTotalTime / totalRequests;
201: }
202: }
|