@@ -44,22 +44,18 @@ public class PerformanceTest {
44
44
public static Iterable <Object []> data () {
45
45
results = new TreeMap <>();
46
46
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 ) {
50
50
testData .add (new Object []{poolSize , threads });
51
51
}
52
52
}
53
53
return testData ;
54
54
}
55
55
56
- private static String key (int poolSize ) {
57
- return poolSize + " conn" ;
58
- }
59
-
60
56
private static final int batchSize = 1000 ;
61
57
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 <>();
63
59
64
60
private final int poolSize ;
65
61
private final int numThreads ;
@@ -75,7 +71,7 @@ public void setup() {
75
71
pool = dbr .builder
76
72
.password ("async-pg" )
77
73
.maxConnections (poolSize )
78
- .build ();
74
+ .build (Executors . newFixedThreadPool ( numThreads ) );
79
75
List <Connection > connections = IntStream .range (0 , poolSize )
80
76
.mapToObj (i -> pool .getConnection ().join ()).collect (Collectors .toList ());
81
77
connections .forEach (connection -> {
@@ -94,16 +90,29 @@ public void observeSomeBatches() {
94
90
double mean = LongStream .range (0 , repeats )
95
91
.map (i -> {
96
92
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 ();
98
99
} catch (Exception ex ) {
99
100
throw new RuntimeException (ex );
100
101
}
101
102
})
102
103
.average ().getAsDouble ();
103
- results .computeIfAbsent (poolSize + " conn" , k -> new TreeMap <>())
104
+ results .computeIfAbsent (poolSize , k -> new TreeMap <>())
104
105
.put (numThreads , Math .round (mean ));
105
106
}
106
107
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
+
107
116
private class Batch {
108
117
109
118
private long batchSize ;
@@ -115,14 +124,21 @@ private class Batch {
115
124
this .batchSize = batchSize ;
116
125
}
117
126
118
- private CompletableFuture <Long > perform () {
127
+ private CompletableFuture <Long > startWithPreparedStatement () {
119
128
onBatch = new CompletableFuture <>();
120
129
startedAt = System .currentTimeMillis ();
121
- nextSample ();
130
+ nextSamplePreparedStatement ();
122
131
return onBatch ;
123
132
}
124
133
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 () {
126
142
pool .getConnection ()
127
143
.thenApply (connection ->
128
144
connection .prepareStatement (SELECT_42 )
@@ -143,7 +159,7 @@ private void nextSample() {
143
159
.thenCompose (Function .identity ())
144
160
.thenAccept (v -> {
145
161
if (++performed < batchSize ) {
146
- nextSample ();
162
+ nextSamplePreparedStatement ();
147
163
} else {
148
164
long duration = currentTimeMillis () - startedAt ;
149
165
onBatch .complete (duration );
@@ -155,21 +171,37 @@ private void nextSample() {
155
171
});
156
172
157
173
}
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
+ }
158
190
}
159
191
160
192
@ AfterClass
161
193
public static void printResults () {
162
194
out .println ();
163
195
out .println ("Requests per second, Hz:" );
164
196
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 ));
166
198
out .println ();
167
199
168
200
results .values ().iterator ().next ().keySet ().forEach (threads -> {
169
201
out .print (" " + threads );
170
202
results .keySet ().forEach (connections -> {
171
203
long batchDuration = results .get (connections ).get (threads );
172
- double rps = 1000 * batchSize / (double ) batchDuration ;
204
+ double rps = 1000 * batchSize * connections / (double ) batchDuration ;
173
205
out .printf ("\t \t %d" , Math .round (rps ));
174
206
});
175
207
out .println ();
0 commit comments