Skip to content

Commit

Permalink
Add metric for maximum size and weighted size of cache (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamuel-bs authored and schlosna committed Jul 24, 2018
1 parent 3116b67 commit 79229c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ protected Long loadValue() {
}
});

cache.policy().eviction().ifPresent(eviction -> {
eviction.weightedSize().ifPresent(weightedSize -> cacheMetrics.put(
cacheMetricName("weighted", "size"),
(Gauge<Long>) () -> weightedSize));
cacheMetrics.put(
cacheMetricName("maximum", "size"),
new CachedGauge<Long>(clock, 500, TimeUnit.MILLISECONDS) {
@Override
protected Long loadValue() {
return eviction.getMaximum();
}
});
});

cacheMetrics.put(cacheMetricName("request", "count"),
derivedGauge(CacheStats::requestCount));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,46 @@
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Policy;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.palantir.tritium.metrics.MetricRegistries;
import com.palantir.tritium.metrics.TestClock;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class CaffeineCacheMetricSetTest {
private static final long MAXIMUM_CACHE_SIZE = 1234L;
private static final long WEIGHTED_CACHE_SIZE = 123L;

private final MetricRegistry metrics = new MetricRegistry();
private final TestClock clock = new TestClock();

@Mock
private LoadingCache<Integer, String> cache;

@Mock
private Policy<Integer, String> policy;

@Mock
private Policy.Eviction<Integer, String> evictionPolicy;

@Before
public void before() {
when(cache.policy()).thenReturn(policy);
when(policy.eviction()).thenReturn(Optional.of(evictionPolicy));
when(evictionPolicy.getMaximum()).thenReturn(MAXIMUM_CACHE_SIZE);
when(evictionPolicy.weightedSize()).thenReturn(OptionalLong.of(WEIGHTED_CACHE_SIZE));
}

@After
public void after() {
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
Expand All @@ -68,9 +88,11 @@ public void testRegisterCache() {
"test1.cache.load.average.millis",
"test1.cache.load.failure.count",
"test1.cache.load.success.count",
"test1.cache.maximum.size",
"test1.cache.miss.count",
"test1.cache.miss.ratio",
"test1.cache.request.count"
"test1.cache.request.count",
"test1.cache.weighted.size"
);

when(cache.stats()).thenReturn(new CacheStats(1L, 2L, 3L, 4L, 5L, 6L));
Expand All @@ -82,7 +104,9 @@ public void testRegisterCache() {
assertThat(gauges.get("test1.cache.hit.ratio").getValue()).isEqualTo(1.0 / 3.0);
assertThat(gauges.get("test1.cache.miss.count").getValue()).isEqualTo(2L);
assertThat(gauges.get("test1.cache.miss.ratio").getValue()).isEqualTo(2.0 / 3.0);
assertThat(gauges.get("test1.cache.maximum.size").getValue()).isEqualTo(MAXIMUM_CACHE_SIZE);
assertThat(gauges.get("test1.cache.estimated.size").getValue()).isEqualTo(42L);
assertThat(gauges.get("test1.cache.weighted.size").getValue()).isEqualTo(WEIGHTED_CACHE_SIZE);
assertThat(gauges.get("test1.cache.eviction.count").getValue()).isEqualTo(6L);
assertThat(gauges.get("test1.cache.load.average.millis").getValue()).isNotEqualTo(5.0 / 3.0);
assertThat(gauges.get("test1.cache.load.failure.count").getValue()).isEqualTo(4L);
Expand Down Expand Up @@ -116,9 +140,12 @@ public void testNoStats() {
"test2.cache.load.average.millis",
"test2.cache.load.failure.count",
"test2.cache.load.success.count",
"test2.cache.maximum.size",
"test2.cache.miss.count",
"test2.cache.miss.ratio",
"test2.cache.request.count");
"test2.cache.request.count",
"test2.cache.weighted.size"
);

when(cache.stats()).thenReturn(new CacheStats(0L, 0L, 0L, 0L, 0L, 0L));
assertThat(metrics.getGauges().get("test2.cache.request.count").getValue()).isEqualTo(0L);
Expand Down

0 comments on commit 79229c3

Please sign in to comment.