@Override public void recordLoadSuccess(long loadTime) { try { delegate.recordLoadSuccess(loadTime); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
@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)); } }
@Override public @Nullable V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { requireNonNull(remappingFunction); boolean[] computed = { false }; for (;;) { CompletableFuture<V> future = delegate.get(key); V oldValue = Async.getWhenSuccessful(future); CompletableFuture<V> valueFuture = delegate.compute(key, (k, oldValueFuture) -> { if (future != oldValueFuture) { return oldValueFuture; } computed[0] = true; long startTime = delegate.statsTicker().read(); V newValue = remappingFunction.apply(key, oldValue); long loadTime = delegate.statsTicker().read() - startTime; if (newValue == null) { delegate.statsCounter().recordLoadFailure(loadTime); return null; } delegate.statsCounter().recordLoadSuccess(loadTime); return CompletableFuture.completedFuture(newValue); }, /* recordMiss */ false, /* recordLoad */ false); if (computed[0]) { return Async.getWhenSuccessful(valueFuture); } } }
@Test public void guarded_exception() { StatsCounter statsCounter = Mockito.mock(StatsCounter.class); when(statsCounter.snapshot()).thenThrow(new NullPointerException()); doThrow(NullPointerException.class).when(statsCounter).recordEviction(); doThrow(NullPointerException.class).when(statsCounter).recordHits(anyInt()); doThrow(NullPointerException.class).when(statsCounter).recordMisses(anyInt()); doThrow(NullPointerException.class).when(statsCounter).recordEviction(anyInt()); doThrow(NullPointerException.class).when(statsCounter).recordLoadSuccess(anyLong()); doThrow(NullPointerException.class).when(statsCounter).recordLoadFailure(anyLong()); StatsCounter guarded = StatsCounter.guardedStatsCounter(statsCounter); guarded.recordHits(1); guarded.recordMisses(1); guarded.recordEviction(); guarded.recordEviction(10); guarded.recordLoadSuccess(1); guarded.recordLoadFailure(1); assertThat(guarded.snapshot(), is(CacheStats.empty())); verify(statsCounter).recordHits(1); verify(statsCounter).recordMisses(1); verify(statsCounter).recordEviction(); verify(statsCounter).recordEviction(10); verify(statsCounter).recordLoadSuccess(1); verify(statsCounter).recordLoadFailure(1); } }
cache().statsCounter().recordLoadSuccess(loadTime);
@Test public void guarded() { StatsCounter counter = StatsCounter.guardedStatsCounter(new ConcurrentStatsCounter()); counter.recordHits(1); counter.recordMisses(1); counter.recordEviction(); counter.recordEviction(10); counter.recordLoadSuccess(1); counter.recordLoadFailure(1); CacheStats expected = new CacheStats(1, 1, 1, 1, 2, 2, 10); assertThat(counter.snapshot(), is(expected)); assertThat(counter.toString(), is(expected.toString())); assertThat(counter.snapshot().toString(), is(expected.toString())); }
long loadTime = cache().statsTicker().read() - startTime; if (success) { cache().statsCounter().recordLoadSuccess(loadTime); } else { cache().statsCounter().recordLoadFailure(loadTime);
cache().statsCounter().recordLoadSuccess(loadTime);
@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))); }
/** Decorates the remapping function to record statistics if enabled. */ default Function<? super K, ? extends V> statsAware( Function<? super K, ? extends V> mappingFunction, boolean recordLoad) { if (!isRecordingStats()) { return mappingFunction; } return key -> { V value; statsCounter().recordMisses(1); long startTime = statsTicker().read(); try { value = mappingFunction.apply(key); } catch (RuntimeException | Error e) { statsCounter().recordLoadFailure(statsTicker().read() - startTime); throw e; } long loadTime = statsTicker().read() - startTime; if (recordLoad) { if (value == null) { statsCounter().recordLoadFailure(loadTime); } else { statsCounter().recordLoadSuccess(loadTime); } } return value; }; }
statsCounter().recordLoadFailure(loadTime); } else { statsCounter().recordLoadSuccess(loadTime);
@Override public void recordLoadSuccess(long loadTime) { try { delegate.recordLoadSuccess(loadTime); } catch (Throwable t) { logger.log(Level.WARNING, "Exception thrown by stats counter", t); } }
asyncCache.cache().statsCounter().recordLoadFailure(loadTime); } else { asyncCache.cache().statsCounter().recordLoadSuccess(loadTime);
statsCounter().recordLoadFailure(loadTime); } else { statsCounter().recordLoadSuccess(loadTime);
cache().statsCounter().recordLoadFailure(loadTime); } else { cache().statsCounter().recordLoadSuccess(loadTime);
cache.statsCounter().recordLoadSuccess(loadTime);
@Override public @Nullable V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { requireNonNull(remappingFunction); boolean[] computed = { false }; for (;;) { CompletableFuture<V> future = delegate.get(key); V oldValue = Async.getWhenSuccessful(future); CompletableFuture<V> valueFuture = delegate.compute(key, (k, oldValueFuture) -> { if (future != oldValueFuture) { return oldValueFuture; } computed[0] = true; long startTime = delegate.statsTicker().read(); V newValue = remappingFunction.apply(key, oldValue); long loadTime = delegate.statsTicker().read() - startTime; if (newValue == null) { delegate.statsCounter().recordLoadFailure(loadTime); return null; } delegate.statsCounter().recordLoadSuccess(loadTime); return CompletableFuture.completedFuture(newValue); }, /* recordMiss */ false, /* recordLoad */ false); if (computed[0]) { return Async.getWhenSuccessful(valueFuture); } } }
long loadTime = cache().statsTicker().read() - startTime; if (success) { cache().statsCounter().recordLoadSuccess(loadTime); } else { cache().statsCounter().recordLoadFailure(loadTime);
/** Decorates the remapping function to record statistics if enabled. */ default Function<? super K, ? extends V> statsAware( Function<? super K, ? extends V> mappingFunction, boolean recordLoad) { if (!isRecordingStats()) { return mappingFunction; } return key -> { V value; statsCounter().recordMisses(1); long startTime = statsTicker().read(); try { value = mappingFunction.apply(key); } catch (RuntimeException | Error e) { statsCounter().recordLoadFailure(statsTicker().read() - startTime); throw e; } long loadTime = statsTicker().read() - startTime; if (recordLoad) { if (value == null) { statsCounter().recordLoadFailure(loadTime); } else { statsCounter().recordLoadSuccess(loadTime); } } return value; }; }
statsCounter().recordLoadFailure(loadTime); } else { statsCounter().recordLoadSuccess(loadTime);