1 /* 2 * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com> and others 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the Eclipse Distribution License v. 1.0 which is available at 6 * https://www.eclipse.org/org/documents/edl-v10.php. 7 * 8 * SPDX-License-Identifier: BSD-3-Clause 9 */ 10 package org.eclipse.jgit.benchmarks; 11 12 import java.security.SecureRandom; 13 import java.util.concurrent.TimeUnit; 14 15 import org.eclipse.jgit.util.SimpleLruCache; 16 import org.openjdk.jmh.annotations.Benchmark; 17 import org.openjdk.jmh.annotations.BenchmarkMode; 18 import org.openjdk.jmh.annotations.Group; 19 import org.openjdk.jmh.annotations.GroupThreads; 20 import org.openjdk.jmh.annotations.Mode; 21 import org.openjdk.jmh.annotations.OutputTimeUnit; 22 import org.openjdk.jmh.annotations.Scope; 23 import org.openjdk.jmh.annotations.Setup; 24 import org.openjdk.jmh.annotations.State; 25 import org.openjdk.jmh.annotations.TearDown; 26 import org.openjdk.jmh.runner.Runner; 27 import org.openjdk.jmh.runner.RunnerException; 28 import org.openjdk.jmh.runner.options.Options; 29 import org.openjdk.jmh.runner.options.OptionsBuilder; 30 31 @State(Scope.Benchmark) 32 public class SimpleLruCacheBenchmark { 33 34 SecureRandom rnd = new SecureRandom(); 35 36 private volatile SimpleLruCache<String, String> cache = new SimpleLruCache<>( 37 100, 0.2f); 38 39 private volatile int i; 40 41 @Setup setupBenchmark()42 public void setupBenchmark() { 43 i = rnd.nextInt(250); 44 } 45 46 @TearDown teardown()47 public void teardown() { 48 cache = null; 49 } 50 51 @Benchmark 52 @Group("readwrite") 53 @GroupThreads(1) 54 @BenchmarkMode({ Mode.AverageTime }) 55 @OutputTimeUnit(TimeUnit.NANOSECONDS) testCacheWrite()56 public SimpleLruCache<String, String> testCacheWrite() { 57 cache.put("k" + i, "v" + i); 58 return cache; 59 } 60 61 @Benchmark 62 @Group("readwrite") 63 @GroupThreads(1) 64 @BenchmarkMode({ Mode.AverageTime }) 65 @OutputTimeUnit(TimeUnit.NANOSECONDS) testCacheRead()66 public SimpleLruCache<String, String> testCacheRead() { 67 cache.get("k" + i); 68 return cache; 69 } 70 main(String[] args)71 public static void main(String[] args) throws RunnerException { 72 Options opt = new OptionsBuilder() 73 .include(SimpleLruCacheBenchmark.class.getSimpleName()) 74 // .addProfiler(StackProfiler.class) 75 // .addProfiler(GCProfiler.class) 76 .forks(1).jvmArgs("-ea").build(); 77 new Runner(opt).run(); 78 } 79 } 80