com.github.benmanes.caffeine.cache.stats.StatsCounter Java Examples

The following examples show how to use com.github.benmanes.caffeine.cache.stats.StatsCounter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CaffeineTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test
public void recordStats_twice() {
  Supplier<StatsCounter> supplier = () -> statsCounter;
  Runnable[] tasks = {
      () -> Caffeine.newBuilder().recordStats().recordStats(),
      () -> Caffeine.newBuilder().recordStats(supplier).recordStats(),
      () -> Caffeine.newBuilder().recordStats().recordStats(supplier),
      () -> Caffeine.newBuilder().recordStats(supplier).recordStats(supplier),
  };
  for (Runnable task : tasks) {
    try {
      task.run();
      Assert.fail();
    } catch (IllegalStateException expected) {}
  }
}
 
Example #2
Source File: AbstractCacheTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testEmptySimpleStats() {
  StatsCounter counter = new ConcurrentStatsCounter();
  CacheStats stats = counter.snapshot();
  assertEquals(0, stats.requestCount());
  assertEquals(0, stats.hitCount());
  assertEquals(1.0, stats.hitRate(), 0.0);
  assertEquals(0, stats.missCount());
  assertEquals(0.0, stats.missRate(), 0.0);
  assertEquals(0, stats.loadSuccessCount());
  assertEquals(0, stats.loadFailureCount());
  assertEquals(0, stats.loadCount());
  assertEquals(0, stats.totalLoadTime());
  assertEquals(0.0, stats.averageLoadPenalty(), 0.0);
  assertEquals(0, stats.evictionCount());
}
 
Example #3
Source File: AbstractCacheTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testSingleSimpleStats() {
  StatsCounter counter = new ConcurrentStatsCounter();
  for (int i = 0; i < 11; i++) {
    counter.recordHits(1);
  }
  for (int i = 0; i < 13; i++) {
    counter.recordLoadSuccess(i);
  }
  for (int i = 0; i < 17; i++) {
    counter.recordLoadFailure(i);
  }
  for (int i = 0; i < 23; i++) {
    counter.recordMisses(1);
  }
  for (int i = 0; i < 27; i++) {
    counter.recordEviction(1, RemovalCause.SIZE);
  }
  CacheStats stats = counter.snapshot();
  int requestCount = 11 + 23;
  assertEquals(requestCount, stats.requestCount());
  assertEquals(11, stats.hitCount());
  assertEquals(11.0 / requestCount, stats.hitRate(), 0.0);
  int missCount = 23;
  assertEquals(missCount, stats.missCount());
  assertEquals(((double) missCount) / requestCount, stats.missRate(), 0.0);
  assertEquals(13, stats.loadSuccessCount());
  assertEquals(17, stats.loadFailureCount());
  assertEquals(13 + 17, stats.loadCount());
  assertEquals(214, stats.totalLoadTime());
  assertEquals(214.0 / (13 + 17), stats.averageLoadPenalty(), 0.0);
  assertEquals(27, stats.evictionCount());
}
 
Example #4
Source File: AbstractCacheTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
public void testSimpleStatsOverflow() {
  StatsCounter counter = new ConcurrentStatsCounter();
  counter.recordLoadSuccess(Long.MAX_VALUE);
  counter.recordLoadSuccess(1);
  CacheStats stats = counter.snapshot();
  assertEquals(Long.MAX_VALUE, stats.totalLoadTime());
}
 
Example #5
Source File: CaffeineTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void recordStats_custom() {
  Supplier<StatsCounter> supplier = () -> statsCounter;
  Caffeine<?, ?> builder = Caffeine.newBuilder().recordStats(supplier);
  builder.statsCounterSupplier.get().recordEviction(1, RemovalCause.SIZE);
  verify(statsCounter).recordEviction(1, RemovalCause.SIZE);
  builder.build();
}
 
Example #6
Source File: UnboundedLocalCache.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@Override
public StatsCounter statsCounter() {
  return statsCounter;
}
 
Example #7
Source File: LocalCache.java    From caffeine with Apache License 2.0 4 votes vote down vote up
/** Returns the {@link StatsCounter} used by this cache. */
@NonNull StatsCounter statsCounter();
 
Example #8
Source File: Caffeine.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@NonNull
Supplier<StatsCounter> getStatsCounterSupplier() {
  return (statsCounterSupplier == null)
      ? StatsCounter::disabledStatsCounter
      : statsCounterSupplier;
}
 
Example #9
Source File: BoundedLocalCache.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@Override
public StatsCounter statsCounter() {
  return StatsCounter.disabledStatsCounter();
}
 
Example #10
Source File: Caffeine.java    From caffeine with Apache License 2.0 3 votes vote down vote up
/**
 * Enables the accumulation of {@link CacheStats} during the operation of the cache. Without this
 * {@link Cache#stats} will return zero for all statistics. Note that recording statistics
 * requires bookkeeping to be performed with each operation, and thus imposes a performance
 * penalty on cache operation. Any exception thrown by the supplied {@link StatsCounter} will be
 * suppressed and logged.
 *
 * @param statsCounterSupplier a supplier instance that returns a new {@link StatsCounter}
 * @return this {@code Caffeine} instance (for chaining)
 */
@NonNull
public Caffeine<K, V> recordStats(
    @NonNull Supplier<? extends StatsCounter> statsCounterSupplier) {
  requireState(this.statsCounterSupplier == null, "Statistics recording was already set");
  requireNonNull(statsCounterSupplier);
  this.statsCounterSupplier = () -> StatsCounter.guardedStatsCounter(statsCounterSupplier.get());
  return this;
}