@@ -48,7 +48,7 @@ public static Iterable<Object[]> data() {
48
48
List <Object []> testData = new ArrayList <>();
49
49
for (int poolSize = 1 ; poolSize <= 4 ; poolSize *= 2 ) {
50
50
results .putIfAbsent (key (poolSize ), new TreeMap <>());
51
- for (int threads = 1 ; threads <= 16 ; threads *= 2 ) {
51
+ for (int threads = 1 ; threads <= 1 ; threads *= 2 ) {
52
52
testData .add (new Object []{poolSize , threads });
53
53
}
54
54
}
@@ -59,28 +59,36 @@ private static String key(int poolSize) {
59
59
return poolSize + " conn" ;
60
60
}
61
61
62
- private static final int batchSize = 100 ;
62
+ private static final int batchSize = 1000 ;
63
63
private static final int repeats = 5 ;
64
64
private static SortedMap <String , SortedMap <Integer , Long >> results = new TreeMap <>();
65
65
66
66
private final int poolSize ;
67
67
private final int numThreads ;
68
- private final ConnectionPool pool ;
68
+ private ConnectionPool pool ;
69
+ private PreparedStatement stmt ;
69
70
70
71
public PerformanceTest (int poolSize , int numThreads ) {
71
72
this .poolSize = poolSize ;
72
73
this .numThreads = numThreads ;
74
+ }
75
+
76
+ @ Before
77
+ public void setup () throws Exception {
73
78
pool = dbr .builder
74
79
.password ("async-pg" )
75
80
.maxConnections (poolSize )
76
81
.build ();
82
+ stmt = pool .getConnection ().get ().prepareStatement (SELECT_42 ).get ();
77
83
}
78
84
79
85
@ After
80
- public void close () {
81
- pool .close ();
86
+ public void tearDown () {
87
+ stmt .close ().join ();
88
+ pool .close ().join ();
82
89
}
83
90
91
+ /*
84
92
@Test(timeout = 2000)
85
93
public void t1_preAllocatePool() throws Exception {
86
94
CompletableFuture.allOf((CompletableFuture<?>[]) IntStream.range(0, poolSize)
@@ -97,13 +105,14 @@ public void t1_preAllocatePool() throws Exception {
97
105
.toArray(size -> new CompletableFuture<?>[size])
98
106
).get();
99
107
}
108
+ */
100
109
101
110
@ Test
102
111
public void t3_run () {
103
112
double mean = LongStream .range (0 , repeats )
104
113
.map (i -> {
105
114
try {
106
- return performBatch ();
115
+ return new Batch ( batchSize ). perform (). get ();
107
116
} catch (Exception ex ) {
108
117
throw new RuntimeException (ex );
109
118
}
@@ -113,6 +122,42 @@ public void t3_run() {
113
122
.put (numThreads , Math .round (mean ));
114
123
}
115
124
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
8000
td>+ })
153
+ .exceptionally (th -> {
154
+ onBatch .completeExceptionally (th );
155
+ return null ;
156
+ });
157
+ }
158
+ }
159
+
160
+ /*
116
161
private long performBatch() throws Exception {
117
162
List<CompletableFuture<Void>> batchFutures = new ArrayList<>();
118
163
long startTime = currentTimeMillis();
@@ -147,18 +192,16 @@ private long performBatch() throws Exception {
147
192
throw new AssertionError(th);
148
193
}));
149
194
150
- /*
151
- batchFutures.add(pool.completeScript("select 42").thenAccept(rs -> {
152
- }));
153
- */
195
+ // batchFutures.add(pool.completeScript(SELECT_42).thenAccept(rs -> {
196
+ // }));
154
197
}
155
198
CompletableFuture
156
199
.allOf(batchFutures.toArray(new CompletableFuture<?>[]{}))
157
200
.get();
158
201
long duration = currentTimeMillis() - startTime;
159
202
return duration;
160
203
}
161
-
204
+ */
162
205
@ AfterClass
163
206
public static void printResults () {
164
207
out .println ();
0 commit comments