|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 5 |
|
| 6 | +import com.google.common.cache.Cache; |
| 7 | +import com.google.common.cache.CacheBuilder; |
6 | 8 | import com.typesafe.config.Config; |
7 | 9 | import com.typesafe.config.ConfigFactory; |
8 | 10 | import io.micrometer.core.instrument.Counter; |
|
13 | 15 | import java.util.HashMap; |
14 | 16 | import java.util.List; |
15 | 17 | import java.util.Map; |
| 18 | +import java.util.concurrent.Callable; |
16 | 19 | import java.util.concurrent.TimeUnit; |
17 | 20 | import java.util.concurrent.atomic.AtomicInteger; |
18 | 21 | import java.util.stream.Collectors; |
@@ -151,9 +154,66 @@ public void testDistributionSummary() { |
151 | 154 | assertEquals(1, distribution.count()); |
152 | 155 | assertEquals(100, distribution.totalAmount()); |
153 | 156 | assertTrue( |
154 | | - Arrays.stream(distribution.takeSnapshot().percentileValues()).map(m -> m.percentile()) |
155 | | - .collect( |
156 | | - Collectors.toList()).containsAll(List.of(0.5, 0.95, 0.99))); |
| 157 | + Arrays.stream(distribution.takeSnapshot().percentileValues()) |
| 158 | + .map(m -> m.percentile()) |
| 159 | + .collect(Collectors.toList()) |
| 160 | + .containsAll(List.of(0.5, 0.95, 0.99))); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testCache() throws Exception { |
| 165 | + initializeCustomRegistry(List.of("testing")); |
| 166 | + Cache<String, Integer> cache = CacheBuilder.newBuilder().maximumSize(10).recordStats().build(); |
| 167 | + |
| 168 | + PlatformMetricsRegistry.registerCache("my.cache", cache); |
| 169 | + Callable<Integer> loader = |
| 170 | + new Callable<Integer>() { |
| 171 | + @Override |
| 172 | + public Integer call() throws Exception { |
| 173 | + return -1; |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + // Doing some cache activity |
| 178 | + cache.put("One", 1); |
| 179 | + cache.put("Two", 2); |
| 180 | + cache.get("One", loader); // hit |
| 181 | + cache.get("Two", loader); // hit |
| 182 | + cache.get("Two", loader); // hit |
| 183 | + cache.get("Three", loader); // miss hence loaded from loader |
| 184 | + cache.get("Failed", loader); // miss hence loaded from loader |
| 185 | + /* |
| 186 | + * Cache = {One,Two, Three, Failed} |
| 187 | + * Cache Hit is basically the number of times cache.get returned an entry which was present in the cache, hence hit = 3 |
| 188 | + * Cache Miss is basically the number of times cache.get returned an entry not present in the cache (hence loaded from loader), hence miss = 2 |
| 189 | + * The way cache.get works is that if there is a cache miss then the entry is loaded from the loader and included in the cache, |
| 190 | + hence the cache size due to the above activity would be 2 (already put One,Two) + 2 (cache miss on Three, Failed) = 4 |
| 191 | +
|
| 192 | + Expected hit count = 3.0, miss count = 2.0, cache size = 4 |
| 193 | + */ |
| 194 | + |
| 195 | + // Checking Cache Stats from registry |
| 196 | + double hits = 0, misses = 0,size = 0; |
| 197 | + hits = PlatformMetricsRegistry.getMeterRegistry().get("cache.gets").tag("result","hit").meter().measure().iterator().next().getValue(); |
| 198 | + misses = PlatformMetricsRegistry.getMeterRegistry().get("cache.gets").tag("result","miss").meter().measure().iterator().next().getValue(); |
| 199 | + size = PlatformMetricsRegistry.getMeterRegistry().get("cache.size").meter().measure().iterator().next().getValue(); |
| 200 | + |
| 201 | + assertEquals(3.0, hits); |
| 202 | + assertEquals(2.0, misses); |
| 203 | + assertEquals(4.0,size); |
| 204 | + |
| 205 | + // Doing some more cache activity |
| 206 | + cache.get("NotPresent", loader); // miss hence loaded from loader |
| 207 | + |
| 208 | + // Cache = {One,Two,Three,Failed,NotPresent} |
| 209 | + // expected hit=3.0, miss=3.0, size=5.0 |
| 210 | + hits = PlatformMetricsRegistry.getMeterRegistry().get("cache.gets").tag("result","hit").meter().measure().iterator().next().getValue(); |
| 211 | + misses = PlatformMetricsRegistry.getMeterRegistry().get("cache.gets").tag("result","miss").meter().measure().iterator().next().getValue(); |
| 212 | + size = PlatformMetricsRegistry.getMeterRegistry().get("cache.size").meter().measure().iterator().next().getValue(); |
| 213 | + |
| 214 | + assertEquals(3.0, hits); |
| 215 | + assertEquals(3.0, misses); |
| 216 | + assertEquals(5.0,size); |
157 | 217 | } |
158 | 218 |
|
159 | 219 | @Test |
|
0 commit comments