Skip to content

Commit 660e94e

Browse files
Connection pool utilization added to performance test
1 parent dfefb4d commit 660e94e

File tree

1 file changed

+29
-74
lines changed

1 file changed

+29
-74
lines changed

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

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,25 @@
1414

1515
package com.github.pgasync;
1616

17+
import com.pgasync.Connection;
1718
import com.pgasync.ConnectionPool;
18-
import com.pgasync.PreparedStatement;
1919
import org.junit.*;
2020
import org.junit.runner.RunWith;
2121
import org.junit.runners.Parameterized;
2222
import org.junit.runners.Parameterized.Parameters;
2323

2424
import java.util.*;
2525
import java.util.concurrent.*;
26-
import java.util.concurrent.atomic.LongAdder;
2726
import java.util.function.Function;
27+
import java.util.stream.Collectors;
2828
import java.util.stream.IntStream;
2929
import java.util.stream.LongStream;
3030

3131
import static com.github.pgasync.DatabaseRule.createPoolBuilder;
3232
import static java.lang.System.currentTimeMillis;
3333
import static java.lang.System.out;
34-
import static org.junit.runners.MethodSorters.NAME_ASCENDING;
3534

3635
@RunWith(Parameterized.class)
37-
@FixMethodOrder(NAME_ASCENDING)
3836
public class PerformanceTest {
3937

4038
private static final String SELECT_42 = "select 42";
@@ -66,49 +64,33 @@ private static String key(int poolSize) {
6664
private final int poolSize;
6765
private final int numThreads;
6866
private ConnectionPool pool;
69-
private PreparedStatement stmt;
7067

7168
public PerformanceTest(int poolSize, int numThreads) {
7269
this.poolSize = poolSize;
7370
this.numThreads = numThreads;
7471
}
7572

7673
@Before
77-
public void setup() throws Exception {
74+
public void setup() {
7875
pool = dbr.builder
7976
.password("async-pg")
8077
.maxConnections(poolSize)
8178
.build();
82-
stmt = pool.getConnection().get().prepareStatement(SELECT_42).get();
79+
List<Connection> connections = IntStream.range(0, poolSize)
80+
.mapToObj(i -> pool.getConnection().join()).collect(Collectors.toList());
81+
connections.forEach(connection -> {
82+
connection.prepareStatement(SELECT_42).join().close().join();
83+
connection.close().join();
84+
});
8385
}
8486

8587
@After
8688
public void tearDown() {
87-
stmt.close().join();
8889
pool.close().join();
8990
}
9091

91-
/*
92-
@Test(timeout = 2000)
93-
public void t1_preAllocatePool() throws Exception {
94-
CompletableFuture.allOf((CompletableFuture<?>[]) IntStream.range(0, poolSize)
95-
.mapToObj(i -> pool.getConnection()
96-
.thenApply(connection ->
97-
connection.prepareStatement(SELECT_42)
98-
.thenApply(PreparedStatement::close)
99-
.thenCompose(Function.identity())
100-
.thenApply(v -> connection.close())
101-
.thenCompose(Function.identity())
102-
)
103-
.thenCompose(Function.identity())
104-
)
105-
.toArray(size -> new CompletableFuture<?>[size])
106-
).get();
107-
}
108-
*/
109-
11092
@Test
111-
public void t3_run() {
93+
public void observeSomeBatches() {
11294
double mean = LongStream.range(0, repeats)
11395
.map(i -> {
11496
try {
@@ -141,7 +123,24 @@ private CompletableFuture<Long> perform() {
141123
}
142124

143125
private void nextSample() {
144-
stmt.query()
126+
pool.getConnection()
127+
.thenApply(connection ->
128+
connection.prepareStatement(SELECT_42)
129+
.thenApply(stmt -> stmt.query()
130+
.thenApply(rs -> stmt.close())
131+
.exceptionally(th -> stmt.close().whenComplete((v, _th) -> {
132+
throw new RuntimeException(th);
133+
}))
134+
.thenCompose(Function.identity())
135+
.thenApply(v -> connection.close())
136+
.exceptionally(th -> connection.close().whenComplete((v, _th) -> {
137+
throw new RuntimeException(th);
138+
}))
139+
.thenCompose(Function.identity())
140+
)
141+
.thenCompose(Function.identity())
142+
)
143+
.thenCompose(Function.identity())
145144
.thenAccept(v -> {
146145
if (++performed < batchSize) {
147146
nextSample();
@@ -154,54 +153,10 @@ private void nextSample() {
154153
onBatch.completeExceptionally(th);
155154
return null;
156155
});
157-
}
158-
}
159-
160-
/*
161-
private long performBatch() throws Exception {
162-
List<CompletableFuture<Void>> batchFutures = new ArrayList<>();
163-
long startTime = currentTimeMillis();
164-
for (int i = 0; i < batchSize; i++) {
165-
166-
batchFutures.add(pool.getConnection()
167-
.thenApply(connection -> connection.prepareStatement(SELECT_42)
168-
.thenApply(stmt -> {
169-
return stmt.query()
170-
.handle((v, th) ->
171-
stmt.close()
172-
.thenAccept(_v -> {
173-
if (th != null) {
174-
throw new RuntimeException(th);
175-
}
176-
})
177-
)
178-
.thenCompose(Function.identity());
179-
}
180-
)
181-
.thenCompose(Function.identity())
182-
.handle((v, th) -> connection.close()
183-
.thenAccept(_v -> {
184-
if (th != null) {
185-
throw new RuntimeException(th);
186-
}
187-
}))
188-
.thenCompose(Function.identity())
189-
)
190-
.thenCompose(Function.identity())
191-
.exceptionally(th -> {
192-
throw new AssertionError(th);
193-
}));
194156

195-
// batchFutures.add(pool.completeScript(SELECT_42).thenAccept(rs -> {
196-
// }));
197157
}
198-
CompletableFuture
199-
.allOf(batchFutures.toArray(new CompletableFuture<?>[]{}))
200-
.get();
201-
long duration = currentTimeMillis() - startTime;
202-
return duration;
203158
}
204-
*/
159+
205160
@AfterClass
206161
public static void printResults() {
207162
out.println();

0 commit comments

Comments
 (0)