Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.cassandra.cache;
- import java.util.Iterator;
- import java.util.concurrent.ExecutionException;
- import com.google.common.cache.Cache;
- import com.google.common.cache.CacheBuilder;
- import com.google.common.collect.ImmutableList;
- import com.google.common.util.concurrent.MoreExecutors;
- import org.junit.Test;
- import com.github.benmanes.caffeine.cache.Caffeine;
- public class CacheHashClashTest
- {
- private static final long ITERS = 200_000;
- static final Long LONG_1 = Long.valueOf(1);
- interface ICache
- {
- public int size();
- public void put(Long key, Long value);
- public Long get(Long key);
- public void clear();
- public Iterator<Long> keyIterator();
- public long reqCount();
- public double hitRate();
- }
- class GuavaCache implements ICache
- {
- final Cache<Long, Long> cache;
- public GuavaCache(long size)
- {
- cache = CacheBuilder.newBuilder()
- .maximumSize(size)
- .recordStats()
- .build();
- }
- @Override
- public int size()
- {
- return (int) cache.size();
- }
- @Override
- public void put(Long key, Long value)
- {
- cache.put(key, value);
- }
- @Override
- public Long get(Long key)
- {
- return cache.getIfPresent(key);
- }
- @Override
- public void clear()
- {
- cache.invalidateAll();
- }
- @Override
- public Iterator<Long> keyIterator()
- {
- return cache.asMap().keySet().iterator();
- }
- public long reqCount()
- {
- return cache.stats().requestCount();
- }
- public double hitRate()
- {
- return cache.stats().hitRate();
- }
- }
- class CaffeineCache implements ICache
- {
- final com.github.benmanes.caffeine.cache.Cache<Long, Long> cache;
- public CaffeineCache(long size)
- {
- cache = Caffeine.newBuilder()
- .maximumSize(size)
- .executor(MoreExecutors.directExecutor())
- .recordStats()
- .build();
- }
- @Override
- public int size()
- {
- return (int) cache.asMap().size();
- }
- @Override
- public void put(Long key, Long value)
- {
- cache.put(key, value);
- }
- @Override
- public Long get(Long key)
- {
- return cache.getIfPresent(key);
- }
- @Override
- public void clear()
- {
- cache.invalidateAll();
- }
- @Override
- public Iterator<Long> keyIterator()
- {
- return cache.policy().eviction().get().hottest(10000000).keySet().iterator();
- }
- public long reqCount()
- {
- return cache.stats().requestCount();
- }
- public double hitRate()
- {
- return cache.stats().hitRate();
- }
- }
- ICache cache;
- @Test
- public void testGuava() throws InterruptedException, ExecutionException
- {
- testCache(new GuavaCache(150));
- }
- @Test
- public void testCaffeine() throws InterruptedException, ExecutionException
- {
- testCache(new CaffeineCache(150));
- }
- public void testCache(ICache cacheToTest) throws InterruptedException, ExecutionException
- {
- cache = cacheToTest;
- long startTime = System.currentTimeMillis();
- int STEP = 5;
- long i = 200000;
- for (long j = 0; j < 300; ++j)
- {
- get(1L);
- get(j);
- }
- System.out.format("%s size %,d requests %,d hit ratio %f\n",
- cache.toString(),
- cache.size(),
- cache.reqCount(),
- cache.hitRate());
- System.out.println(ImmutableList.copyOf(cache.keyIterator()));
- // add a hashcode clash for 1
- {
- Long CLASH = (i << 32) ^ i ^ 1;
- assert CLASH.hashCode() == LONG_1.hashCode();
- get(CLASH);
- System.out.println(ImmutableList.copyOf(cache.keyIterator()));
- }
- // repeat some entries to let CLASH flow to the probation head
- for (long j = 0; j < 300; ++j)
- {
- get(1L);
- get(j);
- }
- System.out.println(ImmutableList.copyOf(cache.keyIterator()));
- // Now run a repeating sequence which has a longer length than eden space size.
- for (i = 0; i < ITERS; i += STEP)
- {
- get(1L);
- for (long j = 0; j < STEP; ++j)
- get(-j);
- }
- System.out.println(ImmutableList.copyOf(cache.keyIterator()));
- long endTime = System.currentTimeMillis();
- System.out.println();
- System.out.format("%s size %,d requests %,d hit ratio %f\n",
- cache.toString(),
- cache.size(),
- cache.reqCount(),
- cache.hitRate());
- System.out.format("%,d lookups completed in %,d ms\n",
- ITERS, endTime - startTime);
- cache.clear();
- }
- public Long get(Long key)
- {
- Long d = cache.get(key);
- if (d == null)
- {
- d = load(key);
- cache.put(key, d);
- }
- return d;
- }
- public Long load(Long key)
- {
- return key;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment