3
3
4
4
package com .azure .perf .test .core ;
5
5
6
+ import com .azure .core .client .traits .HttpTrait ;
6
7
import com .azure .core .http .HttpClient ;
7
8
import 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 ;
8
12
import com .azure .core .http .policy .HttpPipelinePolicy ;
9
13
import io .netty .handler .ssl .SslContext ;
10
14
import io .netty .handler .ssl .SslContextBuilder ;
11
15
import io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
16
+ import okhttp3 .OkHttpClient ;
12
17
import reactor .core .publisher .Flux ;
13
18
import reactor .core .publisher .Mono ;
14
19
20
+ import javax .net .ssl .SSLContext ;
15
21
import javax .net .ssl .SSLException ;
16
- import java . lang . reflect . Method ;
22
+ import javax . net . ssl . X509TrustManager ;
17
23
import java .net .URI ;
24
+ import java .security .KeyManagementException ;
25
+ import java .security .NoSuchAlgorithmException ;
26
+ import java .security .SecureRandom ;
18
27
import java .util .Arrays ;
19
28
import java .util .concurrent .CompletableFuture ;
20
29
@@ -42,33 +51,11 @@ public abstract class ApiPerfTestBase<TOptions extends PerfStressOptions> extend
42
51
*/
43
52
public ApiPerfTestBase (TOptions options ) {
44
53
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
- }
54
54
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 );
63
56
64
57
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 );
72
59
testProxy = options .getTestProxies ().get (parallelIndex % options .getTestProxies ().size ());
73
60
testProxyPolicy = new TestProxyPolicy (testProxy );
74
61
policies = Arrays .asList (testProxyPolicy );
@@ -80,30 +67,81 @@ public ApiPerfTestBase(TOptions options) {
80
67
}
81
68
}
82
69
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
+
83
129
/**
84
130
* Attempts to configure a ClientBuilder using reflection. If a ClientBuilder does not follow the standard convention,
85
131
* it can be configured manually using the "httpClient" and "policies" fields.
86
132
* @param clientBuilder The client builder.
87
133
* @throws IllegalStateException If reflective access to get httpClient or addPolicy methods fail.
88
134
*/
89
- protected void configureClientBuilder (Object clientBuilder ) {
135
+ protected void configureClientBuilder (HttpTrait <?> clientBuilder ) {
90
136
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
+ }
98
140
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 );
104
144
}
105
- } catch (ReflectiveOperationException e ) {
106
- throw new IllegalStateException (e );
107
145
}
108
146
}
109
147
}
0 commit comments