Skip to content

Commit 1e55f46

Browse files
authored
SyncBenchmark (#462)
1 parent 0d10741 commit 1e55f46

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

src/test/java/perf/Benchmark.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package perf;
2+
3+
import java.util.Date;
4+
import java.util.concurrent.CountDownLatch;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
public abstract class Benchmark {
10+
11+
private static final int SYNC_THREADS = 128;
12+
private final CountDownLatch completed = new CountDownLatch(1);
13+
private volatile Long startTime = null;
14+
private volatile Long endTime = null;
15+
private volatile int targetCount = Integer.MAX_VALUE;
16+
private final AtomicInteger counter = new AtomicInteger();
17+
private final ExecutorService es = Executors.newFixedThreadPool(SYNC_THREADS);
18+
private final int warmupDurationSeconds;
19+
private final int numberOfRequests;
20+
21+
public Benchmark(int warmupDurationSeconds, int numberOfRequests) {
22+
this.warmupDurationSeconds = warmupDurationSeconds;
23+
this.numberOfRequests = numberOfRequests;
24+
}
25+
26+
public void run() {
27+
// warmup
28+
startBenchmark();
29+
30+
// start monitor / warmup
31+
startMonitor();
32+
33+
// start benchmark
34+
startMeasuring();
35+
}
36+
37+
private void startMonitor() {
38+
for (int i = 0; i < warmupDurationSeconds; i++) {
39+
counter.set(0);
40+
long start = new Date().getTime();
41+
try {
42+
Thread.sleep(1_000);
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
}
46+
47+
long current = new Date().getTime();
48+
long elapsed = current - start;
49+
double reqsPerSec = 1_000.0 * counter.get() / elapsed;
50+
System.out.println("reqs/s: \t" + reqsPerSec);
51+
}
52+
}
53+
54+
private void startBenchmark() {
55+
start();
56+
new Thread(() -> {
57+
try {
58+
completed.await();
59+
// wait graceful shutdown
60+
Thread.sleep(1_000);
61+
} catch (InterruptedException e) {
62+
e.printStackTrace();
63+
}
64+
// force shutdown
65+
es.shutdown();
66+
shutdown();
67+
}).start();
68+
}
69+
70+
private void startMeasuring() {
71+
counter.set(0);
72+
targetCount = numberOfRequests;
73+
startTime = System.currentTimeMillis();
74+
}
75+
76+
public long waitComplete() {
77+
try {
78+
completed.await();
79+
} catch (InterruptedException e) {
80+
e.printStackTrace();
81+
}
82+
return endTime - startTime;
83+
}
84+
85+
/**
86+
* @return req/s
87+
*/
88+
public long getThroughput() {
89+
return targetCount * 1000L / (endTime - startTime);
90+
}
91+
92+
/**
93+
* notify the success of #count requests
94+
*
95+
* @return whether more requests should be performed
96+
*/
97+
private boolean success() {
98+
if (endTime != null) return false;
99+
if (counter.addAndGet(1) >= targetCount) {
100+
endTime = System.currentTimeMillis();
101+
completed.countDown();
102+
return false;
103+
}
104+
return true;
105+
}
106+
107+
private void start() {
108+
for (int i = 0; i < SYNC_THREADS; i++) {
109+
es.execute(() -> {
110+
boolean more = true;
111+
while (more) {
112+
sendRequest();
113+
more = success();
114+
}
115+
});
116+
}
117+
}
118+
119+
protected abstract void sendRequest();
120+
121+
protected abstract void shutdown();
122+
123+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package perf;
2+
3+
import com.arangodb.ArangoDB;
4+
import com.arangodb.DbName;
5+
import com.arangodb.Protocol;
6+
import com.arangodb.velocystream.Request;
7+
import com.arangodb.velocystream.RequestType;
8+
import org.junit.jupiter.api.Disabled;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.EnumSource;
11+
12+
@Disabled
13+
public class SyncBenchmarkTest {
14+
private final int warmupDurationSeconds = 15;
15+
private final int numberOfRequests = 1_000_000;
16+
17+
@ParameterizedTest
18+
@EnumSource(Protocol.class)
19+
void getVersion(Protocol protocol) {
20+
ArangoDB adb = new ArangoDB.Builder().useProtocol(protocol).build();
21+
Benchmark benchmark = new Benchmark(warmupDurationSeconds, numberOfRequests) {
22+
@Override
23+
protected void sendRequest() {
24+
adb.getVersion();
25+
}
26+
27+
@Override
28+
protected void shutdown() {
29+
adb.shutdown();
30+
}
31+
};
32+
benchmark.run();
33+
System.out.println("elapsed time [ms]: \t" + benchmark.waitComplete());
34+
System.out.println("throughput [req/s]: \t" + benchmark.getThroughput());
35+
}
36+
37+
@ParameterizedTest
38+
@EnumSource(Protocol.class)
39+
void getVersionWithDetails(Protocol protocol) {
40+
ArangoDB adb = new ArangoDB.Builder().useProtocol(protocol).build();
41+
Benchmark benchmark = new Benchmark(warmupDurationSeconds, numberOfRequests) {
42+
private final Request request = new Request(DbName.SYSTEM, RequestType.GET,
43+
"/_api/version").putQueryParam("details", true);
44+
45+
@Override
46+
protected void sendRequest() {
47+
adb.execute(request);
48+
}
49+
50+
@Override
51+
protected void shutdown() {
52+
adb.shutdown();
53+
}
54+
};
55+
benchmark.run();
56+
System.out.println("elapsed time [ms]: \t" + benchmark.waitComplete());
57+
System.out.println("throughput [req/s]: \t" + benchmark.getThroughput());
58+
}
59+
60+
}

0 commit comments

Comments
 (0)