Last active
November 18, 2016 22:26
-
-
Save ssp/2aaffecd71ead03b4a722797ba07fa63 to your computer and use it in GitHub Desktop.
Dauert das Parsen von URIs lange?
This file contains hidden or 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 net.earthlingsoft.uritest; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class ParseURIs { | |
public static void main(String[] args) { | |
validateUris(); | |
measureValidationSpeed(false); | |
measureValidationSpeed(true); | |
} | |
private static void validateUris() { | |
Stream.of( | |
"lollo", | |
"http://lollo.de", | |
"https://lollo.de", | |
"http://lollo.dä/", | |
"http://lollo.net/gägä", | |
"http://lollo.net/gä gä", | |
"http://lollo.net/gä%20gä", | |
"http://lollo.net/gä%20gä?lollo", | |
"http://lollo.net/gä%20gä?lol lo", | |
"http://lollo.net/gä%20gä?lol=lo", | |
"urn:nbn:de:gbv:089-2683311469", | |
"urn:nbn:de:gbv:089-3321752945/fragment/page=40/highlight=220,620,550,820/zoom=150", | |
"hdl:4263537/4086", | |
"ark:/13030/tf5p30086k" | |
) | |
.forEach(s -> parseUri(s, true)); | |
} | |
private static void measureValidationSpeed(final boolean broken) { | |
final int numberOfUris = 1_000_000; | |
final String brokenString = broken ? " " : ""; | |
final List<String> uris = IntStream.range(0, numberOfUris) | |
.mapToObj(i -> "http://lollo.de/" + i + "/lol" + brokenString + "lo") | |
.collect(Collectors.toList()); | |
final long startTime = System.currentTimeMillis(); | |
uris.forEach(s -> parseUri(s, false)); | |
final long finishTime = System.currentTimeMillis(); | |
System.out.println(String.format( | |
"\n %s ms - %s URIs. broken: %s", | |
String.valueOf(finishTime - startTime), | |
numberOfUris, | |
broken)); | |
// ~ 3 seconds for 1_000_000 iterations. | |
} | |
private static URI parseUri(final String uriString, final boolean log) { | |
try { | |
final URI uri = new URI(uriString); | |
if (log) { | |
System.out.println(uri + ": OK"); | |
} | |
return uri; | |
} catch (URISyntaxException e) { | |
if (log) { | |
System.out.println(uriString + ": XXXX (" + e.getMessage() + ")"); | |
} | |
} | |
return null; | |
} | |
} |
This file contains hidden or 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
lollo: OK | |
http://lollo.de: OK | |
https://lollo.de: OK | |
http://lollo.dä/: OK | |
http://lollo.net/gägä: OK | |
http://lollo.net/gä gä: XXXX (Illegal character in path at index 19: http://lollo.net/gä gä) | |
http://lollo.net/gä%20gä: OK | |
http://lollo.net/gä%20gä?lollo: OK | |
http://lollo.net/gä%20gä?lol lo: XXXX (Illegal character in query at index 28: http://lollo.net/gä%20gä?lol lo) | |
http://lollo.net/gä%20gä?lol=lo: OK | |
urn:nbn:de:gbv:089-2683311469: OK | |
urn:nbn:de:gbv:089-3321752945/fragment/page=40/highlight=220,620,550,820/zoom=150: OK | |
hdl:4263537/4086: OK | |
ark:/13030/tf5p30086k: OK | |
1157 ms - 1000000 URIs. broken: false | |
2243 ms - 1000000 URIs. broken: true | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment