@Override public CacheStats snapshot() { return CacheStats.empty(); }
@Test public void disabled() { StatsCounter counter = DisabledStatsCounter.INSTANCE; counter.recordHits(1); counter.recordMisses(1); counter.recordLoadSuccess(1); counter.recordLoadFailure(1); assertThat(counter.snapshot(), is(new CacheStats(0, 0, 0, 0, 0, 0, 0))); assertThat(counter.toString(), is(new CacheStats(0, 0, 0, 0, 0, 0, 0).toString())); for (DisabledStatsCounter type : DisabledStatsCounter.values()) { assertThat(DisabledStatsCounter.valueOf(type.name()), is(counter)); } }
@Test public void concurrent() { StatsCounter counter = new ConcurrentStatsCounter(); ConcurrentTestHarness.timeTasks(5, () -> { counter.recordHits(1); counter.recordMisses(1); counter.recordEviction(); counter.recordEviction(10); counter.recordLoadSuccess(1); counter.recordLoadFailure(1); }); assertThat(counter.snapshot(), is(new CacheStats(5, 5, 5, 5, 10, 10, 50))); }
@Test public void minus() { CacheStats one = new CacheStats(11, 13, 17, 19, 23, 27, 54); CacheStats two = new CacheStats(53, 47, 43, 41, 37, 31, 62); CacheStats diff = two.minus(one); checkStats(diff, 76, 42, 42.0 / 76, 34, 34.0 / 76, 26, 22, 22.0 / 48, 26 + 22, 14, 14.0 / (26 + 22), 4, 8); assertThat(one.minus(two), is(CacheStats.empty())); }
/** * Returns the ratio of cache requests which were hits. This is defined as * {@code hitCount / requestCount}, or {@code 1.0} when {@code requestCount == 0}. Note that * {@code hitRate + missRate =~ 1.0}. * * @return the ratio of cache requests which were hits */ @NonNegative public double hitRate() { long requestCount = requestCount(); return (requestCount == 0) ? 1.0 : (double) hitCount / requestCount; }
static void printStats(Cache<Long, Long> cache) { if (debug) { System.out.printf("size %,d requests %,d hit ratio %f%n", cache.estimatedSize(), cache.stats().requestCount(), cache.stats().hitRate()); } }
public void plus() { CacheStats one = new CacheStats(11, 13, 15, 13, 11, 9, 18); CacheStats two = new CacheStats(53, 47, 41, 39, 37, 35, 70); CacheStats sum = two.plus(one); checkStats(sum, 124, 64, 64.0 / 124, 60, 60.0 / 124, 56, 52, 52.0 / 108, 56 + 52, 48, 48.0 / (56 + 52), 44, 88); assertThat(sum, is(one.plus(two))); }
@Test(dataProvider = "badArgs", expectedExceptions = IllegalArgumentException.class) public void invalid(int hitCount, int missCount, int loadSuccessCount, int loadFailureCount, int totalLoadTime, int evictionCount, int evictionWeight) { new CacheStats(hitCount, missCount, loadSuccessCount, loadFailureCount, totalLoadTime, evictionCount, evictionWeight); }
@Override @SuppressWarnings("deprecation") public void recordEviction() { try { delegate.recordEviction(); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
@Override public void recordLoadSuccess(long loadTime) { try { delegate.recordLoadSuccess(loadTime); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
@Override public void recordLoadFailure(long loadTime) { try { delegate.recordLoadFailure(loadTime); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
@Override public void recordMisses(int count) { try { delegate.recordMisses(count); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
@Override public void recordHits(int count) { try { delegate.recordHits(count); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
@Override public StatsCounter statsCounter() { return StatsCounter.disabledStatsCounter(); }
/** * Returns an accumulator that suppresses and logs any exception thrown by the delegate * <tt>statsCounter</tt>. * * @param statsCounter the accumulator to delegate to * @return an accumulator that suppresses and logs any exception thrown by the delegate */ static @NonNull StatsCounter guardedStatsCounter(@NonNull StatsCounter statsCounter) { return new GuardedStatsCounter(statsCounter); } }
@Override public CacheStats snapshot() { return new CacheStats( hitCount.sum(), missCount.sum(), loadSuccessCount.sum(), loadFailureCount.sum(), totalLoadTime.sum(), evictionCount.sum(), evictionWeight.sum()); }
/** * Records the eviction of an entry from the cache. This should only been called when an entry is * evicted due to the cache's eviction strategy, and not as a result of manual * {@link Cache#invalidate invalidations}. * * @param weight the weight of the evicted entry */ default void recordEviction(@NonNegative int weight) { // This method will be abstract in version 3.0.0 recordEviction(); }