001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.varia.stats;
023:
024: import org.jboss.system.ServiceMBeanSupport;
025: import org.jboss.varia.stats.report.ReportGenerator;
026:
027: import java.util.Iterator;
028: import java.util.Set;
029: import java.util.HashSet;
030:
031: /**
032: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
033: * @version <tt>$Revision: 57210 $</tt>
034: * @jmx:mbean name="jboss.stats:service=StatisticsCollector"
035: * extends="org.jboss.system.ServiceMBean"
036: */
037: public class StatisticsCollector extends ServiceMBeanSupport implements
038: StatisticsCollectorMBean {
039: private final TxStatistics stats = new TxStatistics();
040:
041: private final Set reportGenerators = new HashSet();
042:
043: /**
044: * @jmx.managed-operation
045: */
046: public void registerReportGenerator(ReportGenerator reportGenerator) {
047: reportGenerators.add(reportGenerator);
048: }
049:
050: /**
051: * @jmx.managed-operation
052: */
053: public void unregisterReportGenerator(
054: ReportGenerator reportGenerator) {
055: reportGenerators.remove(reportGenerator);
056: }
057:
058: /**
059: * @jmx.managed-operation
060: */
061: public void clearStatistics() {
062: stats.clear();
063: }
064:
065: /**
066: * @jmx.managed-operation
067: */
068: public void addStatisticalItem(StatisticalItem item) {
069: stats.addStatisticalItem(item);
070: }
071:
072: /**
073: * @jmx.managed-operation
074: */
075: public Iterator reportsIterator() {
076: return stats.getReports();
077: }
078:
079: /**
080: * @jmx.managed-operation
081: */
082: public TxStatistics txStatistics() {
083: return stats;
084: }
085:
086: /**
087: * @jmx.managed-operation
088: */
089: public synchronized String reports() {
090: StringBuffer buf = new StringBuffer();
091:
092: buf
093: .append("<table><tr><th>Report</th><th>Description</th></tr>");
094: for (Iterator generators = reportGenerators.iterator(); generators
095: .hasNext();) {
096: ReportGenerator generator = (ReportGenerator) generators
097: .next();
098: buf.append("<tr><td>").append("<a href='HtmlAdaptor?")
099: .append("action=invokeOpByName&name=").append(
100: generator.getServiceName()).append(
101: "&methodName=generate&").append(
102: "argType=java.lang.String&arg0=").append(
103: "'>").append(generator.getName()).append(
104: "</a></td><td>").append(
105: generator.getDescription()).append(
106: "</td></tr>");
107: }
108: buf.append("</table>");
109:
110: /*
111: buf.append("<table><tr valign='top'><td>");
112:
113: buf.append("<table>");
114: buf.append("<tr><th>Transaction started by</th><th>total</th>");
115: buf.append("</tr>");
116:
117: for(Iterator iter = stats.getReports(); iter.hasNext();)
118: {
119: TxReport report = (TxReport) iter.next();
120:
121: String name = report.getName();
122: buf.append("<tr valign='top'>")
123: .append("<td>");
124:
125: boolean anchor = !name.equals(reportName) && reportName != null;
126: if(anchor)
127: {
128: buf.append("<a href='HtmlAdaptor?")
129: .append("action=invokeOpByName&name=jboss.stats%3Aservice%3DStatisticsCollector&methodName=report&")
130: .append("argType=java.lang.String&arg0=")
131: .append(name)
132: .append("'>");
133: }
134:
135: buf.append(name)
136: .append("</td><td>")
137: .append(report.getCount())
138: .append("</td>");
139:
140: if(anchor)
141: {
142: buf.append("</a>");
143: }
144:
145: buf.append("</td></tr>");
146: }
147:
148: buf.append("</table>");
149: buf.append("</td><td>");
150:
151: TxReport report = stats.getReports(reportName);
152: if(report != null)
153: {
154: buf.append("<table><tr>");
155:
156: String[] itemNames = stats.getCollectedItemNames();
157: for(int i = 0; i < itemNames.length; ++i)
158: {
159: buf.append("<th>").append(itemNames[i]).append("</th>");
160: }
161:
162: buf.append("</tr><tr valign='top'>");
163:
164: for(int i = 0; i < itemNames.length; ++i)
165: {
166: buf.append("<td>");
167: String itemName = itemNames[i];
168:
169: Map itemMap = (Map) report.getStats().get(itemName);
170: if(itemMap != null && !itemMap.isEmpty())
171: {
172: buf.append("<table width='100%'>")
173: .append("<tr><th>item</th><th>%</th><th>avg</th><th>min</th><th>max</th></tr>");
174:
175: for(Iterator itemIter = itemMap.values().iterator(); itemIter.hasNext();)
176: {
177: StatisticalItem item = (StatisticalItem) itemIter.next();
178: buf.append("<tr><td>")
179: .append(item.getValue())
180: .append("</td><td>")
181: .append(100*((double)item.getMergedItemsTotal() / report.getCount()))
182: .append("</td><td>")
183: .append(((double) item.getCount()) / report.getCount())
184: .append("</td><td>")
185: .append(item.getMinCountPerTx())
186: .append("</td><td>")
187: .append(item.getMaxCountPerTx())
188: .append("</td>");
189: }
190:
191: buf.append("</table>");
192: }
193:
194: buf.append("</td>");
195: }
196:
197: buf.append("</tr></table>");
198: }
199:
200: buf.append("</td></tr></table>");
201:
202: buf.append("<ul>")
203: .append("<li><b>Transaction started by</b> - the method which started the transaction</li>")
204: .append("<li><b>total</b> - the total number of transactions in the run</li>")
205: .append("<li><b>%</b> - the percentage of transactions this item took place in</li>")
206: .append("<li><b>avg</b> - the average number of times the item took place in the given transaction</li>")
207: .append("</ul>");
208: */
209:
210: return buf.toString();
211: }
212: }
|