KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > stats > report > CacheReportGenerator


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.report;
23
24 import org.jboss.varia.stats.TxReport;
25 import org.jboss.varia.stats.CacheListener;
26
27 import java.util.Map JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
33  * @version <tt>$Revision: 37459 $</tt>
34  */

35 public class CacheReportGenerator
36    extends ReportGenerator
37 {
38    protected void content(String JavaDoc reportName, StringBuffer JavaDoc content) throws Exception JavaDoc
39    {
40       StringBuffer JavaDoc contentionBuf = new StringBuffer JavaDoc();
41       contentionBuf.append(
42          "<table><tr><th>Lock Contention Per Table</th><th>Total time</th><th>Max time</th><th>count</th></tr>");
43       int contentionTotal = 0;
44       int contentionTimeTotal = 0;
45       long maxContentionTime = 0;
46       StringBuffer JavaDoc evictionBuf = new StringBuffer JavaDoc();
47       evictionBuf.append("<table><tr><th>Eviction per table</th><th>count</th></tr>");
48       int evictionTotal = 0;
49       StringBuffer JavaDoc hitsBuf = new StringBuffer JavaDoc();
50       hitsBuf.append("<table><tr><th>Hits per table</th><th>count</th></tr>");
51       int hitsTotal = 0;
52       StringBuffer JavaDoc missesBuf = new StringBuffer JavaDoc();
53       missesBuf.append("<table><tr><th>Misses per table</th><th>count</th></tr>");
54       int missesTotal = 0;
55
56       StringBuffer JavaDoc reportsTable = new StringBuffer JavaDoc();
57       reportsTable.append("<table><tr><th>Transaction started by</th><th>Total</th></tr>");
58
59       int txTotal = 0;
60
61       Map JavaDoc contention = new HashMap JavaDoc();
62       Map JavaDoc eviction = new HashMap JavaDoc();
63       Map JavaDoc hits = new HashMap JavaDoc();
64       Map JavaDoc misses = new HashMap JavaDoc();
65
66       Iterator JavaDoc reports = getReportsIterator();
67       while(reports.hasNext())
68       {
69          TxReport report = (TxReport) reports.next();
70
71          Map JavaDoc contentionMap = (Map JavaDoc) report.getStats().get(CacheListener.ContentionStats.NAME);
72          Map JavaDoc evictionMap = (Map JavaDoc) report.getStats().get(CacheListener.EvictionStats.NAME);
73          Map JavaDoc hitsMap = (Map JavaDoc) report.getStats().get(CacheListener.HitStats.NAME);
74          Map JavaDoc missesMap = (Map JavaDoc) report.getStats().get(CacheListener.MissStats.NAME);
75
76          txTotal += report.getCount();
77
78          if(contentionMap == null && evictionMap == null && hitsMap == null && missesMap == null)
79          {
80             continue;
81          }
82
83          reportsTable.append("<tr><td>");
84
85          boolean selected = report.getName().equals(reportName);
86          if(!selected)
87          {
88             reportsTable.append("<a HREF='HtmlAdaptor?")
89                .append("action=invokeOpByName&name=")
90                .append(getServiceName())
91                .append("&methodName=generate&")
92                .append("argType=java.lang.String&arg0=")
93                .append(report.getName())
94                .append("'>");
95          }
96
97          reportsTable.append(report.getName());
98
99          if(!selected)
100          {
101             reportsTable.append("</a>");
102          }
103
104          reportsTable.append("</td><td>")
105             .append(report.getCount()).append("</td></tr>");
106
107          if(selected || reportName == null || reportName.trim().length() == 0)
108          {
109             if(contentionMap != null)
110             {
111                for(Iterator JavaDoc items = contentionMap.values().iterator(); items.hasNext();)
112                {
113                   CacheListener.ContentionStats item = (CacheListener.ContentionStats) items.next();
114
115                   Contention c = (Contention) contention.get(item.getValue());
116                   if(c == null)
117                   {
118                      c = new Contention(item.getValue());
119                      contention.put(c.tableName, c);
120                   }
121
122                   c.total += item.getContentionTimeTotal();
123                   c.count += item.getCount();
124                   if(c.maxTime < item.getMaxContentionTime())
125                   {
126                      c.maxTime = item.getMaxContentionTime();
127                   }
128
129                   contentionTotal += item.getCount();
130                   contentionTimeTotal += item.getContentionTimeTotal();
131                   if(item.getMaxContentionTime() > maxContentionTime)
132                   {
133                      maxContentionTime = item.getMaxContentionTime();
134                   }
135                }
136             }
137
138             if(evictionMap != null)
139             {
140                for(Iterator JavaDoc items = evictionMap.values().iterator(); items.hasNext();)
141                {
142                   CacheListener.EvictionStats item = (CacheListener.EvictionStats) items.next();
143
144                   Eviction e = (Eviction) eviction.get(item.getTableName());
145                   if(e == null)
146                   {
147                      e = new Eviction(item.getTableName());
148                      eviction.put(e.tableName, e);
149                   }
150                   e.count += item.getCount();
151
152                   evictionTotal += item.getCount();
153                }
154             }
155
156             if(hitsMap != null)
157             {
158                for(Iterator JavaDoc items = hitsMap.values().iterator(); items.hasNext();)
159                {
160                   CacheListener.HitStats item = (CacheListener.HitStats) items.next();
161
162                   Hit h = (Hit) hits.get(item.getTableName());
163                   if(h == null)
164                   {
165                      h = new Hit(item.getTableName());
166                      hits.put(h.tableName, h);
167                   }
168                   h.partitionHit(item.getPartitionIndex(), item.getCount());
169
170                   hitsTotal += item.getCount();
171                }
172             }
173
174             if(missesMap != null)
175             {
176                for(Iterator JavaDoc items = missesMap.values().iterator(); items.hasNext();)
177                {
178                   CacheListener.MissStats item = (CacheListener.MissStats) items.next();
179                   Miss m = (Miss) misses.get(item.getValue());
180                   if(m == null)
181                   {
182                      m = new Miss(item.getValue());
183                      misses.put(m.tableName, m);
184                   }
185                   m.count += item.getCount();
186                   missesTotal += item.getCount();
187                }
188             }
189          }
190       }
191
192       reportsTable.append("<tr><td>");
193
194       boolean select = reportName != null && reportName.trim().length() > 0;
195       if(select)
196       {
197          reportsTable.append("<a HREF='HtmlAdaptor?")
198             .append("action=invokeOpByName&name=")
199             .append(getServiceName())
200             .append("&methodName=generate&")
201             .append("argType=java.lang.String&arg0=")
202             .append("'>");
203       }
204
205       reportsTable.append("all transactions");
206
207       if(select)
208       {
209          reportsTable.append("</a>");
210       }
211
212       reportsTable.append("</td><td>").append(txTotal).append("</td></tr></table>");
213
214       for(Iterator JavaDoc i = contention.values().iterator(); i.hasNext();)
215       {
216          Contention c = (Contention) i.next();
217          contentionBuf.append("<tr><td>").append(c.tableName).append("</td><td>")
218             .append(c.total).append("</td><td>")
219             .append(c.maxTime).append("</td><td>")
220             .append(c.count).append("</td></tr>");
221       }
222
223       for(Iterator JavaDoc i = eviction.values().iterator(); i.hasNext();)
224       {
225          Eviction e = (Eviction) i.next();
226          evictionBuf.append("<tr><td>").append(e.tableName).append("</td><td>")
227             .append(e.count).append("</td></tr>");
228       }
229
230       StringBuffer JavaDoc partitionBuf = new StringBuffer JavaDoc();
231       partitionBuf.append("<table>");
232       for(Iterator JavaDoc i = hits.values().iterator(); i.hasNext();)
233       {
234          Hit h = (Hit) i.next();
235          hitsBuf.append("<tr><td>").append(h.tableName).append("</td><td>")
236             .append(h.count).append("</td></tr>");
237
238          if(h.partitions != null && h.partitions.length > 0)
239          {
240             partitionBuf.append("<tr><td>");
241
242             partitionBuf.append("<table><tr><th>Table: ").append(h.tableName).append("</th></tr>")
243                .append("<tr><th>Partition index</th><th>count</th><th>%</th></tr>");
244             for(int pI = 0; pI < h.partitions.length; ++pI)
245             {
246                final int hit = h.partitions[pI];
247                partitionBuf.append("<tr><td>")
248                   .append(pI).append("</td><td>")
249                   .append(hit).append("</td><td>")
250                   .append((int)(100*((double)hit/h.maxHitPerPartition))).append("</td></tr>");
251             }
252             partitionBuf.append("</table>");
253
254             partitionBuf.append("</td></tr>");
255          }
256       }
257       partitionBuf.append("</table>");
258
259       for(Iterator JavaDoc i = misses.values().iterator(); i.hasNext();)
260       {
261          Miss m = (Miss) i.next();
262          missesBuf.append("<tr><td>").append(m.tableName).append("</td><td>")
263             .append(m.count).append("</td></tr>");
264       }
265
266       contentionBuf.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
267          .append(contentionTimeTotal).append("</font></td><td><font color='red'>")
268          .append(maxContentionTime).append("</font></td><td><font color='red'>")
269          .append(contentionTotal).append("</font></td></tr>")
270          .append("</table>");
271       evictionBuf.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
272          .append(evictionTotal).append("</font></td></tr>")
273          .append("</table>");
274       hitsBuf.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
275          .append(hitsTotal).append("</font></td></tr>")
276          .append("</table>");
277       missesBuf.append("<tr><td><font color='red'>total</font></td><td><font color='red'>")
278          .append(missesTotal).append("</font></td></tr>")
279          .append("</table>");
280
281       content.append("<table><tr valign='top'><td>")
282          .append(reportsTable)
283          .append("</td><td>").append(contentionBuf)
284          .append("</td><td>").append(evictionBuf)
285          .append("</td><td>").append(hitsBuf)
286          .append("</td><td>").append(missesBuf)
287          .append("</td></tr></table>");
288
289       content.append(partitionBuf);
290    }
291
292    // Inner
293

294    class Contention
295    {
296       public final String JavaDoc tableName;
297       public int total;
298       public long maxTime;
299       public int count;
300
301       public Contention(String JavaDoc tableName)
302       {
303          this.tableName = tableName;
304       }
305    }
306
307    class Eviction
308    {
309       public final String JavaDoc tableName;
310       public int count;
311
312       public Eviction(String JavaDoc tableName)
313       {
314          this.tableName = tableName;
315       }
316    }
317
318    class Hit
319    {
320       public final String JavaDoc tableName;
321       public int count;
322       private int[] partitions;
323       private int maxHitPerPartition;
324
325       public Hit(String JavaDoc tableName)
326       {
327          this.tableName = tableName;
328       }
329
330       public void partitionHit(int partitionIndex, int count)
331       {
332          if(partitions == null)
333          {
334             partitions = new int[partitionIndex + 1];
335          }
336          else if(partitions.length < partitionIndex + 1)
337          {
338             int[] tmp = partitions;
339             partitions = new int[partitionIndex + 1];
340             System.arraycopy(tmp, 0, partitions, 0, tmp.length);
341          }
342          partitions[partitionIndex] += count;
343          this.count += count;
344
345          if(maxHitPerPartition < partitions[partitionIndex])
346          {
347             maxHitPerPartition = partitions[partitionIndex];
348          }
349       }
350    }
351
352    class Miss
353    {
354       public final String JavaDoc tableName;
355       public int count;
356
357       public Miss(String JavaDoc tableName)
358       {
359          this.tableName = tableName;
360       }
361    }
362 }
363
Popular Tags