Repository navigation
Expand file tree
/
Copy pathBenchmark.java
More file actions
63 lines (51 loc) 路 1.71 KB
/
Copy pathBenchmark.java
File metadata and controls
63 lines (51 loc) 路 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package fastdaemon.benchmark;
import fastdaemon.FastDaemon;
import fastdaemon.FastDaemonClient;
import fastdaemon.FastDaemonServer;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
public class Benchmark {
private static final String APP_ID = "fastdaemon-benchmark";
private static final int PORT = 49153;
private FastDaemonClient client;
private Thread serverThread;
private FastDaemonServer server;
@Setup(Level.Trial)
public void setup() throws Exception {
server = FastDaemon.server(APP_ID)
.port(PORT)
.onCommand((cmd, payload) -> "OK")
.build();
serverThread = new Thread(server::run, "BenchServer");
serverThread.setDaemon(true);
serverThread.start();
Thread.sleep(200);
client = FastDaemon.client(APP_ID).port(PORT).build();
if (!client.ping(1000)) {
throw new IllegalStateException("Failed to connect to benchmark daemon server!");
}
}
@TearDown(Level.Trial)
public void tearDown() {
if (client != null) {
client.shutdown();
}
if (server != null) {
server.shutdown();
}
}
@org.openjdk.jmh.annotations.Benchmark
public boolean benchmarkPing() {
return client.ping(500);
}
@org.openjdk.jmh.annotations.Benchmark
public String benchmarkCommandRoundTrip() {
return client.send("ECHO", "payload_test_data", 500);
}
}