Author: markt Date: Thu Oct 20 08:46:40 2016 New Revision: 1765774 URL: http://svn.apache.org/viewvc?rev=1765774&view=rev Log: Add test that confirms using an Exception is faster for valid values than using a bounds check. As expected, the bounds check is faster for invalid values but for the HTTP parser, performance for valid values is more important.
Modified: tomcat/trunk/test/org/apache/tomcat/util/http/parser/TesterParserPerformance.java Modified: tomcat/trunk/test/org/apache/tomcat/util/http/parser/TesterParserPerformance.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/http/parser/TesterParserPerformance.java?rev=1765774&r1=1765773&r2=1765774&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/http/parser/TesterParserPerformance.java (original) +++ tomcat/trunk/test/org/apache/tomcat/util/http/parser/TesterParserPerformance.java Thu Oct 20 08:46:40 2016 @@ -51,6 +51,41 @@ public class TesterParserPerformance { return System.nanoTime() - start; } + + @Test + public void testExceptionVsBoundsCheck() { + Lookup boundsCheck = new BooleanArrayLookupBoundsCheck(); + Lookup exceptionCheck = new BooleanArrayLookupExceptionCheck(); + + int count = 10000; + int loops = 5; + + // Warm up + doLookupTestCheck(boundsCheck, count, 0, 127); + doLookupTestCheck(exceptionCheck, count, 0, 127); + doLookupTestCheck(boundsCheck, count, 128, 255); + doLookupTestCheck(exceptionCheck, count, 128, 255); + + for (int i = 0; i < loops; i++) { + System.out.println("Bounds:Valid : " + doLookupTestCheck(boundsCheck, count, 0, 127) + "ns"); + System.out.println("ExceptionValid : " + doLookupTestCheck(exceptionCheck, count, 0, 127) + "ns"); + System.out.println("Bounds:Invalid : " + doLookupTestCheck(boundsCheck, count, 128, 255) + "ns"); + System.out.println("ExceptionInvalid : " + doLookupTestCheck(exceptionCheck, count, 128, 255) + "ns"); + } + } + + + private long doLookupTestCheck(Lookup lookup, int iterations, int testStart, int testEnd) { + long start = System.nanoTime(); + for (int i = 0; i < iterations; i++) { + for (int j = testStart; j < testEnd; j++) { + lookup.doLookup(j); + } + } + return System.nanoTime() - start; + } + + private interface Lookup { boolean doLookup(int i); } @@ -76,4 +111,33 @@ public class TesterParserPerformance { return values[i]; } } + + + private static class BooleanArrayLookupBoundsCheck implements Lookup { + + private boolean[] values = new boolean[128]; + + @Override + public boolean doLookup(int i) { + if (i < 0 || i > 127) { + return false; + } + return values[i]; + } + } + + + private static class BooleanArrayLookupExceptionCheck implements Lookup { + + private boolean[] values = new boolean[128]; + + @Override + public boolean doLookup(int i) { + try { + return values[i]; + } catch (ArrayIndexOutOfBoundsException aioe) { + return false; + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org