KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > stats > StatisticsCollector


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.stats;
23
24 import org.jboss.system.ServiceMBeanSupport;
25 import org.jboss.varia.stats.report.ReportGenerator;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
33  * @version <tt>$Revision: 37459 $</tt>
34  * @jmx:mbean name="jboss.stats:service=StatisticsCollector"
35  * extends="org.jboss.system.ServiceMBean"
36  */

37 public class StatisticsCollector
38    extends ServiceMBeanSupport
39    implements StatisticsCollectorMBean
40 {
41    private final TxStatistics stats = new TxStatistics();
42
43    private final Set JavaDoc reportGenerators = new HashSet JavaDoc();
44
45
46    /**
47     * @jmx.managed-operation
48     */

49    public void registerReportGenerator(ReportGenerator reportGenerator)
50    {
51       reportGenerators.add(reportGenerator);
52    }
53
54    /**
55     * @jmx.managed-operation
56     */

57    public void unregisterReportGenerator(ReportGenerator reportGenerator)
58    {
59       reportGenerators.remove(reportGenerator);
60    }
61
62    /**
63     * @jmx.managed-operation
64     */

65    public void clearStatistics()
66    {
67       stats.clear();
68    }
69
70    /**
71     * @jmx.managed-operation
72     */

73    public void addStatisticalItem(StatisticalItem item)
74    {
75       stats.addStatisticalItem(item);
76    }
77
78    /**
79     * @jmx.managed-operation
80     */

81    public Iterator JavaDoc reportsIterator()
82    {
83       return stats.getReports();
84    }
85
86    /**
87     * @jmx.managed-operation
88     */

89    public TxStatistics txStatistics()
90    {
91       return stats;
92    }
93
94    /**
95     * @jmx.managed-operation
96     */

97    public synchronized String JavaDoc reports()
98    {
99       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
100
101       buf.append("<table><tr><th>Report</th><th>Description</th></tr>");
102       for(Iterator JavaDoc generators = reportGenerators.iterator(); generators.hasNext();)
103       {
104          ReportGenerator generator = (ReportGenerator)generators.next();
105          buf.append("<tr><td>")
106             .append("<a HREF='HtmlAdaptor?")
107             .append("action=invokeOpByName&name=")
108             .append(generator.getServiceName())
109             .append("&methodName=generate&")
110             .append("argType=java.lang.String&arg0=")
111             .append("'>")
112             .append(generator.getName())
113             .append("</a></td><td>")
114             .append(generator.getDescription())
115             .append("</td></tr>");
116       }
117       buf.append("</table>");
118
119       /*
120       buf.append("<table><tr valign='top'><td>");
121
122       buf.append("<table>");
123       buf.append("<tr><th>Transaction started by</th><th>total</th>");
124       buf.append("</tr>");
125
126       for(Iterator iter = stats.getReports(); iter.hasNext();)
127       {
128          TxReport report = (TxReport) iter.next();
129
130          String name = report.getName();
131          buf.append("<tr valign='top'>")
132             .append("<td>");
133
134          boolean anchor = !name.equals(reportName) && reportName != null;
135          if(anchor)
136          {
137             buf.append("<a HREF='HtmlAdaptor?")
138                .append("action=invokeOpByName&name=jboss.stats%3Aservice%3DStatisticsCollector&methodName=report&")
139                .append("argType=java.lang.String&arg0=")
140                .append(name)
141                .append("'>");
142          }
143
144          buf.append(name)
145             .append("</td><td>")
146             .append(report.getCount())
147             .append("</td>");
148
149          if(anchor)
150          {
151             buf.append("</a>");
152          }
153
154          buf.append("</td></tr>");
155       }
156
157       buf.append("</table>");
158       buf.append("</td><td>");
159
160       TxReport report = stats.getReports(reportName);
161       if(report != null)
162       {
163          buf.append("<table><tr>");
164
165          String[] itemNames = stats.getCollectedItemNames();
166          for(int i = 0; i < itemNames.length; ++i)
167          {
168             buf.append("<th>").append(itemNames[i]).append("</th>");
169          }
170
171          buf.append("</tr><tr valign='top'>");
172
173          for(int i = 0; i < itemNames.length; ++i)
174          {
175             buf.append("<td>");
176             String itemName = itemNames[i];
177
178             Map itemMap = (Map) report.getStats().get(itemName);
179             if(itemMap != null && !itemMap.isEmpty())
180             {
181                buf.append("<table width='100%'>")
182                   .append("<tr><th>item</th><th>%</th><th>avg</th><th>min</th><th>max</th></tr>");
183
184                for(Iterator itemIter = itemMap.values().iterator(); itemIter.hasNext();)
185                {
186                   StatisticalItem item = (StatisticalItem) itemIter.next();
187                   buf.append("<tr><td>")
188                      .append(item.getValue())
189                      .append("</td><td>")
190                      .append(100*((double)item.getMergedItemsTotal() / report.getCount()))
191                      .append("</td><td>")
192                      .append(((double) item.getCount()) / report.getCount())
193                      .append("</td><td>")
194                      .append(item.getMinCountPerTx())
195                      .append("</td><td>")
196                      .append(item.getMaxCountPerTx())
197                      .append("</td>");
198                }
199
200                buf.append("</table>");
201             }
202
203             buf.append("</td>");
204          }
205
206          buf.append("</tr></table>");
207       }
208
209       buf.append("</td></tr></table>");
210
211       buf.append("<ul>")
212          .append("<li><b>Transaction started by</b> - the method which started the transaction</li>")
213          .append("<li><b>total</b> - the total number of transactions in the run</li>")
214          .append("<li><b>%</b> - the percentage of transactions this item took place in</li>")
215          .append("<li><b>avg</b> - the average number of times the item took place in the given transaction</li>")
216          .append("</ul>");
217          */

218
219       return buf.toString();
220    }
221 }
222
Popular Tags