Repository: kafka Updated Branches: refs/heads/trunk a3f068e22 -> 346d0ca53
MINOR: Fix needless GC + Result time unit in JMH Fixes two issues with the JMH benchmark example: * Trivial: The output should be in `ops/ms` for readability reasons (it's in the millions of operations per second) * Important: The benchmark is not actually measuring the LRU Cache performance as most of the time in each run is wasted on concatenating `key + counter` as well as `value + counter`. Fixed by pre-generating 10k K-V pairs (100x the cache capacity) and iterating over them. This brings the performance up by a factor of more than 5 on a standard 4 core i7 (`~6k/ms` before goes to `~35k/ms`). Author: Armin Braun <[email protected]> Reviewers: Bill Bejeck <[email protected]>, Guozhang Wang <[email protected]>, Ismael Juma <[email protected]> Closes #2903 from original-brownbear/fix-jmh-example Project: http://git-wip-us.apache.org/repos/asf/kafka/repo Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/346d0ca5 Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/346d0ca5 Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/346d0ca5 Branch: refs/heads/trunk Commit: 346d0ca5382800f4bbc670b80660408705742392 Parents: a3f068e Author: Armin Braun <[email protected]> Authored: Mon Sep 18 10:38:26 2017 +0100 Committer: Ismael Juma <[email protected]> Committed: Mon Sep 18 10:52:54 2017 +0100 ---------------------------------------------------------------------- .../kafka/jmh/cache/LRUCacheBenchmark.java | 28 +++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kafka/blob/346d0ca5/jmh-benchmarks/src/main/java/org/apache/kafka/jmh/cache/LRUCacheBenchmark.java ---------------------------------------------------------------------- diff --git a/jmh-benchmarks/src/main/java/org/apache/kafka/jmh/cache/LRUCacheBenchmark.java b/jmh-benchmarks/src/main/java/org/apache/kafka/jmh/cache/LRUCacheBenchmark.java index ecf73f9..7d65979 100644 --- a/jmh-benchmarks/src/main/java/org/apache/kafka/jmh/cache/LRUCacheBenchmark.java +++ b/jmh-benchmarks/src/main/java/org/apache/kafka/jmh/cache/LRUCacheBenchmark.java @@ -17,9 +17,11 @@ package org.apache.kafka.jmh.cache; +import java.util.concurrent.TimeUnit; import org.apache.kafka.common.cache.LRUCache; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; @@ -35,25 +37,39 @@ import org.openjdk.jmh.runner.options.OptionsBuilder; * http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/ */ @State(Scope.Thread) +@OutputTimeUnit(TimeUnit.MILLISECONDS) public class LRUCacheBenchmark { + private static final int DISTINCT_KEYS = 10_000; + + private static final String KEY = "the_key_to_use"; + + private static final String VALUE = "the quick brown fox jumped over the lazy dog the olympics are about to start"; + + private final String[] keys = new String[DISTINCT_KEYS]; + + private final String[] values = new String[DISTINCT_KEYS]; + private LRUCache<String, String> lruCache; - private final String key = "the_key_to_use"; - private final String value = "the quick brown fox jumped over the lazy dog the olympics are about to start"; int counter; - @Setup(Level.Trial) - public void setUpCaches() { + public void setUp() { + for (int i = 0; i < DISTINCT_KEYS; ++i) { + keys[i] = KEY + i; + values[i] = VALUE + i; + } lruCache = new LRUCache<>(100); } @Benchmark public String testCachePerformance() { counter++; - lruCache.put(key + counter, value + counter); - return lruCache.get(key + counter); + int index = counter % DISTINCT_KEYS; + String hashkey = keys[index]; + lruCache.put(hashkey, values[index]); + return lruCache.get(hashkey); } public static void main(String[] args) throws RunnerException {
