@@ -48,7 +48,7 @@ public static Iterable<Object[]> data() {
4848 List <Object []> testData = new ArrayList <>();
4949 for (int poolSize = 1 ; poolSize <= 4 ; poolSize *= 2 ) {
5050 results .putIfAbsent (key (poolSize ), new TreeMap <>());
51- for (int threads = 1 ; threads <= 16 ; threads *= 2 ) {
51+ for (int threads = 1 ; threads <= 1 ; threads *= 2 ) {
5252 testData .add (new Object []{poolSize , threads });
5353 }
5454 }
@@ -59,28 +59,36 @@ private static String key(int poolSize) {
5959 return poolSize + " conn" ;
6060 }
6161
62- private static final int batchSize = 100 ;
62+ private static final int batchSize = 1000 ;
6363 private static final int repeats = 5 ;
6464 private static SortedMap <String , SortedMap <Integer , Long >> results = new TreeMap <>();
6565
6666 private final int poolSize ;
6767 private final int numThreads ;
68- private final ConnectionPool pool ;
68+ private ConnectionPool pool ;
69+ private PreparedStatement stmt ;
6970
7071 public PerformanceTest (int poolSize , int numThreads ) {
7172 this .poolSize = poolSize ;
7273 this .numThreads = numThreads ;
74+ }
75+
76+ @ Before
77+ public void setup () throws Exception {
7378 pool = dbr .builder
7479 .password ("async-pg" )
7580 .maxConnections (poolSize )
7681 .build ();
82+ stmt = pool .getConnection ().get ().prepareStatement (SELECT_42 ).get ();
7783 }
7884
7985 @ After
80- public void close () {
81- pool .close ();
86+ public void tearDown () {
87+ stmt .close ().join ();
88+ pool .close ().join ();
8289 }
8390
91+ /*
8492 @Test(timeout = 2000)
8593 public void t1_preAllocatePool() throws Exception {
8694 CompletableFuture.allOf((CompletableFuture<?>[]) IntStream.range(0, poolSize)
@@ -97,13 +105,14 @@ public void t1_preAllocatePool() throws Exception {
97105 .toArray(size -> new CompletableFuture<?>[size])
98106 ).get();
99107 }
108+ */
100109
101110 @ Test
102111 public void t3_run () {
103112 double mean = LongStream .range (0 , repeats )
104113 .map (i -> {
105114 try {
106- return performBatch ();
115+ return new Batch ( batchSize ). perform (). get ();
107116 } catch (Exception ex ) {
108117 throw new RuntimeException (ex );
109118 }
@@ -113,6 +122,42 @@ public void t3_run() {
113122 .put (numThreads , Math .round (mean ));
114123 }
115124
125+ private class Batch {
126+
127+ private long batchSize ;
128+ private long performed ;
129+ private long startedAt ;
130+ private CompletableFuture <Long > onBatch ;
131+
132+ Batch (long batchSize ) {
133+ this .batchSize = batchSize ;
134+ }
135+
136+ private CompletableFuture <Long > perform () {
137+ onBatch = new CompletableFuture <>();
138+ startedAt = System .currentTimeMillis ();
139+ nextSample ();
140+ return onBatch ;
141+ }
142+
143+ private void nextSample () {
144+ stmt .query ()
145+ .thenAccept (v -> {
146+ if (++performed < batchSize ) {
147+ nextSample ();
148+ } else {
149+ long duration = currentTimeMillis () - startedAt ;
150+ onBatch .complete (duration );
151+ }
152+ })
153+ .exceptionally (th -> {
154+ onBatch .completeExceptionally (th );
155+ return null ;
156+ });
157+ }
158+ }
159+
160+ /*
116161 private long performBatch() throws Exception {
117162 List<CompletableFuture<Void>> batchFutures = new ArrayList<>();
118163 long startTime = currentTimeMillis();
@@ -147,18 +192,16 @@ private long performBatch() throws Exception {
147192 throw new AssertionError(th);
148193 }));
149194
150- /*
151- batchFutures.add(pool.completeScript("select 42").thenAccept(rs -> {
152- }));
153- */
195+ // batchFutures.add(pool.completeScript(SELECT_42).thenAccept(rs -> {
196+ // }));
154197 }
155198 CompletableFuture
156199 .allOf(batchFutures.toArray(new CompletableFuture<?>[]{}))
157200 .get();
158201 long duration = currentTimeMillis() - startTime;
159202 return duration;
160203 }
161-
204+ */
162205 @ AfterClass
163206 public static void printResults () {
164207 out .println ();
0 commit comments