Last active
August 21, 2019 06:33
-
-
Save hellojinjie/5651936 to your computer and use it in GitHub Desktop.
五种不同的 URL 参数解析方法的性能比较
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package conger; | |
import java.lang.reflect.Method; | |
import java.net.URL; | |
import java.net.URLDecoder; | |
import java.nio.charset.Charset; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.apache.catalina.util.RequestUtil; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.utils.URLEncodedUtils; | |
import org.eclipse.jetty.util.MultiMap; | |
import org.eclipse.jetty.util.UrlEncoded; | |
public class UrlParse { | |
private static final String url = "http://nlqosdrecv01.neulion.com/nfldrecv/ProxyBean?hasQueryString=true&msgID=247&eventType=HEARTBEAT&clientID=E79B80DE-2C57-53F9-F5A3-12EFE3041573&viewID=66119FDA-097C-240A-5F58-12EFFC154803&playTime=7452&streamType=1&streamURL=adaptive%3A%2F%2Fnlds131.neulion.com%3A443%2Fnlds_vod%2Fnfl%2Fvod%2F2013%2F01%2F06%2F55827%2F3_55827_ind_bal_2012_h_whole_1_pc.mp4&streamLength=7955&startupTime=1889&updateInterval=30000&bitrate=4500&os=Mac%20OS%2010.6.8&player=MAC%2011%2C5%2C502%2C136&browserVersion=Safari%205.1&bandwidth=4400&dropFrameCount=0&cdnName=nlds131.cdnak.neulion.com&bytesLoaded=3440846848&bytesLoadedDelta=23227392&cdnName=nlds131.cdnl3nl.neulion.com&bytesLoaded=179725312&bytesLoadedDelta=7340032&siteID=nfl&gameID=55827&streamDescription=GP%3A%201%2F6%2F2013%20IND%40BALbroadcast&productID=nflgp&homeTeam=BAL&userID=&awayTeam=IND&progType=broadcast&appType=desktop&gameDate=1%2F6%2F2013&windowMode=full%20mosaic&ipAddress=121.73.16.153"; | |
private String[] parameters; | |
public UrlParse() throws Exception { | |
URL u = new URL(url); | |
String query = u.getQuery(); | |
MultiMap<String> valuesJetty = new MultiMap<String>(); | |
UrlEncoded.decodeTo(query, valuesJetty, "UTF-8", 1000); | |
parameters = valuesJetty.keySet().toArray(new String[0]); | |
} | |
public static void main(String[] args) throws Exception { | |
UrlParse parser = new UrlParse(); | |
parser.parse(); | |
parser.compare(); | |
} | |
/** | |
* 把各个方法的解析结果打印出来,人工判断下是不是正确的。 | |
* 其中 regex 和 split 没有处理 parameter array 的情况 | |
* @throws Exception | |
*/ | |
public void parse() throws Exception { | |
URL u = new URL(url); | |
String query = u.getQuery(); | |
List<NameValuePair> valueHttpclient = URLEncodedUtils.parse(query, Charset.forName("UTF-8")); | |
System.out.println(valueHttpclient); | |
MultiMap<String> valuesJetty = new MultiMap<String>(); | |
UrlEncoded.decodeTo(query, valuesJetty, "UTF-8", 1000); | |
System.out.println(valuesJetty); | |
Map<String, String[]> valueTomcat = new HashMap<String, String[]>(); | |
RequestUtil.parseParameters(valueTomcat, query, "UTF-8"); | |
for (Map.Entry<String, String[]> entry : valueTomcat.entrySet()) { | |
System.out.print(entry.getKey() + ":" + entry.getValue()[0] + ";"); | |
} | |
System.out.println(valueTomcat); | |
for (String s : parameters) { | |
Pattern p = Pattern.compile(s + "=([^&]*)(&|$)"); | |
Matcher m = p.matcher(URLDecoder.decode(url, "UTF-8")); | |
if (m.find()) { | |
System.out.print(s + ":" + m.group(1) + ";"); | |
} | |
} | |
System.out.println(); | |
String u1 = URLDecoder.decode(url, "UTF-8"); | |
for (String s : parameters) { | |
String[] a = new String[100]; | |
if (u1.indexOf(s) != -1) { | |
a = (u1.substring(u1.indexOf(s))).split("&"); | |
String[] b = a[0].split("="); | |
if (b.length == 2) { | |
System.out.print(s + ":" + b[1] + ";"); | |
} | |
} | |
} | |
System.out.println(); | |
} | |
public void compare() throws Exception { | |
System.out.println("---first---"); | |
this.runTest(); | |
System.out.println("---second---"); | |
this.runTest(); | |
System.out.println("---third---"); | |
this.runTest(); | |
System.out.println("---fouth---"); | |
this.runTest(); | |
} | |
public void runTest() throws Exception { | |
this.runMethod("httpclient"); | |
this.runMethod("jettyUtil"); | |
this.runMethod("tomcat"); | |
this.runMethod("regex"); | |
this.runMethod("split"); | |
} | |
public void runMethod(String method) throws Exception { | |
long start = System.currentTimeMillis(); | |
Method m = this.getClass().getDeclaredMethod(method, (Class<?>[])null); | |
for (int i = 0; i < 100000; i++) { | |
m.invoke(this, (Object[])null); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println(method + ": " + (end - start)); | |
} | |
public void httpclient() throws Exception { | |
URL u = new URL(url); | |
String query = u.getQuery(); | |
URLEncodedUtils.parse(query, Charset.forName("UTF-8")); | |
} | |
public void jettyUtil() throws Exception { | |
URL u = new URL(url); | |
String query = u.getQuery(); | |
MultiMap<String> values = new MultiMap<String>(); | |
UrlEncoded.decodeTo(query, values, "UTF-8", 1000); | |
} | |
public void tomcat() throws Exception { | |
URL u = new URL(url); | |
String query = u.getQuery(); | |
Map<String, String> values = new HashMap<String, String>(); | |
RequestUtil.parseParameters(values, query, "UTF-8"); | |
} | |
public void regex() throws Exception { | |
String u = URLDecoder.decode(url, "UTF-8"); | |
for (String s : parameters) { | |
Pattern p = Pattern.compile(s + "=([^&]*)(&|$)"); | |
Matcher m = p.matcher(u); | |
if (m.find()) { | |
m.group(1); | |
} | |
} | |
} | |
public void split() throws Exception { | |
String u = URLDecoder.decode(url, "UTF-8"); | |
for (String s : parameters) { | |
String[] a = new String[100]; | |
if (u.indexOf(s) != -1) { | |
a = (u.substring(u.indexOf(s))).split("&"); | |
a[0].split("="); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment