Skip to content

Commit bdb368a

Browse files
committed
perf: 将热点代码中的replace替换为fastReplace。非热点代码replace保持不变
1 parent 0017b25 commit bdb368a

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

src/main/java/top/meethigher/proxy/http/ReverseHttpProxy.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.*;
1515
import java.util.concurrent.ThreadLocalRandom;
1616

17+
import static top.meethigher.proxy.http.UrlParser.fastReplace;
18+
1719
/**
1820
* 基于Vertx实现的HTTP反向代理
1921
*
@@ -703,7 +705,9 @@ protected String getProxyUrl(RoutingContext ctx, HttpServerRequest serverReq, Ht
703705

704706
// 在vertx中,uri表示hostPort后面带有参数的地址。而这里的uri表示不带有参数的地址。
705707
final String uri = serverReq.path();
706-
final String params = serverReq.uri().replace(uri, "");
708+
709+
//final String params = serverReq.uri().replace(uri, "");
710+
final String params = fastReplace(serverReq.uri(), uri, "");
707711

708712

709713
// 若不是多级匹配,则直接代理到目标地址。注意要带上请求参数
@@ -715,7 +719,9 @@ protected String getProxyUrl(RoutingContext ctx, HttpServerRequest serverReq, Ht
715719
if (matchedUri.endsWith("/")) {
716720
matchedUri = matchedUri.substring(0, matchedUri.length() - 1);
717721
}
718-
String suffixUri = uri.replace(matchedUri, "");
722+
723+
//String suffixUri = uri.replace(matchedUri, "");
724+
String suffixUri = fastReplace(uri, matchedUri, "");
719725

720726
// 代理路径尾部与用户初始请求保持一致
721727
if (uri.endsWith("/") && !suffixUri.endsWith("/")) {

src/main/java/top/meethigher/proxy/http/UrlParser.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44

55
public class UrlParser {
66

7+
8+
/**
9+
* 在压测时,性能比String.replace略优
10+
*/
11+
public static String fastReplace(String text, String search, String replacement) {
12+
if (text == null || search == null || replacement == null || search.isEmpty()) {
13+
return text;
14+
}
15+
StringBuilder result = new StringBuilder();
16+
int start = 0, index;
17+
while ((index = text.indexOf(search, start)) >= 0) {
18+
result.append(text, start, index).append(replacement);
19+
start = index + search.length();
20+
}
21+
result.append(text.substring(start));
22+
return result.toString();
23+
}
24+
25+
726
/**
827
* 拼接两个uri。不要求uri以"/"结尾
928
*/
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package top.meethigher.proxy.http;
2+
3+
import org.junit.Test;
4+
5+
public class UrlParserTest {
6+
7+
8+
@Test
9+
public void fastReplaceAgg() {
10+
11+
String origin = "http://10.0.0.1:8080";
12+
String target = "https://meethigher.top";
13+
14+
// 函数进行预热
15+
int preheatNumber = 2000;
16+
17+
int total = 5000000;
18+
19+
// 预热
20+
for (int i = 0; i < 2; i++) {
21+
if (i == 0) {
22+
for (int j = 0; j < preheatNumber; j++) {
23+
String replace = origin.replace(origin, target);
24+
}
25+
} else {
26+
for (int j = 0; j < preheatNumber; j++) {
27+
String replace = UrlParser.fastReplace(origin, origin, target);
28+
}
29+
}
30+
}
31+
32+
// 实际耗时记录
33+
for (int i = 0; i < 2; i++) {
34+
long start = System.currentTimeMillis();
35+
if (i == 0) {
36+
for (int j = 0; j < total; j++) {
37+
String replace = origin.replace(origin, target);
38+
}
39+
} else {
40+
for (int j = 0; j < total; j++) {
41+
String replace = UrlParser.fastReplace(origin, origin, target);
42+
}
43+
}
44+
long end = System.currentTimeMillis();
45+
System.out.println("方案 " + i + " 耗时: " + (end - start) + " ms");
46+
}
47+
48+
}
49+
50+
51+
@Test
52+
public void fastReplace1() {
53+
54+
String origin = "http://10.0.0.1:8080";
55+
String target = "https://meethigher.top";
56+
57+
// 函数进行预热
58+
int preheatNumber = 2000;
59+
60+
int total = 5000000;
61+
for (int i = 0; i < preheatNumber; i++) {
62+
String replace = origin.replace(origin, target);
63+
}
64+
65+
long start = System.currentTimeMillis();
66+
for (int j = 0; j < total; j++) {
67+
String replace = origin.replace(origin, target);
68+
}
69+
long end = System.currentTimeMillis();
70+
System.out.println("耗时: " + (end - start) + " ms");
71+
72+
}
73+
74+
75+
@Test
76+
public void fastReplace2() {
77+
78+
String origin = "http://10.0.0.1:8080";
79+
String target = "https://meethigher.top";
80+
81+
// 函数进行预热
82+
int preheatNumber = 2000;
83+
84+
int total = 5000000;
85+
for (int i = 0; i < preheatNumber; i++) {
86+
String replace = UrlParser.fastReplace(origin, origin, target);
87+
}
88+
89+
long start = System.currentTimeMillis();
90+
for (int j = 0; j < total; j++) {
91+
String replace = UrlParser.fastReplace(origin, origin, target);
92+
}
93+
long end = System.currentTimeMillis();
94+
System.out.println("耗时: " + (end - start) + " ms");
95+
96+
}
97+
98+
99+
@Test
100+
public void fastReplace() {
101+
String str = "hello, world";
102+
System.out.println(UrlParser.fastReplace(str, ",", "!"));
103+
}
104+
}

0 commit comments

Comments
 (0)