From 7a9bc0d2635327a008a0c94b4e61b35ae6a12352 Mon Sep 17 00:00:00 2001 From: "vladimir.bukhtoyarov" Date: Tue, 8 Nov 2016 00:19:31 +0300 Subject: [PATCH] benchmarks for "top" functionality --- .../metricscore/hdr/top/TopBenchmark.java | 218 ++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 src/main/benchmark/com/github/metricscore/hdr/top/TopBenchmark.java diff --git a/src/main/benchmark/com/github/metricscore/hdr/top/TopBenchmark.java b/src/main/benchmark/com/github/metricscore/hdr/top/TopBenchmark.java new file mode 100644 index 0000000..5bf10bf --- /dev/null +++ b/src/main/benchmark/com/github/metricscore/hdr/top/TopBenchmark.java @@ -0,0 +1,218 @@ +/* + * + * Copyright 2016 Vladimir Bukhtoyarov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.metricscore.hdr.top; + + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.time.Duration; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode({Mode.Throughput, Mode.AverageTime}) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +public class TopBenchmark { + + @State(Scope.Benchmark) + public static class TopState { + + final Top chunkedTop_1 = Top.builder(1) + .resetAllPositionsPeriodicallyByChunks(Duration.ofSeconds(4), 4) + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top chunkedTop_10 = Top.builder(10) + .resetAllPositionsPeriodicallyByChunks(Duration.ofSeconds(4), 4) + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top periodicallyTop_1 = Top.builder(1) + .resetAllPositionsPeriodically(Duration.ofSeconds(1)) + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top periodicallyTop_10 = Top.builder(10) + .resetAllPositionsPeriodically(Duration.ofSeconds(1)) + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top resetOnSnapshotTop_1 = Top.builder(1) + .resetAllPositionsOnSnapshot() + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top resetOnSnapshotTop_10 = Top.builder(10) + .resetAllPositionsOnSnapshot() + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top uniformTop_1 = Top.builder(1) + .neverResetPositions() + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + + final Top uniformTop_10 = Top.builder(10) + .neverResetPositions() + .withSnapshotCachingDuration(Duration.ZERO) + .build(); + } + + @Benchmark + public long baseLine() { + return getRandomValue(); + } + + @Group("chunkedTop_1") + @GroupThreads(3) + @Benchmark + public void update_chunkedTop_1(TopState state) { + state.chunkedTop_1.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("chunkedTop_1") + @GroupThreads(1) + @Benchmark + public List getSnapshot_chunkedTop_1(TopState state) { + return state.chunkedTop_1.getPositionsInDescendingOrder(); + } + + @Group("chunkedTop_10") + @GroupThreads(3) + @Benchmark + public void update_chunkedTop_10(TopState state) { + state.chunkedTop_10.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("chunkedTop_10") + @GroupThreads(1) + @Benchmark + public List getSnapshot_chunkedTop_10(TopState state) { + return state.chunkedTop_10.getPositionsInDescendingOrder(); + } + + @Group("periodicallyTop_1") + @GroupThreads(3) + @Benchmark + public void update_periodicallyTop_1(TopState state) { + state.periodicallyTop_1.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("periodicallyTop_1") + @GroupThreads(1) + @Benchmark + public List getSnapshot_periodicallyTop_1(TopState state) { + return state.periodicallyTop_1.getPositionsInDescendingOrder(); + } + + @Group("periodicallyTop_10") + @GroupThreads(3) + @Benchmark + public void update_periodicallyTop_10(TopState state) { + state.periodicallyTop_10.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("periodicallyTop_10") + @GroupThreads(1) + @Benchmark + public List getSnapshot_periodicallyTop_10(TopState state) { + return state.periodicallyTop_10.getPositionsInDescendingOrder(); + } + + @Group("resetOnSnapshotTop_1") + @GroupThreads(3) + @Benchmark + public void update_resetOnSnapshotTop_1(TopState state) { + state.resetOnSnapshotTop_1.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("resetOnSnapshotTop_1") + @GroupThreads(1) + @Benchmark + public List getSnapshot_resetOnSnapshotTop_1(TopState state) { + return state.resetOnSnapshotTop_1.getPositionsInDescendingOrder(); + } + + @Group("resetOnSnapshotTop_10") + @GroupThreads(3) + @Benchmark + public void update_resetOnSnapshotTop_10(TopState state) { + state.resetOnSnapshotTop_10.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("resetOnSnapshotTop_10") + @GroupThreads(1) + @Benchmark + public List getSnapshot_resetOnSnapshotTop_10(TopState state) { + return state.resetOnSnapshotTop_10.getPositionsInDescendingOrder(); + } + + @Group("uniformTop_1") + @GroupThreads(3) + @Benchmark + public void update_uniformTop_1(TopState state) { + state.uniformTop_1.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("uniformTop_1") + @GroupThreads(1) + @Benchmark + public List getSnapshot_uniformTop_1(TopState state) { + return state.uniformTop_1.getPositionsInDescendingOrder(); + } + + @Group("uniformTop_10") + @GroupThreads(3) + @Benchmark + public void update_uniformTop_10(TopState state) { + state.uniformTop_10.update(0, getRandomValue(), TimeUnit.NANOSECONDS, () -> "Some query to something"); + } + + @Group("uniformTop_10") + @GroupThreads(1) + @Benchmark + public List getSnapshot_uniformTop_10(TopState state) { + return state.uniformTop_10.getPositionsInDescendingOrder(); + } + + private static long getRandomValue() { + return ThreadLocalRandom.current().nextLong(15_000_000) + 5_000_000; + } + + public static class FourThread { + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(((Class) TopBenchmark.class).getSimpleName()) + .warmupIterations(5) + .measurementIterations(5) + .threads(4) + .forks(1) + .build(); + try { + new Runner(opt).run(); + } catch (RunnerException e) { + throw new RuntimeException(e); + } + } + } + +}