xref: /JGit/org.eclipse.jgit.benchmarks/src/org/eclipse/jgit/benchmarks/SimpleLruCacheBenchmark.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
19a638b08SMatthias Sohn /*
2*5c5f7c6bSMatthias Sohn  * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com> and others
39a638b08SMatthias Sohn  *
4*5c5f7c6bSMatthias Sohn  * This program and the accompanying materials are made available under the
5*5c5f7c6bSMatthias Sohn  * terms of the Eclipse Distribution License v. 1.0 which is available at
6*5c5f7c6bSMatthias Sohn  * https://www.eclipse.org/org/documents/edl-v10.php.
79a638b08SMatthias Sohn  *
8*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
99a638b08SMatthias Sohn  */
109a638b08SMatthias Sohn package org.eclipse.jgit.benchmarks;
119a638b08SMatthias Sohn 
129a638b08SMatthias Sohn import java.security.SecureRandom;
139a638b08SMatthias Sohn import java.util.concurrent.TimeUnit;
149a638b08SMatthias Sohn 
159a638b08SMatthias Sohn import org.eclipse.jgit.util.SimpleLruCache;
169a638b08SMatthias Sohn import org.openjdk.jmh.annotations.Benchmark;
179a638b08SMatthias Sohn import org.openjdk.jmh.annotations.BenchmarkMode;
189a638b08SMatthias Sohn import org.openjdk.jmh.annotations.Group;
199a638b08SMatthias Sohn import org.openjdk.jmh.annotations.GroupThreads;
209a638b08SMatthias Sohn import org.openjdk.jmh.annotations.Mode;
219a638b08SMatthias Sohn import org.openjdk.jmh.annotations.OutputTimeUnit;
229a638b08SMatthias Sohn import org.openjdk.jmh.annotations.Scope;
239a638b08SMatthias Sohn import org.openjdk.jmh.annotations.Setup;
249a638b08SMatthias Sohn import org.openjdk.jmh.annotations.State;
259a638b08SMatthias Sohn import org.openjdk.jmh.annotations.TearDown;
269a638b08SMatthias Sohn import org.openjdk.jmh.runner.Runner;
279a638b08SMatthias Sohn import org.openjdk.jmh.runner.RunnerException;
289a638b08SMatthias Sohn import org.openjdk.jmh.runner.options.Options;
299a638b08SMatthias Sohn import org.openjdk.jmh.runner.options.OptionsBuilder;
309a638b08SMatthias Sohn 
319a638b08SMatthias Sohn @State(Scope.Benchmark)
329a638b08SMatthias Sohn public class SimpleLruCacheBenchmark {
339a638b08SMatthias Sohn 
349a638b08SMatthias Sohn 	SecureRandom rnd = new SecureRandom();
359a638b08SMatthias Sohn 
369a638b08SMatthias Sohn 	private volatile SimpleLruCache<String, String> cache = new SimpleLruCache<>(
379a638b08SMatthias Sohn 			100, 0.2f);
389a638b08SMatthias Sohn 
399a638b08SMatthias Sohn 	private volatile int i;
409a638b08SMatthias Sohn 
419a638b08SMatthias Sohn 	@Setup
setupBenchmark()429a638b08SMatthias Sohn 	public void setupBenchmark() {
439a638b08SMatthias Sohn 		i = rnd.nextInt(250);
449a638b08SMatthias Sohn 	}
459a638b08SMatthias Sohn 
469a638b08SMatthias Sohn 	@TearDown
teardown()479a638b08SMatthias Sohn 	public void teardown() {
489a638b08SMatthias Sohn 		cache = null;
499a638b08SMatthias Sohn 	}
509a638b08SMatthias Sohn 
519a638b08SMatthias Sohn 	@Benchmark
529a638b08SMatthias Sohn 	@Group("readwrite")
539a638b08SMatthias Sohn 	@GroupThreads(1)
549a638b08SMatthias Sohn 	@BenchmarkMode({ Mode.AverageTime })
559a638b08SMatthias Sohn 	@OutputTimeUnit(TimeUnit.NANOSECONDS)
testCacheWrite()569a638b08SMatthias Sohn 	public SimpleLruCache<String, String> testCacheWrite() {
579a638b08SMatthias Sohn 		cache.put("k" + i, "v" + i);
589a638b08SMatthias Sohn 		return cache;
599a638b08SMatthias Sohn 	}
609a638b08SMatthias Sohn 
619a638b08SMatthias Sohn 	@Benchmark
629a638b08SMatthias Sohn 	@Group("readwrite")
639a638b08SMatthias Sohn 	@GroupThreads(1)
649a638b08SMatthias Sohn 	@BenchmarkMode({ Mode.AverageTime })
659a638b08SMatthias Sohn 	@OutputTimeUnit(TimeUnit.NANOSECONDS)
testCacheRead()669a638b08SMatthias Sohn 	public SimpleLruCache<String, String> testCacheRead() {
679a638b08SMatthias Sohn 		cache.get("k" + i);
689a638b08SMatthias Sohn 		return cache;
699a638b08SMatthias Sohn 	}
709a638b08SMatthias Sohn 
main(String[] args)719a638b08SMatthias Sohn 	public static void main(String[] args) throws RunnerException {
729a638b08SMatthias Sohn 		Options opt = new OptionsBuilder()
739a638b08SMatthias Sohn 				.include(SimpleLruCacheBenchmark.class.getSimpleName())
749a638b08SMatthias Sohn 				// .addProfiler(StackProfiler.class)
759a638b08SMatthias Sohn 				// .addProfiler(GCProfiler.class)
769a638b08SMatthias Sohn 				.forks(1).jvmArgs("-ea").build();
779a638b08SMatthias Sohn 		new Runner(opt).run();
789a638b08SMatthias Sohn 	}
799a638b08SMatthias Sohn }
80