Skip to content

Commit 3b381d4

Browse files
author
Bytekeeper
committed
Added micro benchmarking using JMH
1 parent 0d4ac23 commit 3b381d4

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<jmh.version>1.21</jmh.version>
1314
</properties>
1415

1516
<build>
@@ -45,6 +46,30 @@
4546
</execution>
4647
</executions>
4748
</plugin>
49+
<plugin>
50+
<groupId>org.codehaus.mojo</groupId>
51+
<artifactId>exec-maven-plugin</artifactId>
52+
<version>1.6.0</version>
53+
<executions>
54+
<execution>
55+
<id>run-benchmarks</id>
56+
<phase>integration-test</phase>
57+
<goals>
58+
<goal>exec</goal>
59+
</goals>
60+
<configuration>
61+
<classpathScope>test</classpathScope>
62+
<executable>java</executable>
63+
<arguments>
64+
<argument>-classpath</argument>
65+
<classpath/>
66+
<argument>org.openjdk.jmh.Main</argument>
67+
<argument>.*</argument>
68+
</arguments>
69+
</configuration>
70+
</execution>
71+
</executions>
72+
</plugin>
4873
</plugins>
4974
</build>
5075

@@ -86,5 +111,20 @@
86111
<version>3.12.2</version>
87112
<scope>test</scope>
88113
</dependency>
114+
115+
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh -->
116+
<dependency>
117+
<groupId>org.openjdk.jmh</groupId>
118+
<artifactId>jmh-core</artifactId>
119+
<version>${jmh.version}</version>
120+
<scope>test</scope>
121+
</dependency>
122+
123+
<dependency>
124+
<groupId>org.openjdk.jmh</groupId>
125+
<artifactId>jmh-generator-annprocess</artifactId>
126+
<version>${jmh.version}</version>
127+
<scope>test</scope>
128+
</dependency>
89129
</dependencies>
90130
</project>

src/main/java/bwapi/Client.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Client {
4545
+ 4 // LastKeepAliveTime
4646
;
4747
private static final List<Integer> SUPPORTED_BWAPI_VERSIONS = Arrays.asList(10003);
48-
private static final int MAX_COUNT = 19999;
48+
static final int MAX_COUNT = 19999;
4949

5050
private static final int maxNumGames = 8;
5151
private static final int gameTableSize = GAME_SIZE * maxNumGames;
@@ -71,6 +71,13 @@ class Client {
7171
throw new Exception("All servers busy!");
7272
}
7373

74+
/**
75+
* For test purposes only
76+
*/
77+
Client(ByteBuffer buffer) {
78+
data = new ClientData(buffer).new GameData(0);
79+
}
80+
7481
public GameData data() {
7582
return data;
7683
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package bwapi;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
import org.openjdk.jmh.infra.Blackhole;
5+
6+
import java.nio.ByteBuffer;
7+
import java.util.SplittableRandom;
8+
import java.util.stream.Collectors;
9+
10+
@Warmup(iterations = 3, time = 5)
11+
@Measurement(iterations = 3, time = 5)
12+
@Fork(3)
13+
public class ClientDataBenchmark {
14+
@State(Scope.Thread)
15+
public static class EmptyState {
16+
Client client;
17+
Game game;
18+
String[] strings;
19+
20+
@Setup(Level.Invocation)
21+
public void setup() {
22+
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
23+
game = new Game(client);
24+
strings = buildStrings();
25+
}
26+
27+
}
28+
29+
@State(Scope.Thread)
30+
public static class FilledWithStrings {
31+
Client client;
32+
ClientData.GameData data;
33+
Game game;
34+
35+
@Setup(Level.Invocation)
36+
public void setup() {
37+
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
38+
data = client.data();
39+
game = new Game(client);
40+
String[] strings = buildStrings();
41+
for (String s : strings) {
42+
client.addString(s);
43+
}
44+
}
45+
}
46+
47+
private static String[] buildStrings() {
48+
SplittableRandom rnd = new SplittableRandom(987654321L);
49+
String[] strings = new String[Client.MAX_COUNT];
50+
for (int i = 0; i < strings.length; i++) {
51+
strings[i] = rnd.ints(1022, 0, 9)
52+
.mapToObj(Integer::toString)
53+
.collect(Collectors.joining());
54+
}
55+
return strings;
56+
}
57+
58+
@Benchmark
59+
@OperationsPerInvocation(Client.MAX_COUNT)
60+
public int addUnitCommand(EmptyState s) {
61+
for (int i = 0; i < Client.MAX_COUNT; i++) {
62+
s.game.addUnitCommand(0, 1, 2, 3, 4, 5);
63+
}
64+
return s.client.data().getCommandCount();
65+
}
66+
67+
@Benchmark
68+
@OperationsPerInvocation(Client.MAX_COUNT)
69+
public int addString(EmptyState s) {
70+
for (int i = 0; i < Client.MAX_COUNT; i++) {
71+
s.client.addString(s.strings[i]);
72+
}
73+
return s.client.data().getStringCount();
74+
}
75+
76+
@Benchmark
77+
@OperationsPerInvocation(Client.MAX_COUNT)
78+
public void getString(FilledWithStrings s, Blackhole blackhole) {
79+
for (int i = 0; i < Client.MAX_COUNT; i++) {
80+
blackhole.consume(s.data.getStrings(i));
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)