Skip to content

Commit d1f0d8a

Browse files
committed
test: 添加dns解析慢的单元测试
1 parent 47757a7 commit d1f0d8a

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package top.meethigher.proxy.http;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.VertxOptions;
5+
import io.vertx.core.dns.DnsClient;
6+
import io.vertx.core.dns.DnsClientOptions;
7+
8+
import java.net.InetAddress;
9+
import java.util.concurrent.CountDownLatch;
10+
import java.util.concurrent.atomic.AtomicLong;
11+
12+
public class DnsResolverTest {
13+
14+
public static void main(String[] args) throws Exception {
15+
String hostname = "reqres.in";
16+
int iterations = 10;
17+
18+
System.out.println("Testing with InetAddress...");
19+
// 当前没有该host缓存时,会直接sun.net.spi.nameservice.NameService.lookupAllHostAddr
20+
testWithInetAddress(hostname, iterations);
21+
22+
System.out.println("Testing with Vert.x DnsClient...");
23+
// io.vertx.core.dns.impl.DnsClientImpl.Query.run
24+
testWithVertx(hostname, iterations);
25+
26+
27+
}
28+
29+
private static void testWithVertx(String hostname, int iterations) throws InterruptedException {
30+
Vertx vertx = Vertx.vertx(new VertxOptions()
31+
// .setAddressResolverOptions(new AddressResolverOptions()
32+
// .setCacheMinTimeToLive(60) // DNS 缓存时间,减少重复解析
33+
// .setCacheMaxTimeToLive(600)
34+
// .setQueryTimeout(5000))
35+
36+
);
37+
38+
DnsClient dnsClient = vertx.createDnsClient(
39+
new DnsClientOptions()
40+
.setQueryTimeout(10000)
41+
);
42+
43+
CountDownLatch latch = new CountDownLatch(iterations);
44+
AtomicLong totalTime = new AtomicLong();
45+
46+
for (int i = 0; i < iterations; i++) {
47+
long start = System.nanoTime();
48+
dnsClient.lookup(hostname, result -> {
49+
long duration = System.nanoTime() - start;
50+
if (result.succeeded()) {
51+
System.out.println("Vert.x resolved: " + result.result() + " in " + duration / 1_000_000.0 + " ms");
52+
} else {
53+
System.err.println("Vert.x failed to resolve: " + result.cause());
54+
}
55+
totalTime.addAndGet(duration);
56+
latch.countDown();
57+
});
58+
}
59+
60+
latch.await();
61+
System.out.println("Vert.x Average Time: " + (totalTime.get() / iterations) / 1_000_000.0 + " ms");
62+
vertx.close();
63+
}
64+
65+
private static void testWithInetAddress(String hostname, int iterations) throws Exception {
66+
long totalTime = 0;
67+
68+
for (int i = 0; i < iterations; i++) {
69+
long start = System.nanoTime();
70+
InetAddress address = InetAddress.getByName(hostname);
71+
long duration = System.nanoTime() - start;
72+
System.out.println("InetAddress resolved: " + address.getHostAddress() + " in " + duration / 1_000_000.0 + " ms");
73+
totalTime += duration;
74+
}
75+
76+
System.out.println("InetAddress Average Time: " + (totalTime / iterations) / 1_000_000.0 + " ms");
77+
}
78+
}

src/test/java/top/meethigher/proxy/http/ReverseHttpProxyTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11+
import java.util.Arrays;
12+
import java.util.Collections;
1113
import java.util.LinkedHashMap;
1214
import java.util.Map;
1315
import java.util.concurrent.ExecutionException;
@@ -210,4 +212,43 @@ private void proxyUrlCommon(Map<String, String> cases) throws InterruptedExcepti
210212
Assert.assertTrue(actual);
211213
}
212214

215+
216+
/**
217+
* 代理服务开启h2c(HTTP/2 cleartext)
218+
* 后端服务也支持http2
219+
*/
220+
@Test
221+
public void testServerHttp2Proxy() throws Exception {
222+
Router router = Router.router(vertx);
223+
HttpClientOptions httpClientOptions = new HttpClientOptions()
224+
.setTrustAll(true)
225+
.setVerifyHost(false)
226+
// httpclient支持与后端服务进行协商使用使用http2
227+
.setUseAlpn(true)
228+
.setProtocolVersion(HttpVersion.HTTP_2);
229+
HttpServerOptions httpServerOptions = new HttpServerOptions()
230+
// 服务端支持与客户端进行协商,使用h2c(HTTP/2 cleartext)
231+
// 常规情况下,h2只在开启了tls使用。如果不开启tls,需要指定使用的是h2c
232+
.setAlpnVersions(Collections.unmodifiableList(Arrays.asList(HttpVersion.HTTP_1_1, HttpVersion.HTTP_2)))
233+
.setUseAlpn(true)
234+
.setHttp2ClearTextEnabled(true);
235+
236+
HttpServer httpServer = vertx.createHttpServer(httpServerOptions);
237+
HttpClient httpClient = vertx.createHttpClient(httpClientOptions);
238+
239+
ReverseHttpProxy.create(router, httpServer, httpClient)
240+
.addRoute(new ProxyRoute()
241+
.setSourceUrl("/*")
242+
.setTargetUrl("https://reqres.in"))
243+
.port(8080)
244+
.start();
245+
246+
LockSupport.park();
247+
}
248+
249+
@Test
250+
public void testVertxDnsResolve() {
251+
// 比较okhttp与vertx http的dns解析。发现vertx特别慢。因此定位问题
252+
// 复现代码参考https://github.com/meethigher/bug-test/tree/vertx-http-dns
253+
}
213254
}

src/test/java/top/meethigher/proxy/tcp/ReverseTcpProxyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ReverseTcpProxyTest {
99

1010
@Test
1111
public void testVertxTCPReverseProxy() throws Exception {
12-
ReverseTcpProxy proxy = ReverseTcpProxy.create(Vertx.vertx(), "10.0.0.1", 888);
12+
ReverseTcpProxy proxy = ReverseTcpProxy.create(Vertx.vertx(), "meethigher.top", 443);
1313
proxy.port(8080).start();
1414
TimeUnit.MINUTES.sleep(10);
1515
proxy.stop();

src/test/resources/logback-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</appender>
77

88
<!-- 设置根日志级别为TRACE -->
9-
<root level="INFO">
9+
<root level="TRACE">
1010
<appender-ref ref="CONSOLE"/>
1111
</root>
1212

0 commit comments

Comments
 (0)