Skip to content

Commit b271fca

Browse files
authored
add okhttp. (Azure#28362)
1 parent 134ff67 commit b271fca

File tree

6 files changed

+101
-58
lines changed

6 files changed

+101
-58
lines changed

common/perf-test-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,10 @@
7676
<artifactId>azure-core-http-netty</artifactId>
7777
<version>1.11.9</version> <!-- {x-version-update;com.azure:azure-core-http-netty;dependency} -->
7878
</dependency>
79+
<dependency>
80+
<groupId>com.azure</groupId>
81+
<artifactId>azure-core-http-okhttp</artifactId>
82+
<version>1.8.0</version> <!-- {x-version-update;com.azure:azure-core-http-okhttp;dependency} -->
83+
</dependency>
7984
</dependencies>
8085
</project>

common/perf-test-core/src/main/java/com/azure/perf/test/core/ApiPerfTestBase.java

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33

44
package com.azure.perf.test.core;
55

6+
import com.azure.core.client.traits.HttpTrait;
67
import com.azure.core.http.HttpClient;
78
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;
812
import com.azure.core.http.policy.HttpPipelinePolicy;
913
import io.netty.handler.ssl.SslContext;
1014
import io.netty.handler.ssl.SslContextBuilder;
1115
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
16+
import okhttp3.OkHttpClient;
1217
import reactor.core.publisher.Flux;
1318
import reactor.core.publisher.Mono;
1419

20+
import javax.net.ssl.SSLContext;
1521
import javax.net.ssl.SSLException;
16-
import java.lang.reflect.Method;
22+
import javax.net.ssl.X509TrustManager;
1723
import java.net.URI;
24+
import java.security.KeyManagementException;
25+
import java.security.NoSuchAlgorithmException;
26+
import java.security.SecureRandom;
1827
import java.util.Arrays;
1928
import 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
}

common/perf-test-core/src/main/java/com/azure/perf/test/core/PerfStressOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class PerfStressOptions {
4646
@Parameter(names = { "-c", "--count" }, description = "Number of items")
4747
private int count = 10;
4848

49+
@Parameter(names = { "--http-client" }, description = "The http client to use. Can be netty, okhttp.")
50+
private HttpClientType httpClient = HttpClientType.NETTY;
51+
4952
/**
5053
* Get the configured count for performance test.
5154
* @return The count.
@@ -126,9 +129,21 @@ public boolean isSync() {
126129
return sync;
127130
}
128131

132+
/**
133+
* The http client to use. Can be netty, okhttp.
134+
* @return The http client to use.
135+
*/
136+
public HttpClientType getHttpClient() {
137+
return httpClient;
138+
}
139+
129140
private static class SemiColonSplitter implements IParameterSplitter {
130141
public List<String> split(String value) {
131142
return Arrays.asList(value.split(";"));
132143
}
133144
}
145+
146+
public enum HttpClientType {
147+
NETTY, OKHTTP
148+
}
134149
}

common/perf-test-core/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
module com.azure.core.test.perf {
55
requires com.azure.core;
6+
requires com.azure.core.http.okhttp;
67
requires com.azure.http.netty;
78
requires reactor.core;
89
requires org.reactivestreams;
@@ -14,4 +15,5 @@
1415
requires io.netty.handler;
1516
requires reactor.netty.core;
1617
requires io.netty.codec.http;
18+
requires okhttp3;
1719
}

sdk/core/azure-core-perf/src/main/java/com/azure/core/perf/core/CorePerfStressOptions.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ public class CorePerfStressOptions extends PerfStressOptions {
1111
@Parameter(names = { "-e", "--endpoint" }, description = "The base endpoint for rest proxy tests")
1212
private String endpoint = "http://unused";
1313

14-
@Parameter(names = { "--http-client" }, description = "The http client to use. Can be netty, okhttp. "
15-
+ "Must be specified if non-mock backend type is used otherwise is ignored")
16-
private HttpClientType httpClient = null;
17-
1814
@Parameter(names = { "--backend-type"}, description = "The backend type used for tests. "
1915
+ "Options are mock, blobs or wiremock. "
2016
+ "Defaults to mock.")
@@ -38,15 +34,6 @@ public String getEndpoint() {
3834
return endpoint;
3935
}
4036

41-
/**
42-
* The http client to use. Can be netty, okhttp.
43-
* Must be specified if non-mock backend type is used otherwise is ignored
44-
* @return The http client to use.
45-
*/
46-
public HttpClientType getHttpClient() {
47-
return httpClient;
48-
}
49-
5037
/**
5138
* The backend type used for tests. Options are mock, blobs or wiremock. Defaults to mock.
5239
* @return The backend type used for tests.
@@ -73,10 +60,6 @@ public BinaryDataSource getBinaryDataSource() {
7360
return binaryDataSource;
7461
}
7562

76-
public enum HttpClientType {
77-
NETTY, OKHTTP
78-
}
79-
8063
public enum BackendType {
8164
MOCK, BLOBS, WIREMOCK
8265
}

sdk/core/azure-core-perf/src/test/java/com/azure/core/perf/CorePerfIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.azure.core.perf.core.CorePerfStressOptions;
77
import com.azure.core.perf.core.CorePerfStressOptions.BackendType;
88
import com.azure.core.perf.core.CorePerfStressOptions.BinaryDataSource;
9-
import com.azure.core.perf.core.CorePerfStressOptions.HttpClientType;
109
import com.azure.perf.test.core.PerfStressTest;
1110
import com.beust.jcommander.JCommander;
1211
import org.junit.jupiter.params.ParameterizedTest;
@@ -21,6 +20,7 @@
2120
import java.util.function.Supplier;
2221
import java.util.stream.Stream;
2322

23+
import static com.azure.perf.test.core.PerfStressOptions.HttpClientType;
2424
import static org.junit.jupiter.api.Assertions.assertFalse;
2525
import static org.junit.jupiter.api.Assertions.assertNotNull;
2626

0 commit comments

Comments
 (0)