Clover coverage report -
Coverage timestamp: Sa Jul 7 2007 09:11:40 CEST
file stats: LOC: 295   Methods: 22
NCLOC: 110   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StatisticListenerImpl.java 87,5% 97,1% 95,5% 95,3%
coverage coverage
 1    /*
 2    * Copyright (c) 2002-2007 by OpenSymphony
 3    * All rights reserved.
 4    */
 5    package com.opensymphony.oscache.extra;
 6   
 7    import com.opensymphony.oscache.base.Cache;
 8    import com.opensymphony.oscache.base.events.CacheEntryEvent;
 9    import com.opensymphony.oscache.base.events.CacheEntryEventListener;
 10    import com.opensymphony.oscache.base.events.CacheGroupEvent;
 11    import com.opensymphony.oscache.base.events.CacheMapAccessEvent;
 12    import com.opensymphony.oscache.base.events.CacheMapAccessEventListener;
 13    import com.opensymphony.oscache.base.events.CacheMapAccessEventType;
 14    import com.opensymphony.oscache.base.events.CachePatternEvent;
 15    import com.opensymphony.oscache.base.events.CachewideEvent;
 16    import com.opensymphony.oscache.base.events.ScopeEvent;
 17    import com.opensymphony.oscache.base.events.ScopeEventListener;
 18    import com.opensymphony.oscache.extra.ScopeEventListenerImpl;
 19   
 20    /**
 21    * A simple implementation of a statistic reporter which uses the
 22    * event listeners. It uses the events to count the cache hit and
 23    * misses and of course the flushes.
 24    * <p>
 25    * We are not using any synchronized so that this does not become a bottleneck.
 26    * The consequence is that on retrieving values, the operations that are
 27    * currently being done won't be counted.
 28    */
 29    public class StatisticListenerImpl implements CacheMapAccessEventListener,
 30    CacheEntryEventListener, ScopeEventListener {
 31   
 32    /**
 33    * Hit counter.
 34    */
 35    private static int hitCount = 0;
 36   
 37    /**
 38    * Miss counter.
 39    */
 40    private static int missCount = 0;
 41   
 42    /**
 43    * Stale hit counter.
 44    */
 45    private static int staleHitCount = 0;
 46   
 47    /**
 48    * Hit counter sum.
 49    */
 50    private static int hitCountSum = 0;
 51   
 52    /**
 53    * Miss counter sum.
 54    */
 55    private static int missCountSum = 0;
 56   
 57    /**
 58    * Stale hit counter.
 59    */
 60    private static int staleHitCountSum = 0;
 61   
 62    /**
 63    * Flush hit counter.
 64    */
 65    private static int flushCount = 0;
 66   
 67    /**
 68    * Miss counter sum.
 69    */
 70    private static int entriesAdded = 0;
 71   
 72    /**
 73    * Stale hit counter.
 74    */
 75    private static int entriesRemoved = 0;
 76   
 77    /**
 78    * Flush hit counter.
 79    */
 80    private static int entriesUpdated = 0;
 81   
 82    /**
 83    * Constructor, empty for us.
 84    */
 85  34 public StatisticListenerImpl() {
 86   
 87    }
 88   
 89    /**
 90    * This method handles an event each time the cache is accessed.
 91    *
 92    * @param event
 93    * The event triggered when the cache was accessed
 94    * @see com.opensymphony.oscache.base.events.CacheMapAccessEventListener#accessed(CacheMapAccessEvent)
 95    */
 96  97 public void accessed(CacheMapAccessEvent event) {
 97    // Retrieve the event type and update the counters
 98  97 CacheMapAccessEventType type = event.getEventType();
 99   
 100    // Handles a hit event
 101  97 if (type == CacheMapAccessEventType.HIT) {
 102  62 hitCount++;
 103  35 } else if (type == CacheMapAccessEventType.STALE_HIT) { // Handles a
 104    // stale hit
 105    // event
 106  28 staleHitCount++;
 107  7 } else if (type == CacheMapAccessEventType.MISS) { // Handles a miss
 108    // event
 109  7 missCount++;
 110    }
 111    }
 112   
 113    /**
 114    * Logs the flush of the cache.
 115    *
 116    * @param info the string to be logged.
 117    */
 118  51 private void flushed(String info) {
 119  51 flushCount++;
 120   
 121  51 hitCountSum += hitCount;
 122  51 staleHitCountSum += staleHitCount;
 123  51 missCountSum += missCount;
 124   
 125  51 hitCount = 0;
 126  51 staleHitCount = 0;
 127  51 missCount = 0;
 128    }
 129   
 130    /**
 131    * Event fired when a specific or all scopes are flushed.
 132    *
 133    * @param event ScopeEvent
 134    * @see com.opensymphony.oscache.base.events.ScopeEventListener#scopeFlushed(ScopeEvent)
 135    */
 136  10 public void scopeFlushed(ScopeEvent event) {
 137  10 flushed("scope " + ScopeEventListenerImpl.SCOPE_NAMES[event.getScope()]);
 138    }
 139   
 140    /**
 141    * Event fired when an entry is added to the cache.
 142    *
 143    * @param event CacheEntryEvent
 144    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryAdded(CacheEntryEvent)
 145    */
 146  69 public void cacheEntryAdded(CacheEntryEvent event) {
 147  69 entriesAdded++;
 148    }
 149   
 150    /**
 151    * Event fired when an entry is flushed from the cache.
 152    *
 153    * @param event CacheEntryEvent
 154    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryFlushed(CacheEntryEvent)
 155    */
 156  21 public void cacheEntryFlushed(CacheEntryEvent event) {
 157    // do nothing, because a group or other flush is coming
 158  21 if (!Cache.NESTED_EVENT.equals(event.getOrigin())) {
 159  9 flushed("entry " + event.getKey() + " / " + event.getOrigin());
 160    }
 161    }
 162   
 163    /**
 164    * Event fired when an entry is removed from the cache.
 165    *
 166    * @param event CacheEntryEvent
 167    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryRemoved(CacheEntryEvent)
 168    */
 169  5 public void cacheEntryRemoved(CacheEntryEvent event) {
 170  5 entriesRemoved++;
 171    }
 172   
 173    /**
 174    * Event fired when an entry is updated in the cache.
 175    *
 176    * @param event CacheEntryEvent
 177    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryUpdated(CacheEntryEvent)
 178    */
 179  12 public void cacheEntryUpdated(CacheEntryEvent event) {
 180  12 entriesUpdated++;
 181    }
 182   
 183    /**
 184    * Event fired when a group is flushed from the cache.
 185    *
 186    * @param event CacheGroupEvent
 187    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheGroupFlushed(CacheGroupEvent)
 188    */
 189  14 public void cacheGroupFlushed(CacheGroupEvent event) {
 190  14 flushed("group " + event.getGroup());
 191    }
 192   
 193    /**
 194    * Event fired when a key pattern is flushed from the cache.
 195    *
 196    * @param event CachePatternEvent
 197    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cachePatternFlushed(CachePatternEvent)
 198    */
 199  11 public void cachePatternFlushed(CachePatternEvent event) {
 200  11 flushed("pattern " + event.getPattern());
 201    }
 202   
 203    /**
 204    * An event that is fired when an entire cache gets flushed.
 205    *
 206    * @param event CachewideEvent
 207    * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheFlushed(CachewideEvent)
 208    */
 209  7 public void cacheFlushed(CachewideEvent event) {
 210  7 flushed("wide " + event.getDate());
 211    }
 212   
 213    /**
 214    * Return the counters in a string form.
 215    *
 216    * @return String
 217    */
 218  0 public String toString() {
 219  0 return "StatisticListenerImpl: Hit = " + hitCount + " / " + hitCountSum
 220    + ", stale hit = " + staleHitCount + " / " + staleHitCountSum
 221    + ", miss = " + missCount + " / " + missCountSum + ", flush = "
 222    + flushCount + ", entries (added, removed, updates) = "
 223    + entriesAdded + ", " + entriesRemoved + ", " + entriesUpdated;
 224    }
 225   
 226    /**
 227    * @return Returns the entriesAdded.
 228    */
 229  10 public int getEntriesAdded() {
 230  10 return entriesAdded;
 231    }
 232   
 233    /**
 234    * @return Returns the entriesRemoved.
 235    */
 236  10 public int getEntriesRemoved() {
 237  10 return entriesRemoved;
 238    }
 239   
 240    /**
 241    * @return Returns the entriesUpdated.
 242    */
 243  10 public int getEntriesUpdated() {
 244  10 return entriesUpdated;
 245    }
 246   
 247    /**
 248    * @return Returns the flushCount.
 249    */
 250  10 public int getFlushCount() {
 251  10 return flushCount;
 252    }
 253   
 254    /**
 255    * @return Returns the hitCount.
 256    */
 257  5 public int getHitCount() {
 258  5 return hitCount;
 259    }
 260   
 261    /**
 262    * @return Returns the hitCountSum.
 263    */
 264  5 public int getHitCountSum() {
 265  5 return hitCountSum;
 266    }
 267   
 268    /**
 269    * @return Returns the missCount.
 270    */
 271  5 public int getMissCount() {
 272  5 return missCount;
 273    }
 274   
 275    /**
 276    * @return Returns the missCountSum.
 277    */
 278  5 public int getMissCountSum() {
 279  5 return missCountSum;
 280    }
 281   
 282    /**
 283    * @return Returns the staleHitCount.
 284    */
 285  5 public int getStaleHitCount() {
 286  5 return staleHitCount;
 287    }
 288   
 289    /**
 290    * @return Returns the staleHitCountSum.
 291    */
 292  5 public int getStaleHitCountSum() {
 293  5 return staleHitCountSum;
 294    }
 295    }