1 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 ; 28 import java.util.Iterator ; 29 import java.util.HashMap ; 30 31 35 public class CacheReportGenerator 36 extends ReportGenerator 37 { 38 protected void content(String reportName, StringBuffer content) throws Exception 39 { 40 StringBuffer contentionBuf = new StringBuffer (); 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 evictionBuf = new StringBuffer (); 47 evictionBuf.append("<table><tr><th>Eviction per table</th><th>count</th></tr>"); 48 int evictionTotal = 0; 49 StringBuffer hitsBuf = new StringBuffer (); 50 hitsBuf.append("<table><tr><th>Hits per table</th><th>count</th></tr>"); 51 int hitsTotal = 0; 52 StringBuffer missesBuf = new StringBuffer (); 53 missesBuf.append("<table><tr><th>Misses per table</th><th>count</th></tr>"); 54 int missesTotal = 0; 55 56 StringBuffer reportsTable = new StringBuffer (); 57 reportsTable.append("<table><tr><th>Transaction started by</th><th>Total</th></tr>"); 58 59 int txTotal = 0; 60 61 Map contention = new HashMap (); 62 Map eviction = new HashMap (); 63 Map hits = new HashMap (); 64 Map misses = new HashMap (); 65 66 Iterator reports = getReportsIterator(); 67 while(reports.hasNext()) 68 { 69 TxReport report = (TxReport) reports.next(); 70 71 Map contentionMap = (Map ) report.getStats().get(CacheListener.ContentionStats.NAME); 72 Map evictionMap = (Map ) report.getStats().get(CacheListener.EvictionStats.NAME); 73 Map hitsMap = (Map ) report.getStats().get(CacheListener.HitStats.NAME); 74 Map missesMap = (Map ) 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 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 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 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 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 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 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 partitionBuf = new StringBuffer (); 231 partitionBuf.append("<table>"); 232 for(Iterator 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 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 294 class Contention 295 { 296 public final String tableName; 297 public int total; 298 public long maxTime; 299 public int count; 300 301 public Contention(String tableName) 302 { 303 this.tableName = tableName; 304 } 305 } 306 307 class Eviction 308 { 309 public final String tableName; 310 public int count; 311 312 public Eviction(String tableName) 313 { 314 this.tableName = tableName; 315 } 316 } 317 318 class Hit 319 { 320 public final String tableName; 321 public int count; 322 private int[] partitions; 323 private int maxHitPerPartition; 324 325 public Hit(String 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 tableName; 355 public int count; 356 357 public Miss(String tableName) 358 { 359 this.tableName = tableName; 360 } 361 } 362 } 363 | Popular Tags |