|
| 1 | +package org.dcache.nfs.benchmarks; |
| 2 | + |
| 3 | +import org.dcache.nfs.util.Cache; |
| 4 | +import org.dcache.nfs.util.NopCacheEventListener; |
| 5 | +import org.openjdk.jmh.annotations.Benchmark; |
| 6 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 7 | +import org.openjdk.jmh.annotations.Mode; |
| 8 | +import org.openjdk.jmh.annotations.Scope; |
| 9 | +import org.openjdk.jmh.annotations.Setup; |
| 10 | +import org.openjdk.jmh.annotations.State; |
| 11 | +import org.openjdk.jmh.annotations.Threads; |
| 12 | +import org.openjdk.jmh.annotations.Warmup; |
| 13 | +import org.openjdk.jmh.results.format.ResultFormatType; |
| 14 | +import org.openjdk.jmh.runner.Runner; |
| 15 | +import org.openjdk.jmh.runner.RunnerException; |
| 16 | +import org.openjdk.jmh.runner.options.Options; |
| 17 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 18 | + |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +@BenchmarkMode(Mode.Throughput) |
| 22 | +public class CacheBenchmark { |
| 23 | + |
| 24 | + @State(Scope.Benchmark) |
| 25 | + public static class CacheHolder { |
| 26 | + |
| 27 | + private Cache<String, String> cache; |
| 28 | + |
| 29 | + @Setup |
| 30 | + public void setUp() { |
| 31 | + cache = new Cache<>("test cache", 64, Integer.MAX_VALUE, |
| 32 | + Integer.MAX_VALUE, |
| 33 | + new NopCacheEventListener()); |
| 34 | + cache.put("foo", "bar"); |
| 35 | + } |
| 36 | + |
| 37 | + public Cache<String, String> getCache() { |
| 38 | + return cache; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Benchmark |
| 43 | + @Threads(16) |
| 44 | + @Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS) |
| 45 | + public long cachePutRemoveBenchmark(CacheHolder cacheHolder) { |
| 46 | + |
| 47 | + final var cache = cacheHolder.getCache(); |
| 48 | + var key = "key"; |
| 49 | + var val = "val"; |
| 50 | + |
| 51 | + cache.put(key, val); |
| 52 | + cache.remove(key); |
| 53 | + |
| 54 | + return cache.lastClean(); |
| 55 | + } |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + @Threads(16) |
| 59 | + @Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS) |
| 60 | + public String cacheGetBenchmark(CacheHolder cacheHolder) { |
| 61 | + |
| 62 | + final var cache = cacheHolder.getCache(); |
| 63 | + return cache.get("foo"); |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) throws RunnerException { |
| 67 | + Options opt = new OptionsBuilder() |
| 68 | + .include(CacheBenchmark.class.getSimpleName()) |
| 69 | + .resultFormat(ResultFormatType.JSON) |
| 70 | + .build(); |
| 71 | + |
| 72 | + new Runner(opt).run(); |
| 73 | + } |
| 74 | +} |
0 commit comments