Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Aug 27, 2023
1 parent 0bd81b9 commit 7c65bad
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 0 deletions.
99 changes: 99 additions & 0 deletions benchmarks/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.meteormsg</groupId>
<artifactId>meteor-parent</artifactId>
<version>${revision}</version>
</parent>

<artifactId>benchmarks</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<jmh.version>1.37</jmh.version>
</properties>

<dependencies>
<dependency>
<groupId>com.meteormsg</groupId>
<artifactId>meteor-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.meteormsg</groupId>
<artifactId>meteor-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.meteormsg.transport</groupId>
<artifactId>meteor-jedis</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-generator-annprocess -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>benchmarks</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions benchmarks/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd target/
java -jar benchmarks.jar -rf json
25 changes: 25 additions & 0 deletions benchmarks/src/main/java/com/meteormsg/benchmarks/MeteorBench.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Integer> getScores();
HashMap<String, Integer> incrementFor(String name);
}

public static class ScoreboardImplementation implements PersonalScores {
private HashMap<String, Integer> scores = new HashMap<>();

@Override
public HashMap<String, Integer> getScores() {
return scores;
}

@Override
public HashMap<String, Integer> incrementFor(String name) {
if (scores.containsKey(name)) {
scores.put(name, scores.get(name) + 1);
} else {
scores.put(name, 1);
}
return scores;
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<module>meteor-core</module>
<module>examples</module>
<module>meteor-jedis</module>
<module>benchmarks</module>
</modules>

<properties>
Expand Down

0 comments on commit 7c65bad

Please sign in to comment.