Skip to content

Commit 5c4b37f

Browse files
Threads count variance restored in performance test
1 parent 660e94e commit 5c4b37f

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

src/test/java/com/github/pgasync/PerformanceTest.java

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,18 @@ public class PerformanceTest {
4444
public static Iterable<Object[]> data() {
4545
results = new TreeMap<>();
4646
List<Object[]> testData = new ArrayList<>();
47-
for (int poolSize = 1; poolSize <= 4; poolSize *= 2) {
48-
results.putIfAbsent(key(poolSize), new TreeMap<>());
49-
for (int threads = 1; threads <= 1; threads *= 2) {
47+
for (int poolSize = 1; poolSize <= 16; poolSize *= 2) {
48+
results.putIfAbsent(poolSize, new TreeMap<>());
49+
for (int threads = 1; threads <= 16; threads *= 2) {
5050
testData.add(new Object[]{poolSize, threads});
5151
}
5252
}
5353
return testData;
5454
}
5555

56-
private static String key(int poolSize) {
57-
return poolSize + " conn";
58-
}
59-
6056
private static final int batchSize = 1000;
6157
private static final int repeats = 5;
62-
private static SortedMap<String, SortedMap<Integer, Long>> results = new TreeMap<>();
58+
private static SortedMap<Integer, SortedMap<Integer, Long>> results = new TreeMap<>();
6359

6460
private final int poolSize;
6561
private final int numThreads;
@@ -75,7 +71,7 @@ public void setup() {
7571
pool = dbr.builder
7672
.password("async-pg")
7773
.maxConnections(poolSize)
78-
.build();
74+
.build(Executors.newFixedThreadPool(numThreads));
7975
List<Connection> connections = IntStream.range(0, poolSize)
8076
.mapToObj(i -> pool.getConnection().join()).collect(Collectors.toList());
8177
connections.forEach(connection -> {
@@ -94,16 +90,29 @@ public void observeSomeBatches() {
9490
double mean = LongStream.range(0, repeats)
9591
.map(i -> {
9692
try {
97-
return new Batch(batchSize).perform().get();
93+
List<CompletableFuture<Long>> batches = IntStream.range(0, poolSize)
94+
//.mapToObj(ci -> startBatchWithPreparedStatement())
95+
.mapToObj(ci -> startBatchWithSimpleQuery())
96+
.collect(Collectors.toList());
97+
CompletableFuture.allOf(batches.toArray(new CompletableFuture<?>[]{})).get();
98+
return batches.stream().map(CompletableFuture::join).max(Long::compare).get();
9899
} catch (Exception ex) {
99100
throw new RuntimeException(ex);
100101
}
101102
})
102103
.average().getAsDouble();
103-
results.computeIfAbsent(poolSize + " conn", k -> new TreeMap<>())
104+
results.computeIfAbsent(poolSize, k -> new TreeMap<>())
104105
.put(numThreads, Math.round(mean));
105106
}
106107

108+
private CompletableFuture<Long> startBatchWithPreparedStatement() {
109+
return new Batch(batchSize).startWithPreparedStatement();
110+
}
111+
112+
private CompletableFuture<Long> startBatchWithSimpleQuery() {
113+
return new Batch(batchSize).startWithSimpleQuery();
114+
}
115+
107116
private class Batch {
108117

109118
private long batchSize;
@@ -115,14 +124,21 @@ private class Batch {
115124
this.batchSize = batchSize;
116125
}
117126

118-
private CompletableFuture<Long> perform() {
127+
private CompletableFuture<Long> startWithPreparedStatement() {
119128
onBatch = new CompletableFuture<>();
120129
startedAt = System.currentTimeMillis();
121-
nextSample();
130+
nextSamplePreparedStatement();
122131
return onBatch;
123132
}
124133

125-
private void nextSample() {
134+
private CompletableFuture<Long> startWithSimpleQuery() {
135+
onBatch = new CompletableFuture<>();
136+
startedAt = System.currentTimeMillis();
137+
nextSampleSimpleQuery();
138+
return onBatch;
139+
}
140+
141+
private void nextSamplePreparedStatement() {
126142
pool.getConnection()
127143
.thenApply(connection ->
128144
connection.prepareStatement(SELECT_42)
@@ -143,7 +159,7 @@ private void nextSample() {
143159
.thenCompose(Function.identity())
144160
.thenAccept(v -> {
145161
if (++performed < batchSize) {
146-
nextSample();
162+
nextSamplePreparedStatement();
147163
} else {
148164
long duration = currentTimeMillis() - startedAt;
149165
onBatch.complete(duration);
@@ -155,21 +171,37 @@ private void nextSample() {
155171
});
156172

157173
}
174+
175+
private void nextSampleSimpleQuery() {
176+
pool.completeScript(SELECT_42)
177+
.thenAccept(v -> {
178+
if (++performed < batchSize) {
179+
nextSamplePreparedStatement();
180+
} else {
181+
long duration = currentTimeMillis() - startedAt;
182+
onBatch.complete(duration);
183+
}
184+
})
185+
.exceptionally(th -> {
186+
onBatch.completeExceptionally(th);
187+
return null;
188+
});
189+
}
158190
}
159191

160192
@AfterClass
161193
public static void printResults() {
162194
out.println();
163195
out.println("Requests per second, Hz:");
164196
out.print(" threads");
165-
results.keySet().forEach(i -> out.printf("\t%s\t", i));
197+
results.keySet().forEach(i -> out.printf("\t%d conn\t", i));
166198
out.println();
167199

168200
results.values().iterator().next().keySet().forEach(threads -> {
169201
out.print(" " + threads);
170202
results.keySet().forEach(connections -> {
171203
long batchDuration = results.get(connections).get(threads);
172-
double rps = 1000 * batchSize / (double) batchDuration;
204+
double rps = 1000 * batchSize * connections / (double) batchDuration;
173205
out.printf("\t\t%d", Math.round(rps));
174206
});
175207
out.println();

0 commit comments

Comments
 (0)