Skip to content

Instantly share code, notes, and snippets.

@jiacai2050
Created September 6, 2017 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiacai2050/e3f9451b3fa289fb2d6a4db370a7d6a4 to your computer and use it in GitHub Desktop.
Save jiacai2050/e3f9451b3fa289fb2d6a4db370a7d6a4 to your computer and use it in GitHub Desktop.
guava caffeine 性能测试
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yahoo.ycsb.generator.NumberGenerator;
import com.yahoo.ycsb.generator.ScrambledZipfianGenerator;
import junit.framework.TestCase;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.text.NumberFormat;
import java.util.concurrent.CountDownLatch;
public class CacheTest extends TestCase {
static final int numElements = 10_000_000;
static final int zipfItemCount = numElements / 100;
static int maxSize = zipfItemCount;
static int initCapacity = 50_000;
static int threadCount = 2 << 4;
static int loopCount = 5;
Integer[] ints;
interface MockCache<K, V> {
V get(K k);
}
class GuavaCache implements MockCache<Integer, Integer> {
Cache<Integer, Integer> c = CacheBuilder.newBuilder()
.initialCapacity(initCapacity)
.maximumSize(maxSize)
.recordStats()
.build();
@Override
public Integer get(Integer s) {
Integer v = c.getIfPresent(s);
if (v == null) {
c.put(s, s);
}
return v;
}
public void dump() {
System.out.println(c.stats());
}
}
class CaffeineCache implements MockCache<Integer, Integer> {
com.github.benmanes.caffeine.cache.Cache<Integer, Integer> c = Caffeine.newBuilder()
.maximumSize(maxSize)
.initialCapacity(initCapacity)
.recordStats()
.build();
@Override
public Integer get(Integer s) {
Integer v = c.getIfPresent(s);
if (v == null) {
c.put(s, s);
}
return v;
}
public void dump() {
System.out.println(c);
System.out.println(c.stats());
}
}
protected void setUp() throws Exception {
ints = new Integer[numElements];
NumberGenerator generator = new ScrambledZipfianGenerator(zipfItemCount);
for (int i = 0; i < numElements; i++) {
ints[i] = generator.nextValue().intValue();
}
}
public void test_guava() throws Exception {
GuavaCache c = new GuavaCache();
for (int i = 0; i < loopCount; ++i) {
p0(c, "guava", threadCount);
}
c.dump();
}
public void test_caffeine() throws Exception {
CaffeineCache c = new CaffeineCache();
for (int i = 0; i < loopCount; ++i) {
p0(c, "caffeine", threadCount);
}
c.dump();
}
private void p0(MockCache mockCache, String name, int threadCount) throws Exception {
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(threadCount);
final CountDownLatch dumpLatch = new CountDownLatch(1);
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; ++i) {
int finalI = i;
Thread thread = new Thread() {
public void run() {
try {
startLatch.await();
for (int j = 0; j < ints.length; j++) {
mockCache.get(ints[j]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
endLatch.countDown();
try {
dumpLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
threads[i] = thread;
thread.start();
}
long startMillis = System.currentTimeMillis();
long startYGC = TestUtil.getYoungGC();
long startFullGC = TestUtil.getFullGC();
startLatch.countDown();
endLatch.await();
long[] threadIdArray = new long[threads.length];
for (int i = 0; i < threads.length; ++i) {
threadIdArray[i] = threads[i].getId();
}
ThreadInfo[] threadInfoArray = ManagementFactory.getThreadMXBean().getThreadInfo(threadIdArray);
dumpLatch.countDown();
long blockedCount = 0;
long waitedCount = 0;
for (int i = 0; i < threadInfoArray.length; ++i) {
ThreadInfo threadInfo = threadInfoArray[i];
blockedCount += threadInfo.getBlockedCount();
waitedCount += threadInfo.getWaitedCount();
}
long millis = System.currentTimeMillis() - startMillis;
long ygc = TestUtil.getYoungGC() - startYGC;
long fullGC = TestUtil.getFullGC() - startFullGC;
System.out.println("thread " + threadCount + " " + name + " millis : "
+ NumberFormat.getInstance().format(millis) + "; YGC " + ygc + " FGC " + fullGC
+ " blocked "
+ NumberFormat.getInstance().format(blockedCount) //
+ " waited " + NumberFormat.getInstance().format(waitedCount));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment