Skip to content

Instantly share code, notes, and snippets.

@Anebrithien
Created November 14, 2019 02:27
Show Gist options
  • Save Anebrithien/83765ea138bebd7e98541304a67216bc to your computer and use it in GitHub Desktop.
Save Anebrithien/83765ea138bebd7e98541304a67216bc to your computer and use it in GitHub Desktop.
Benchmark between java.util.regex and org.springframework.web.util.pattern.PathPattern
import java.util.ArrayList;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import org.springframework.http.server.PathContainer;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
public class PatternBenchmarkTest {
private String full = "GET:/appCode/v001/xxData/{lon}/{lat}/point";
private PathPattern pathPattern = new PathPatternParser().parse(full);
private Pattern javaPattern = Pattern.compile("GET:/appCode/v001/xxData/([^/#]+)/([^/#]+)/point");
@Test
public void testGo() {
String s1 = "GET:/appCode/v001/xxData/{lon}/{lat}/point";
String s2 = "GET:/appCode/v001/xxData/119.23/39.78/point";
String s3 = "GET:/appCode/v002/xxData/119.23/39.78/point";
String s4 = "GET:/appCode/v001/xxData/119.23/aa/39.78/point";
String[] pool = {s1, s2, s3, s4};
//region configs
long elementSize = 100_0000;
int runCount = 100;
//endregion
AtomicInteger idx = new AtomicInteger(0);
List<String> list = Stream.generate(() -> {
idx.compareAndSet(pool.length, 0);
return pool[idx.getAndAdd(1)];
})
.limit(elementSize)
.collect(Collectors.toList());
List<Long> costOfPathPattern = new ArrayList<>();
List<Long> costOfJavaPattern = new ArrayList<>();
for (int i = 0; i < runCount; i++) {
int seqNo = (i + 1);
System.out.println("run seq: " + seqNo);
long start1 = System.currentTimeMillis();
list.parallelStream().forEach(this::matchByPathPattern);
long end1 = System.currentTimeMillis();
long start2 = System.currentTimeMillis();
list.parallelStream().forEach(this::matchByJavaPattern);
long end2 = System.currentTimeMillis();
costOfPathPattern.add(end1 - start1);
costOfJavaPattern.add(end2 - start2);
}
LongSummaryStatistics statsOfPathPattern = costOfPathPattern.stream()
.mapToLong(Long::longValue)
.summaryStatistics();
LongSummaryStatistics statsOfJavaPattern = costOfJavaPattern.stream()
.mapToLong(Long::longValue)
.summaryStatistics();
System.out.println("statsOfPathPattern = " + statsOfPathPattern);
System.out.println("statsOfJavaPattern = " + statsOfJavaPattern);
}
private boolean matchByPathPattern(String input) {
PathContainer container = PathContainer.parsePath(input);
return pathPattern.matches(container);
}
private boolean matchByJavaPattern(String input) {
Matcher m = javaPattern.matcher(input);
return m.matches();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment