From 7c65badf4653bb3825d3e524393dfcb6e5f6841e Mon Sep 17 00:00:00 2001 From: Mats Date: Sun, 27 Aug 2023 14:34:10 +0200 Subject: [PATCH] Add benchmarks --- benchmarks/pom.xml | 99 +++++++++++++++++++ benchmarks/run.sh | 2 + .../com/meteormsg/benchmarks/MeteorBench.java | 25 +++++ .../meteormsg/benchmarks/ScoresWithMaps.java | 67 +++++++++++++ .../meteormsg/benchmarks/SimpleIncrement.java | 65 ++++++++++++ pom.xml | 1 + 6 files changed, 259 insertions(+) create mode 100644 benchmarks/pom.xml create mode 100644 benchmarks/run.sh create mode 100644 benchmarks/src/main/java/com/meteormsg/benchmarks/MeteorBench.java create mode 100644 benchmarks/src/main/java/com/meteormsg/benchmarks/ScoresWithMaps.java create mode 100644 benchmarks/src/main/java/com/meteormsg/benchmarks/SimpleIncrement.java diff --git a/benchmarks/pom.xml b/benchmarks/pom.xml new file mode 100644 index 0000000..84fcfe6 --- /dev/null +++ b/benchmarks/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.meteormsg + meteor-parent + ${revision} + + + benchmarks + + + 17 + 17 + UTF-8 + + 1.37 + + + + + com.meteormsg + meteor-core + ${project.parent.version} + + + com.meteormsg + meteor-common + ${project.parent.version} + + + com.meteormsg.transport + meteor-jedis + ${project.parent.version} + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + test + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 17 + 17 + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.0 + + + package + + shade + + + benchmarks + + + org.openjdk.jmh.Main + + + + + + + + + + \ No newline at end of file diff --git a/benchmarks/run.sh b/benchmarks/run.sh new file mode 100644 index 0000000..8e8c2a2 --- /dev/null +++ b/benchmarks/run.sh @@ -0,0 +1,2 @@ +cd target/ +java -jar benchmarks.jar -rf json \ No newline at end of file diff --git a/benchmarks/src/main/java/com/meteormsg/benchmarks/MeteorBench.java b/benchmarks/src/main/java/com/meteormsg/benchmarks/MeteorBench.java new file mode 100644 index 0000000..7727fd0 --- /dev/null +++ b/benchmarks/src/main/java/com/meteormsg/benchmarks/MeteorBench.java @@ -0,0 +1,25 @@ +package com.meteormsg.benchmarks; + +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.util.concurrent.TimeUnit; + +@BenchmarkMode({Mode.All}) +@OutputTimeUnit(TimeUnit.SECONDS) +@State(Scope.Thread) +public class MeteorBench { + + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder() + .include(SimpleIncrement.class.getSimpleName()) + .include(ScoresWithMaps.class.getSimpleName()) + .forks(1) + .build(); + + new Runner(options).run(); + } +} diff --git a/benchmarks/src/main/java/com/meteormsg/benchmarks/ScoresWithMaps.java b/benchmarks/src/main/java/com/meteormsg/benchmarks/ScoresWithMaps.java new file mode 100644 index 0000000..0b611d6 --- /dev/null +++ b/benchmarks/src/main/java/com/meteormsg/benchmarks/ScoresWithMaps.java @@ -0,0 +1,67 @@ +package com.meteormsg.benchmarks; + +import com.meteormsg.base.RpcOptions; +import com.meteormsg.base.defaults.LoopbackTransport; +import com.meteormsg.core.Meteor; +import org.openjdk.jmh.annotations.*; + +import java.io.IOException; +import java.util.HashMap; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode({Mode.All}) +@OutputTimeUnit(TimeUnit.SECONDS) +@State(Scope.Thread) +public class ScoresWithMaps { + + private Meteor meteor; + private PersonalScores scoreboardStub; + + @Param({"1", "10", "50"}) // Example parameter values + private int workerThreads; + + @Setup + public void setup() { + System.out.println("workerThreads: " + workerThreads); + RpcOptions rpcOptions = new RpcOptions(); + rpcOptions.setExecutorThreads(workerThreads); + meteor = new Meteor(new LoopbackTransport(), rpcOptions); + meteor.registerImplementation(new ScoreboardImplementation(), "parkour-leaderboard"); + scoreboardStub = meteor.registerProcedure(PersonalScores.class, "parkour-leaderboard"); + } + + @TearDown + public void tearDown() throws IOException { + meteor.stop(); + } + + @Benchmark + public void benchmarkMethod() { + scoreboardStub.incrementFor("player1"); + } + + public interface PersonalScores { + HashMap getScores(); + HashMap incrementFor(String name); + } + + public static class ScoreboardImplementation implements PersonalScores { + private HashMap scores = new HashMap<>(); + + @Override + public HashMap getScores() { + return scores; + } + + @Override + public HashMap incrementFor(String name) { + if (scores.containsKey(name)) { + scores.put(name, scores.get(name) + 1); + } else { + scores.put(name, 1); + } + return scores; + } + } + +} diff --git a/benchmarks/src/main/java/com/meteormsg/benchmarks/SimpleIncrement.java b/benchmarks/src/main/java/com/meteormsg/benchmarks/SimpleIncrement.java new file mode 100644 index 0000000..74932b6 --- /dev/null +++ b/benchmarks/src/main/java/com/meteormsg/benchmarks/SimpleIncrement.java @@ -0,0 +1,65 @@ +package com.meteormsg.benchmarks; + +import com.meteormsg.base.RpcOptions; +import com.meteormsg.base.defaults.LoopbackTransport; +import com.meteormsg.core.Meteor; +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.io.IOException; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode({Mode.All}) +@OutputTimeUnit(TimeUnit.SECONDS) +@State(Scope.Thread) +public class SimpleIncrement { + + private Meteor meteor; + private Scoreboard scoreboardStub; + + @Param({"1", "10", "50"}) // Example parameter values + private int workerThreads; + + @Setup + public void setup() { + System.out.println("workerThreads: " + workerThreads); + RpcOptions rpcOptions = new RpcOptions(); + rpcOptions.setExecutorThreads(workerThreads); + meteor = new Meteor(new LoopbackTransport(), rpcOptions); + meteor.registerImplementation(new ScoreboardImplementation(), "parkour-leaderboard"); + scoreboardStub = meteor.registerProcedure(Scoreboard.class, "parkour-leaderboard"); + } + + @TearDown + public void tearDown() throws IOException { + meteor.stop(); + } + + @Benchmark + public void benchmarkMethod() { + scoreboardStub.incrementScore(); + } + + public interface Scoreboard { + int incrementScore(); + int getScore(); + } + + public static class ScoreboardImplementation implements Scoreboard { + private Integer score = 0; + + @Override + public int incrementScore() { + return ++score; + } + + @Override + public int getScore() { + return score; + } + } + +} diff --git a/pom.xml b/pom.xml index c8f8e47..7dc1af9 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ meteor-core examples meteor-jedis + benchmarks