Created
January 11, 2012 23:54
-
-
Save kimchy/1597555 to your computer and use it in GitHub Desktop.
Very simply test to show the cost of stats in guava cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.common.cache.Cache; | |
| import com.google.common.cache.CacheBuilder; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.ThreadPoolExecutor; | |
| import java.util.concurrent.TimeUnit; | |
| /** | |
| */ | |
| public class Test { | |
| static final long WARMUP = 1000000; | |
| static final long RUN = 50000000; | |
| static final int THREADS = 5; | |
| public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException { | |
| CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); | |
| Method cacheBuilderDisableStatsMethodX = CacheBuilder.class.getDeclaredMethod("disableStats"); | |
| cacheBuilderDisableStatsMethodX.setAccessible(true); | |
| cacheBuilderDisableStatsMethodX.invoke(cacheBuilder); | |
| Cache<String, String> noStatsCache = cacheBuilder.build(); | |
| runTest(noStatsCache, "no stats"); | |
| Cache<String, String> statsCache = CacheBuilder.newBuilder().build(); | |
| runTest(statsCache, "with stats"); | |
| } | |
| private static void runTest(final Cache<String, String> cache, String type) throws InterruptedException { | |
| ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(THREADS); | |
| executorService.prestartAllCoreThreads(); | |
| System.out.println("--- Starting: (" + type + ")"); | |
| init(cache); | |
| System.out.println("Start Warmup"); | |
| execute(cache, WARMUP); | |
| System.out.println("Done Warmup"); | |
| System.out.println("Start Test"); | |
| long start = System.currentTimeMillis(); | |
| for (int i = 0; i < THREADS; i++) { | |
| executorService.submit(new Runnable() { | |
| @Override | |
| public void run() { | |
| execute(cache, RUN); | |
| } | |
| }); | |
| } | |
| executorService.shutdown(); | |
| executorService.awaitTermination(10000, TimeUnit.SECONDS); | |
| long took = System.currentTimeMillis() - start; | |
| System.out.println("--- Done: (" + type + "), took: " + took + "ms"); | |
| } | |
| static void init(Cache<String, String> cache) { | |
| for (int i = 0; i < 100; i++) { | |
| cache.put(Integer.toString(i), Integer.toString(i)); | |
| } | |
| } | |
| static void execute(Cache<String, String> cache, long count) { | |
| for (long i = 0; i < count; i++) { | |
| cache.getIfPresent("1"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment