Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marceloverdijk/1a25b6699c30ede7650c to your computer and use it in GitHub Desktop.
Save marceloverdijk/1a25b6699c30ede7650c to your computer and use it in GitHub Desktop.
import com.google.common.cache.Cache;
import com.google.common.cache.CacheStats;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CacheMetricsTests {
private SimpleCacheManager cacheManager;
private CacheMetrics cacheMetrics;
@Before
public void setUp() {
this.cacheManager = new SimpleCacheManager();
this.cacheMetrics = new CacheMetrics(cacheManager);
}
@Test
public void cache_metrics_should_return_metrics() {
Cache cache1 = mock(Cache.class);
CacheStats cache1Stats = new CacheStats(40L, 20L, 0L, 0L, 0L, 10L);
when(cache1.size()).thenReturn(5L);
when(cache1.stats()).thenReturn(cache1Stats);
Cache cache2 = mock(Cache.class);
CacheStats cache2Stats = new CacheStats(20L, 40L, 0L, 0L, 0L, 5L);
when(cache2.size()).thenReturn(2L);
when(cache2.stats()).thenReturn(cache2Stats);
cacheManager.setCaches(asList(
new GuavaCache("cache1", cache1),
new GuavaCache("cache2", cache2)
));
cacheManager.afterPropertiesSet();
Metric<?>[] metrics = cacheMetrics.metrics().toArray(new Metric<?>[] {});
assertThat(metrics.length, is(14));
assertThat(metrics[0].getName(), is("cache.cache1.size"));
assertThat(metrics[0].getValue(), is(5L));
assertThat(metrics[1].getName(), is("cache.cache1.eviction_count"));
assertThat(metrics[1].getValue(), is(10L));
assertThat(metrics[2].getName(), is("cache.cache1.hit_count"));
assertThat(metrics[2].getValue(), is(40L));
assertThat(metrics[3].getName(), is("cache.cache1.hit_rate"));
assertThat(metrics[3].getValue(), is(0.6666666666666666)); // hit_count / request_count
assertThat(metrics[4].getName(), is("cache.cache1.miss_count"));
assertThat(metrics[4].getValue(), is(20L));
assertThat(metrics[5].getName(), is("cache.cache1.miss_rate"));
assertThat(metrics[5].getValue(), is(0.3333333333333333)); // miss_count / request_count
assertThat(metrics[6].getName(), is("cache.cache1.request_count"));
assertThat(metrics[6].getValue(), is(60L)); // hit_count + miss_count
assertThat(metrics[7].getName(), is("cache.cache2.size"));
assertThat(metrics[7].getValue(), is(2L));
assertThat(metrics[8].getName(), is("cache.cache2.eviction_count"));
assertThat(metrics[8].getValue(), is(5L));
assertThat(metrics[9].getName(), is("cache.cache2.hit_count"));
assertThat(metrics[9].getValue(), is(20L));
assertThat(metrics[10].getName(), is("cache.cache2.hit_rate"));
assertThat(metrics[10].getValue(), is(0.3333333333333333)); // hit_count / request_count
assertThat(metrics[11].getName(), is("cache.cache2.miss_count"));
assertThat(metrics[11].getValue(), is(40L));
assertThat(metrics[12].getName(), is("cache.cache2.miss_rate"));
assertThat(metrics[12].getValue(), is(0.6666666666666666)); // miss_count / request_count
assertThat(metrics[13].getName(), is("cache.cache2.request_count"));
assertThat(metrics[13].getValue(), is(60L)); // hit_count + miss_count
}
@Test
public void cahe_metrics_should_return_no_metrics_when_cache_is_no_guava_cache() {
cacheManager.setCaches(asList(new ConcurrentMapCache("cache1")));
cacheManager.afterPropertiesSet();
assertThat(cacheManager.getCacheNames(), contains("cache1")); // make sure there is a cache instance
assertThat(cacheMetrics.metrics().size(), is(0)); // metrics should be empty
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment