1717package software .amazon .jdbc .benchmarks ;
1818
1919import static org .mockito .ArgumentMatchers .any ;
20+ import static org .mockito .ArgumentMatchers .anyBoolean ;
2021import static org .mockito .ArgumentMatchers .anyString ;
2122import static org .mockito .ArgumentMatchers .eq ;
2223import static org .mockito .Mockito .when ;
2324
25+ import com .zaxxer .hikari .HikariConfig ;
2426import java .sql .Connection ;
2527import java .sql .ResultSet ;
2628import java .sql .SQLException ;
4648import org .openjdk .jmh .runner .RunnerException ;
4749import org .openjdk .jmh .runner .options .Options ;
4850import org .openjdk .jmh .runner .options .OptionsBuilder ;
51+ import software .amazon .jdbc .ConnectionPluginManager ;
4952import software .amazon .jdbc .ConnectionProvider ;
53+ import software .amazon .jdbc .ConnectionProviderManager ;
54+ import software .amazon .jdbc .HikariPooledConnectionProvider ;
55+ import software .amazon .jdbc .HostListProviderService ;
5056import software .amazon .jdbc .HostSpec ;
57+ import software .amazon .jdbc .PluginManagerService ;
5158import software .amazon .jdbc .PluginService ;
59+ import software .amazon .jdbc .benchmarks .testplugin .TestConnectionWrapper ;
5260import software .amazon .jdbc .dialect .Dialect ;
5361import software .amazon .jdbc .wrapper .ConnectionWrapper ;
5462
@@ -63,13 +71,16 @@ public class PluginBenchmarks {
6371 private static final String WRITER_SESSION_ID = "MASTER_SESSION_ID" ;
6472 private static final String FIELD_SERVER_ID = "SERVER_ID" ;
6573 private static final String FIELD_SESSION_ID = "SESSION_ID" ;
66- private static final String CONNECTION_STRING = "driverProtocol ://my.domain.com" ;
74+ private static final String CONNECTION_STRING = "jdbc:postgresql ://my.domain.com" ;
6775 private static final String PG_CONNECTION_STRING =
6876 "jdbc:aws-wrapper:postgresql://instance-0.XYZ.us-east-2.rds.amazonaws.com" ;
6977 private static final int TEST_PORT = 5432 ;
7078 private final HostSpec writerHostSpec = new HostSpec ("instance-0" , TEST_PORT );
7179
7280 @ Mock private PluginService mockPluginService ;
81+ @ Mock private ConnectionPluginManager mockConnectionPluginManager ;
82+ @ Mock private HostListProviderService mockHostListProviderService ;
83+ @ Mock private PluginManagerService mockPluginManagerService ;
7384 @ Mock ConnectionProvider mockConnectionProvider ;
7485 @ Mock Connection mockConnection ;
7586 @ Mock Statement mockStatement ;
@@ -89,6 +100,11 @@ public static void main(String[] args) throws RunnerException {
89100 @ Setup (Level .Iteration )
90101 public void setUpIteration () throws Exception {
91102 closeable = MockitoAnnotations .openMocks (this );
103+ when (mockConnectionPluginManager .connect (any (), any (), any (Properties .class ), anyBoolean ()))
104+ .thenReturn (mockConnection );
105+ when (mockConnectionPluginManager .execute (
106+ any (), any (), any (), eq ("Connection.createStatement" ), any (), any ()))
107+ .thenReturn (mockStatement );
92108 when (mockConnectionProvider .connect (anyString (), any (Properties .class ))).thenReturn (
93109 mockConnection );
94110 when (mockConnectionProvider .connect (anyString (), any (Dialect .class ), any (HostSpec .class ), any (Properties .class )))
@@ -101,6 +117,7 @@ public void setUpIteration() throws Exception {
101117 .thenReturn ("instance-0" , "instance-1" );
102118 when (mockResultSet .getStatement ()).thenReturn (mockStatement );
103119 when (mockStatement .getConnection ()).thenReturn (mockConnection );
120+ when (this .mockPluginService .acceptsStrategy (any (), eq ("random" ))).thenReturn (true );
104121 when (this .mockPluginService .getCurrentHostSpec ()).thenReturn (writerHostSpec );
105122 }
106123
@@ -110,48 +127,60 @@ public void tearDownIteration() throws Exception {
110127 }
111128
112129 @ Benchmark
113- public void initAndReleaseBaseLine () throws SQLException {
130+ public void initAndReleaseBaseLine () {
114131 }
115132
116133 @ Benchmark
117134 public ConnectionWrapper initAndReleaseWithExecutionTimePlugin () throws SQLException {
118- try (ConnectionWrapper wrapper = new ConnectionWrapper (
135+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
119136 useExecutionTimePlugin (),
120137 CONNECTION_STRING ,
121- mockConnectionProvider )) {
138+ mockConnectionPluginManager ,
139+ mockPluginService ,
140+ mockHostListProviderService ,
141+ mockPluginManagerService )) {
122142 wrapper .releaseResources ();
123143 return wrapper ;
124144 }
125145 }
126146
127147 @ Benchmark
128148 public ConnectionWrapper initAndReleaseWithAuroraHostListPlugin () throws SQLException {
129- try (ConnectionWrapper wrapper = new ConnectionWrapper (
149+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
130150 useAuroraHostListPlugin (),
131151 CONNECTION_STRING ,
132- mockConnectionProvider )) {
152+ mockConnectionPluginManager ,
153+ mockPluginService ,
154+ mockHostListProviderService ,
155+ mockPluginManagerService )) {
133156 wrapper .releaseResources ();
134157 return wrapper ;
135158 }
136159 }
137160
138161 @ Benchmark
139162 public ConnectionWrapper initAndReleaseWithExecutionTimeAndAuroraHostListPlugins () throws SQLException {
140- try (ConnectionWrapper wrapper = new ConnectionWrapper (
163+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
141164 useExecutionTimeAndAuroraHostListPlugins (),
142165 CONNECTION_STRING ,
143- mockConnectionProvider )) {
166+ mockConnectionPluginManager ,
167+ mockPluginService ,
168+ mockHostListProviderService ,
169+ mockPluginManagerService )) {
144170 wrapper .releaseResources ();
145171 return wrapper ;
146172 }
147173 }
148174
149175 @ Benchmark
150176 public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin () throws SQLException {
151- try (ConnectionWrapper wrapper = new ConnectionWrapper (
177+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
152178 useReadWriteSplittingPlugin (),
153179 CONNECTION_STRING ,
154- mockConnectionProvider )) {
180+ mockConnectionPluginManager ,
181+ mockPluginService ,
182+ mockHostListProviderService ,
183+ mockPluginManagerService )) {
155184 wrapper .releaseResources ();
156185 return wrapper ;
157186 }
@@ -160,46 +189,66 @@ public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin() throws SQL
160189 @ Benchmark
161190 public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPlugin ()
162191 throws SQLException {
163- try (ConnectionWrapper wrapper = new ConnectionWrapper (
192+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
164193 useAuroraHostListAndReadWriteSplittingPlugin (),
165194 PG_CONNECTION_STRING ,
166- mockConnectionProvider )) {
195+ mockConnectionPluginManager ,
196+ mockPluginService ,
197+ mockHostListProviderService ,
198+ mockPluginManagerService )) {
167199 wrapper .releaseResources ();
168200 return wrapper ;
169201 }
170202 }
171203
172204 @ Benchmark
173- public ConnectionWrapper initAndReleaseWithReadWriteSplittingPluginWithReaderLoadBalancing ()
174- throws SQLException {
175- try (ConnectionWrapper wrapper = new ConnectionWrapper (
176- useReadWriteSplittingPluginWithReaderLoadBalancing (),
205+ public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin_internalConnectionPools () throws SQLException {
206+ HikariPooledConnectionProvider provider =
207+ new HikariPooledConnectionProvider ((hostSpec , props ) -> new HikariConfig ());
208+ ConnectionProviderManager .setConnectionProvider (provider );
209+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
210+ useReadWriteSplittingPlugin (),
177211 CONNECTION_STRING ,
178- mockConnectionProvider )) {
212+ mockConnectionPluginManager ,
213+ mockPluginService ,
214+ mockHostListProviderService ,
215+ mockPluginManagerService )) {
179216 wrapper .releaseResources ();
217+ ConnectionProviderManager .releaseResources ();
218+ ConnectionProviderManager .resetProvider ();
180219 return wrapper ;
181220 }
182221 }
183222
184223 @ Benchmark
185- public ConnectionWrapper
186- initAndReleaseWithAuroraHostListAndReadWriteSplittingPluginWithReaderLoadBalancing ()
224+ public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPlugin_internalConnectionPools ()
187225 throws SQLException {
188- try (ConnectionWrapper wrapper = new ConnectionWrapper (
189- useAuroraHostListAndReadWriteSplittingPluginWithReaderLoadBalancing (),
226+ HikariPooledConnectionProvider provider =
227+ new HikariPooledConnectionProvider ((hostSpec , props ) -> new HikariConfig ());
228+ ConnectionProviderManager .setConnectionProvider (provider );
229+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
230+ useAuroraHostListAndReadWriteSplittingPlugin (),
190231 PG_CONNECTION_STRING ,
191- mockConnectionProvider )) {
232+ mockConnectionPluginManager ,
233+ mockPluginService ,
234+ mockHostListProviderService ,
235+ mockPluginManagerService )) {
192236 wrapper .releaseResources ();
237+ ConnectionProviderManager .releaseResources ();
238+ ConnectionProviderManager .resetProvider ();
193239 return wrapper ;
194240 }
195241 }
196242
197243 @ Benchmark
198244 public Statement executeStatementBaseline () throws SQLException {
199- try (ConnectionWrapper wrapper = new ConnectionWrapper (
245+ try (ConnectionWrapper wrapper = new TestConnectionWrapper (
200246 useExecutionTimePlugin (),
201247 CONNECTION_STRING ,
202- mockConnectionProvider );
248+ mockConnectionPluginManager ,
249+ mockPluginService ,
250+ mockHostListProviderService ,
251+ mockPluginManagerService );
203252 Statement statement = wrapper .createStatement ()) {
204253 return statement ;
205254 }
@@ -208,10 +257,13 @@ public Statement executeStatementBaseline() throws SQLException {
208257 @ Benchmark
209258 public ResultSet executeStatementWithExecutionTimePlugin () throws SQLException {
210259 try (
211- ConnectionWrapper wrapper = new ConnectionWrapper (
260+ ConnectionWrapper wrapper = new TestConnectionWrapper (
212261 useExecutionTimePlugin (),
213262 CONNECTION_STRING ,
214- mockConnectionProvider );
263+ mockConnectionPluginManager ,
264+ mockPluginService ,
265+ mockHostListProviderService ,
266+ mockPluginManagerService );
215267 Statement statement = wrapper .createStatement ();
216268 ResultSet resultSet = statement .executeQuery ("some sql" )) {
217269 return resultSet ;
@@ -247,18 +299,4 @@ Properties useAuroraHostListAndReadWriteSplittingPlugin() {
247299 properties .setProperty ("wrapperPlugins" , "auroraHostList,readWriteSplitting" );
248300 return properties ;
249301 }
250-
251- Properties useReadWriteSplittingPluginWithReaderLoadBalancing () {
252- final Properties properties = new Properties ();
253- properties .setProperty ("wrapperPlugins" , "readWriteSplitting" );
254- properties .setProperty ("loadBalanceReadOnlyTraffic" , "true" );
255- return properties ;
256- }
257-
258- Properties useAuroraHostListAndReadWriteSplittingPluginWithReaderLoadBalancing () {
259- final Properties properties = new Properties ();
260- properties .setProperty ("wrapperPlugins" , "auroraHostList,readWriteSplitting" );
261- properties .setProperty ("loadBalanceReadOnlyTraffic" , "true" );
262- return properties ;
263- }
264302}
0 commit comments