001: // StatisticsFrame.java
002: // $Id: StatisticsFrame.java,v 1.27 2005/07/22 15:44:15 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.status;
007:
008: import java.util.Date;
009:
010: import org.w3c.tools.resources.Attribute;
011: import org.w3c.tools.resources.AttributeHolder;
012: import org.w3c.tools.resources.AttributeRegistry;
013: import org.w3c.tools.resources.FramedResource;
014: import org.w3c.tools.resources.IntegerAttribute;
015: import org.w3c.tools.resources.Resource;
016:
017: import org.w3c.tools.resources.store.ResourceStoreManager;
018:
019: import org.w3c.www.http.HTTP;
020: import org.w3c.www.http.HttpMessage;
021:
022: import org.w3c.jigsaw.frames.HTTPFrame;
023:
024: import org.w3c.jigsaw.http.Reply;
025: import org.w3c.jigsaw.http.Request;
026: import org.w3c.jigsaw.http.httpd;
027: import org.w3c.jigsaw.http.httpdStatistics;
028:
029: import org.w3c.jigsaw.html.HtmlGenerator;
030:
031: /**
032: * This class exports the server statistics.
033: * It makes available a bunch of various parameters about the current
034: * server, and uses the Refresh meta-tag (as the ThreadStat) to
035: * make them redisplay.
036: * <p>This would benefit from being an applet.
037: */
038:
039: public class StatisticsFrame extends HTTPFrame {
040: private static int REFRESH_DEFAULT = 5;
041:
042: /**
043: * Attribute index - Our refresh interval.
044: */
045: protected static int ATTR_REFRESH = -1;
046:
047: static {
048: Attribute a = null;
049: Class cls = null;
050: try {
051: cls = Class
052: .forName("org.w3c.jigsaw.status.StatisticsFrame");
053: } catch (Exception ex) {
054: ex.printStackTrace();
055: System.exit(1);
056: }
057: // The refresh interval attribute:
058: a = new IntegerAttribute("refresh", new Integer(5),
059: Attribute.EDITABLE);
060: ATTR_REFRESH = AttributeRegistry.registerAttribute(cls, a);
061: }
062:
063: static String time_tbl = ("<table border=\"1\" class=\"time\">"
064: + "<caption>Request processing times" + "</caption><tr>"
065: + "<th>min" + "<th>avg" + "<th>max" + "</tr><tr>");
066:
067: static String dyn_time_tbl = ("<table border=\"1\" class=\"time\">"
068: + "<caption>Dynamic request processing times"
069: + "</caption><tr>" + "<th>min" + "<th>avg" + "<th>max"
070: + "</tr><tr>");
071:
072: static String sta_time_tbl = ("<table border=\"1\" class=\"time\">"
073: + "<caption>Static request processing times"
074: + "</caption><tr>" + "<th>min" + "<th>avg" + "<th>max"
075: + "</tr><tr>");
076:
077: public void registerResource(FramedResource resource) {
078: super .registerOtherResource(resource);
079: }
080:
081: /**
082: * Get the current set of statistics.
083: * Display the collected statistics in an HTML table.
084: * @param request TYhe request to process.
085: */
086:
087: public Reply get(Request request) {
088: HtmlGenerator g = new HtmlGenerator("Statistics");
089: int refresh = getInt(ATTR_REFRESH, REFRESH_DEFAULT);
090: if (refresh > 0) {
091: g.addMeta("Refresh", Integer.toString(refresh));
092: }
093: addStyleSheet(g);
094: // Dump the statistics:
095: httpdStatistics stats = ((httpd) getServer()).getStatistics();
096: // Uptime:
097: g.append("<h1>Server statistics</h1>");
098: long start_time = stats.getStartTime();
099: long uptime = (System.currentTimeMillis() - start_time) / 1000;
100: g.append("<p>Your server was started on <span class=\"date\">");
101: g.append(new Date(start_time).toString());
102: long duptime = uptime / (3600L * 24L);
103: long htemp = uptime % (3600L * 24L);
104: long huptime = htemp / 3600L;
105: long mtemp = htemp % 3600L;
106: long muptime = mtemp / 60L;
107: long suptime = mtemp % 60L;
108: g.append("</span>\n<p>It has now been running for <span "
109: + "class=\"uptime\">");
110: g.append(Long.toString(duptime));
111: g.append(" days, ");
112: g.append(Long.toString(huptime));
113: g.append(" hours, ");
114: g.append(Long.toString(muptime));
115: g.append(" minutes and ");
116: g.append(Long.toString(suptime));
117: g.append(" seconds.</span>\n");
118: // Hits and bytes:
119: long nb_hits = stats.getHitCount();
120: long dyn_hits = stats.getDynamicHitCount();
121: long static_hits = stats.getStaticHitCount();
122: float static_pcent = 0;
123: float dyn_pcent = 0;
124: if (nb_hits > 0) {
125: static_pcent = ((float) static_hits / (float) nb_hits) * 100;
126: dyn_pcent = ((float) dyn_hits / (float) nb_hits) * 100;
127: }
128: g.append("<ul><li>hits: ", Long.toString(nb_hits));
129: g
130: .append(" <ul>\n <li>static: ", Long
131: .toString(static_hits));
132: g.append(" (", Float.toString(static_pcent));
133: g.append("%)</li>\n <li>dynamic: ", Long.toString(dyn_hits));
134: g.append(" (", Float.toString(dyn_pcent));
135: g.append("%)</li>\n </ul>\n");
136: long bytes = stats.getEmittedBytes();
137: long kbytes = bytes / 1024;
138: long mbytes = kbytes / 1024;
139: long gbytes = mbytes / 1024;
140: long tbytes = gbytes / 1024;
141: if (tbytes != 0) {
142: g.append("</li>\n<li>bytes: ", Long.toString(tbytes),
143: "TB, ");
144: g.append(Long.toString(gbytes % 1024), "GB, ");
145: g.append(Long.toString(mbytes % 1024), "MB, ");
146: g.append(Long.toString(kbytes % 1024), "KB, ");
147: g.append(Long.toString(bytes % 1024));
148: } else if (gbytes != 0) {
149: g.append("</li>\n<li>bytes: ", Long.toString(gbytes),
150: "GB, ");
151: g.append(Long.toString(mbytes % 1024), "MB, ");
152: g.append(Long.toString(kbytes % 1024), "KB, ");
153: g.append(Long.toString(bytes % 1024));
154: } else if (mbytes != 0) {
155: g.append("</li>\n<li>bytes: ", Long.toString(mbytes),
156: "MB, ");
157: g.append(Long.toString(kbytes % 1024), "KB, ");
158: g.append(Long.toString(bytes % 1024));
159: } else if (kbytes != 0) {
160: g.append("</li>\n<li>bytes: ", Long.toString(kbytes),
161: "KB, ");
162: g.append(Long.toString(bytes % 1024));
163: } else {
164: g.append("</li>\n<li>bytes: ", Long.toString(bytes));
165: }
166: // avg hit/sec
167: float avghits = 0;
168: float avghitsday = 0;
169: if (uptime > 0) {
170: avghits = ((float) nb_hits) / ((float) uptime);
171: avghitsday = ((float) nb_hits * 86400) / ((float) uptime);
172: }
173: g.append("</li>\n<li>Average hits per second: ");
174: g.append(Float.toString(avghits));
175: g.append("</li>\n<li>Average hits per day: ");
176: g.append(Integer.toString((int) avghitsday));
177: // avg bytes/hit
178: long avgbph;
179:
180: if (nb_hits > 0) {
181: avgbph = bytes / nb_hits;
182: } else {
183: avgbph = 0;
184: }
185: kbytes = avgbph / 1024;
186: mbytes = kbytes / 1024;
187: gbytes = mbytes / 1024;
188: if (gbytes != 0) {
189: g.append("</li>\n<li>Average bytes per hit: ");
190: g.append(Long.toString(gbytes), "GB, ");
191: g.append(Long.toString(mbytes % 1024), "MB, ");
192: g.append(Long.toString(kbytes % 1024), "KB, ");
193: g.append(Long.toString(avgbph % 1024));
194: } else if (mbytes != 0) {
195: g.append("</li>\n<li>Average bytes per hit: ");
196: g.append(Long.toString(mbytes), "MB, ");
197: g.append(Long.toString(kbytes % 1024), "KB, ");
198: g.append(Long.toString(avgbph % 1024));
199: } else if (kbytes != 0) {
200: g.append("</li>\n<li>Average bytes per hit: ");
201: g.append(Long.toString(kbytes), "KB, ");
202: g.append(Long.toString(avgbph % 1024));
203: } else {
204: g.append("</li>\n<li>Average bytes per hit: ", Long
205: .toString(avgbph));
206: }
207: // avg throughput
208: long avgbps = 0;
209: if (uptime > 0) {
210: avgbps = bytes / uptime;
211: }
212: kbytes = avgbps / 1024;
213: mbytes = kbytes / 1024;
214: gbytes = mbytes / 1024;
215: if (gbytes != 0) {
216: g.append("</li>\n<li>Average bytes per second: ");
217: g.append(Long.toString(gbytes), "GB, ");
218: g.append(Long.toString(mbytes % 1024), "MB, ");
219: g.append(Long.toString(kbytes % 1024), "KB, ");
220: g.append(Long.toString(avgbps % 1024));
221: } else if (mbytes != 0) {
222: g.append("</li>\n<li>Average bytes per second: ");
223: g.append(Long.toString(mbytes), "MB, ");
224: g.append(Long.toString(kbytes % 1024), "KB, ");
225: g.append(Long.toString(avgbps % 1024));
226: } else if (kbytes != 0) {
227: g.append("</li>\n<li>Average bytes per second: ");
228: g.append(Long.toString(kbytes), "KB, ");
229: g.append(Long.toString(avgbps % 1024));
230: } else {
231: g.append("</li>\n<li>Average bytes per second: ", Long
232: .toString(avgbps));
233: }
234: g.append("</li>\n</ul>");
235: // Request times:
236: g.append(time_tbl);
237: g.append("<td>", Long.toString(stats.getMinRequestTime()),
238: " <span class=\"unit\">ms</span>");
239: g.append("</td>\n<td>", Long.toString(stats
240: .getMeanRequestTime()),
241: " <span class=\"unit\">ms</span>");
242: g.append("</td>\n<td>", Long
243: .toString(stats.getMaxRequestTime()),
244: " <span class=\"unit\">ms</span>");
245: g.append("</td>\n</table>\n");
246:
247: // static
248: if (static_hits > 0) {
249: g.append(sta_time_tbl);
250: g.append("<td>", Long.toString(stats
251: .getMinStaticRequestTime()),
252: " <span class=\"unit\">ms</span>");
253: g.append("</td>\n<td>", Long.toString(stats
254: .getMeanStaticRequestTime()),
255: " <span class=\"unit\">ms</span>");
256: g.append("</td>\n<td>", Long.toString(stats
257: .getMaxStaticRequestTime()),
258: " <span class=\"unit\">ms</span>");
259: g.append("</td>\n</table>\n");
260: }
261:
262: // dynamic
263: if (dyn_hits > 0) {
264: g.append(dyn_time_tbl);
265: g.append("<td>", Long.toString(stats
266: .getMinDynamicRequestTime()),
267: " <span class=\"unit\">ms</span>");
268: g.append("</td>\n<td>", Long.toString(stats
269: .getMeanDynamicRequestTime()),
270: " <span class=\"unit\">ms</span>");
271: g.append("</td>\n<td>", Long.toString(stats
272: .getMaxDynamicRequestTime()),
273: " <span class=\"unit\">ms</span>");
274: g.append("</td>\n</table>\n");
275: }
276:
277: // Get Server internal Stats
278: try {
279: g.append(((httpd) getServer()).getHTMLStatus());
280: } catch (Exception ex) {
281: ex.printStackTrace();
282: }
283: Reply reply = request.makeReply(HTTP.OK);
284: reply.setNoCache();
285: reply.setStream(g);
286: reply.setDynamic(true);
287: return reply;
288: }
289: }
|