Search in sources :

Example 1 with CacheStats

use of com.github.benmanes.caffeine.cache.stats.CacheStats in project caffeine by ben-manes.

the class HasStats method matchesSafely.

@Override
protected boolean matchesSafely(CacheContext context, Description description) {
    if (!context.isRecordingStats()) {
        return true;
    }
    CacheStats stats = context.stats();
    desc = new DescriptionBuilder(description);
    ForkJoinPool.commonPool().awaitQuiescence(10, TimeUnit.SECONDS);
    switch(type) {
        case HIT:
            return desc.expectThat(type.name(), stats.hitCount(), is(count)).matches();
        case MISS:
            return desc.expectThat(type.name(), stats.missCount(), is(count)).matches();
        case EVICTION_COUNT:
            return desc.expectThat(type.name(), stats.evictionCount(), is(count)).matches();
        case EVICTION_WEIGHT:
            return desc.expectThat(type.name(), stats.evictionWeight(), is(count)).matches();
        case LOAD_SUCCESS:
            return desc.expectThat(type.name(), stats.loadSuccessCount(), is(count)).matches();
        case LOAD_FAILURE:
            return desc.expectThat(type.name(), stats.loadFailureCount(), is(count)).matches();
        default:
            throw new AssertionError("Unknown stats type");
    }
}
Also used : DescriptionBuilder(com.github.benmanes.caffeine.testing.DescriptionBuilder) CacheStats(com.github.benmanes.caffeine.cache.stats.CacheStats)

Example 2 with CacheStats

use of com.github.benmanes.caffeine.cache.stats.CacheStats in project caffeine by ben-manes.

the class CacheStatsTest method testEmpty.

public void testEmpty() {
    CacheStats stats = new CacheStats(0, 0, 0, 0, 0, 0, 0);
    assertEquals(0, stats.requestCount());
    assertEquals(0, stats.hitCount());
    assertEquals(1.0, stats.hitRate());
    assertEquals(0, stats.missCount());
    assertEquals(0.0, stats.missRate());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(0, stats.loadFailureCount());
    assertEquals(0.0, stats.loadFailureRate());
    assertEquals(0, stats.loadCount());
    assertEquals(0, stats.totalLoadTime());
    assertEquals(0.0, stats.averageLoadPenalty());
    assertEquals(0, stats.evictionCount());
}
Also used : CacheStats(com.github.benmanes.caffeine.cache.stats.CacheStats)

Example 3 with CacheStats

use of com.github.benmanes.caffeine.cache.stats.CacheStats in project caffeine by ben-manes.

the class CacheStatsTest method testSingle.

public void testSingle() {
    CacheStats stats = new CacheStats(11, 13, 17, 19, 23, 27, 54);
    assertEquals(24, stats.requestCount());
    assertEquals(11, stats.hitCount());
    assertEquals(11.0 / 24, stats.hitRate());
    assertEquals(13, stats.missCount());
    assertEquals(13.0 / 24, stats.missRate());
    assertEquals(17, stats.loadSuccessCount());
    assertEquals(19, stats.loadFailureCount());
    assertEquals(19.0 / 36, stats.loadFailureRate());
    assertEquals(17 + 19, stats.loadCount());
    assertEquals(23, stats.totalLoadTime());
    assertEquals(23.0 / (17 + 19), stats.averageLoadPenalty());
    assertEquals(27, stats.evictionCount());
    assertEquals(54, stats.evictionWeight());
}
Also used : CacheStats(com.github.benmanes.caffeine.cache.stats.CacheStats)

Example 4 with CacheStats

use of com.github.benmanes.caffeine.cache.stats.CacheStats in project narchy by automenta.

the class CaffeineMemoize method summary.

@Override
public String summary() {
    CacheStats stats = cache.stats();
    String a;
    if (stats.hitCount() > 0)
        a = n2(stats.hitRate() * 100f) + "% hits, ";
    else
        a = "";
    return a + cache.estimatedSize() + " size";
}
Also used : CacheStats(com.github.benmanes.caffeine.cache.stats.CacheStats)

Example 5 with CacheStats

use of com.github.benmanes.caffeine.cache.stats.CacheStats in project druid by druid-io.

the class CaffeineCache method doMonitor.

@Override
public void doMonitor(ServiceEmitter emitter) {
    final CacheStats oldStats = priorStats.get();
    final CacheStats newStats = cache.stats();
    final CacheStats deltaStats = newStats.minus(oldStats);
    final ServiceMetricEvent.Builder builder = ServiceMetricEvent.builder();
    emitter.emit(builder.build("query/cache/caffeine/delta/requests", deltaStats.requestCount()));
    emitter.emit(builder.build("query/cache/caffeine/total/requests", newStats.requestCount()));
    emitter.emit(builder.build("query/cache/caffeine/delta/loadTime", deltaStats.totalLoadTime()));
    emitter.emit(builder.build("query/cache/caffeine/total/loadTime", newStats.totalLoadTime()));
    emitter.emit(builder.build("query/cache/caffeine/delta/evictionBytes", deltaStats.evictionWeight()));
    emitter.emit(builder.build("query/cache/caffeine/total/evictionBytes", newStats.evictionWeight()));
    if (!priorStats.compareAndSet(oldStats, newStats)) {
        // ISE for stack trace
        log.warn(new IllegalStateException("Multiple monitors"), "Multiple monitors on the same cache causing race conditions and unreliable stats reporting");
    }
}
Also used : CacheStats(com.github.benmanes.caffeine.cache.stats.CacheStats) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent)

Aggregations

CacheStats (com.github.benmanes.caffeine.cache.stats.CacheStats)12 CacheSpec (com.github.benmanes.caffeine.cache.testing.CacheSpec)1 CheckNoStats (com.github.benmanes.caffeine.cache.testing.CheckNoStats)1 CheckNoWriter (com.github.benmanes.caffeine.cache.testing.CheckNoWriter)1 DescriptionBuilder (com.github.benmanes.caffeine.testing.DescriptionBuilder)1 ServiceMetricEvent (com.metamx.emitter.service.ServiceMetricEvent)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ServiceMetricEvent (org.apache.druid.java.util.emitter.service.ServiceMetricEvent)1 CacheKey (org.apache.metron.enrichment.bolt.CacheKey)1 JSONObject (org.json.simple.JSONObject)1 Test (org.testng.annotations.Test)1