33
44package com .azure .perf .test .core ;
55
6+ import com .azure .core .client .traits .HttpTrait ;
67import com .azure .core .http .HttpClient ;
78import com .azure .core .http .netty .NettyAsyncHttpClientBuilder ;
9+ import com .azure .core .http .netty .NettyAsyncHttpClientProvider ;
10+ import com .azure .core .http .okhttp .OkHttpAsyncClientProvider ;
11+ import com .azure .core .http .okhttp .OkHttpAsyncHttpClientBuilder ;
812import com .azure .core .http .policy .HttpPipelinePolicy ;
913import io .netty .handler .ssl .SslContext ;
1014import io .netty .handler .ssl .SslContextBuilder ;
1115import io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
16+ import okhttp3 .OkHttpClient ;
1217import reactor .core .publisher .Flux ;
1318import reactor .core .publisher .Mono ;
1419
20+ import javax .net .ssl .SSLContext ;
1521import javax .net .ssl .SSLException ;
16- import java . lang . reflect . Method ;
22+ import javax . net . ssl . X509TrustManager ;
1723import java .net .URI ;
24+ import java .security .KeyManagementException ;
25+ import java .security .NoSuchAlgorithmException ;
26+ import java .security .SecureRandom ;
1827import java .util .Arrays ;
1928import java .util .concurrent .CompletableFuture ;
2029
@@ -42,33 +51,11 @@ public abstract class ApiPerfTestBase<TOptions extends PerfStressOptions> extend
4251 */
4352 public ApiPerfTestBase (TOptions options ) {
4453 super (options );
45- final SslContext sslContext ;
46- if (options .isInsecure ()) {
47- try {
48- sslContext = SslContextBuilder .forClient ()
49- .trustManager (InsecureTrustManagerFactory .INSTANCE )
50- .build ();
51- } catch (SSLException e ) {
52- throw new IllegalStateException (e );
53- }
5454
55- reactor .netty .http .client .HttpClient nettyHttpClient = reactor .netty .http .client .HttpClient .create ()
56- .secure (sslContextSpec -> sslContextSpec .sslContext (sslContext ));
57-
58- httpClient = new NettyAsyncHttpClientBuilder (nettyHttpClient ).build ();
59- } else {
60- sslContext = null ;
61- httpClient = null ;
62- }
55+ httpClient = createHttpClient (options );
6356
6457 if (options .getTestProxies () != null && !options .getTestProxies ().isEmpty ()) {
65- if (options .isInsecure ()) {
66- recordPlaybackHttpClient = reactor .netty .http .client .HttpClient .create ()
67- .secure (sslContextSpec -> sslContextSpec .sslContext (sslContext ));
68- } else {
69- recordPlaybackHttpClient = reactor .netty .http .client .HttpClient .create ();
70- }
71-
58+ recordPlaybackHttpClient = createRecordPlaybackClient (options );
7259 testProxy = options .getTestProxies ().get (parallelIndex % options .getTestProxies ().size ());
7360 testProxyPolicy = new TestProxyPolicy (testProxy );
7461 policies = Arrays .asList (testProxyPolicy );
@@ -80,30 +67,81 @@ public ApiPerfTestBase(TOptions options) {
8067 }
8168 }
8269
70+ private static HttpClient createHttpClient (PerfStressOptions options ) {
71+ PerfStressOptions .HttpClientType httpClientType = options .getHttpClient ();
72+ switch (httpClientType ) {
73+ case NETTY :
74+ if (options .isInsecure ()) {
75+ try {
76+ SslContext sslContext = SslContextBuilder .forClient ()
77+ .trustManager (InsecureTrustManagerFactory .INSTANCE )
78+ .build ();
79+
80+ reactor .netty .http .client .HttpClient nettyHttpClient =
81+ reactor .netty .http .client .HttpClient .create ()
82+ .secure (sslContextSpec -> sslContextSpec .sslContext (sslContext ));
83+
84+ return new NettyAsyncHttpClientBuilder (nettyHttpClient ).build ();
85+ } catch (SSLException e ) {
86+ throw new IllegalStateException (e );
87+ }
88+ } else {
89+ return new NettyAsyncHttpClientProvider ().createInstance ();
90+ }
91+ case OKHTTP :
92+ if (options .isInsecure ()) {
93+ try {
94+ SSLContext sslContext = SSLContext .getInstance ("SSL" );
95+ sslContext .init (
96+ null , InsecureTrustManagerFactory .INSTANCE .getTrustManagers (), new SecureRandom ());
97+ OkHttpClient okHttpClient = new OkHttpClient .Builder ()
98+ .sslSocketFactory (sslContext .getSocketFactory (),
99+ (X509TrustManager ) InsecureTrustManagerFactory .INSTANCE .getTrustManagers ()[0 ])
100+ .build ();
101+ return new OkHttpAsyncHttpClientBuilder (okHttpClient ).build ();
102+ } catch (NoSuchAlgorithmException | KeyManagementException e ) {
103+ throw new IllegalStateException (e );
104+ }
105+ } else {
106+ return new OkHttpAsyncClientProvider ().createInstance ();
107+ }
108+ default :
109+ throw new IllegalArgumentException ("Unsupported http client " + httpClientType );
110+ }
111+ }
112+
113+ private static reactor .netty .http .client .HttpClient createRecordPlaybackClient (PerfStressOptions options ) {
114+ if (options .isInsecure ()) {
115+ try {
116+ SslContext sslContext = SslContextBuilder .forClient ()
117+ .trustManager (InsecureTrustManagerFactory .INSTANCE )
118+ .build ();
119+ return reactor .netty .http .client .HttpClient .create ()
120+ .secure (sslContextSpec -> sslContextSpec .sslContext (sslContext ));
121+ } catch (SSLException e ) {
122+ throw new IllegalStateException (e );
123+ }
124+ } else {
125+ return reactor .netty .http .client .HttpClient .create ();
126+ }
127+ }
128+
83129 /**
84130 * Attempts to configure a ClientBuilder using reflection. If a ClientBuilder does not follow the standard convention,
85131 * it can be configured manually using the "httpClient" and "policies" fields.
86132 * @param clientBuilder The client builder.
87133 * @throws IllegalStateException If reflective access to get httpClient or addPolicy methods fail.
88134 */
89- protected void configureClientBuilder (Object clientBuilder ) {
135+ protected void configureClientBuilder (HttpTrait <?> clientBuilder ) {
90136 if (httpClient != null || policies != null ) {
91- Class <?> clientBuilderClass = clientBuilder .getClass ();
92-
93- try {
94- if (httpClient != null ) {
95- Method httpClientMethod = clientBuilderClass .getMethod ("httpClient" , HttpClient .class );
96- httpClientMethod .invoke (clientBuilder , httpClient );
97- }
137+ if (httpClient != null ) {
138+ clientBuilder .httpClient (httpClient );
139+ }
98140
99- if (policies != null ) {
100- Method addPolicyMethod = clientBuilderClass .getMethod ("addPolicy" , HttpPipelinePolicy .class );
101- for (HttpPipelinePolicy policy : policies ) {
102- addPolicyMethod .invoke (clientBuilder , policy );
103- }
141+ if (policies != null ) {
142+ for (HttpPipelinePolicy policy : policies ) {
143+ clientBuilder .addPolicy (policy );
104144 }
105- } catch (ReflectiveOperationException e ) {
106- throw new IllegalStateException (e );
107145 }
108146 }
109147 }
0 commit comments