This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new 2ae809353d Refactor to avoid issues if JVM optimises conversion to ASCII 2ae809353d is described below commit 2ae809353d9537548b52355e4d91322493eb1592 Author: Mark Thomas <ma...@apache.org> AuthorDate: Mon Nov 14 11:38:38 2022 +0000 Refactor to avoid issues if JVM optimises conversion to ASCII --- .../apache/tomcat/util/buf/TestMessageBytes.java | 79 ++++++++++++++-------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/test/org/apache/tomcat/util/buf/TestMessageBytes.java b/test/org/apache/tomcat/util/buf/TestMessageBytes.java index f2016f478d..3311996394 100644 --- a/test/org/apache/tomcat/util/buf/TestMessageBytes.java +++ b/test/org/apache/tomcat/util/buf/TestMessageBytes.java @@ -16,7 +16,10 @@ */ package org.apache.tomcat.util.buf; -import java.nio.charset.Charset; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; import org.junit.Assert; @@ -24,6 +27,26 @@ import org.junit.Test; public class TestMessageBytes { + private static final String CONVERSION_STRING = + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + + "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; + + private static final int CONVERSION_LOOPS = 1000000; + @Test public void testToStringFromNull() { MessageBytes mb = MessageBytes.newInstance(); @@ -73,8 +96,7 @@ public class TestMessageBytes { /* - * Checks the the optimized code is at least twice as fast as the - * non-optimized code. + * Checks the the optimized code is faster than the non-optimized code. */ @Test public void testConversionPerformance() { @@ -87,12 +109,12 @@ public class TestMessageBytes { * non-optimised code. Loop three times allows once to warn up the JVM * once to run the test and once more in case of unexpected CI /GC * slowness. The test will exit early if possible. + * + * MeesageBytes only optimises conversion for ISO_8859_1 */ for (int i = 0; i < 3; i++) { - optimized = doTestConversionPerformance(StandardCharsets.ISO_8859_1); - // US_ASCII chosen as the conversion is the same and it is another - // Charset available on all platforms. - nonOptimized = doTestConversionPerformance(StandardCharsets.US_ASCII); + optimized = doTestOptimisedConversionPerformance(); + nonOptimized = doTestConversionPerformance(); System.out.println(optimized + " " + nonOptimized); if (optimized * 2 < nonOptimized) { @@ -100,37 +122,36 @@ public class TestMessageBytes { } } - Assert.assertTrue("Non-optimised code was faster (" + nonOptimized + "ns) compared to optimized (" + optimized + "ns)", optimized < nonOptimized); + Assert.assertTrue("Non-optimised code was faster (" + nonOptimized + "ns) compared to optimized (" + + optimized + "ns)", optimized < nonOptimized); } - private long doTestConversionPerformance(Charset charset) { + private long doTestOptimisedConversionPerformance() { MessageBytes mb = MessageBytes.newInstance(); - int loops = 1000000; - long start = System.nanoTime(); - for (int i = 0; i < loops; i++) { + for (int i = 0; i < CONVERSION_LOOPS; i++) { mb.recycle(); - mb.setCharset(charset); - mb.setString("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" + - "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"); + mb.setCharset(StandardCharsets.ISO_8859_1); + mb.setString(CONVERSION_STRING); mb.toBytes(); } return System.nanoTime() - start; } + + + private long doTestConversionPerformance() { + long start = System.nanoTime(); + for (int i = 0; i < CONVERSION_LOOPS; i++) { + CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder().onMalformedInput( + CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); + try { + encoder.encode(CharBuffer.wrap(CONVERSION_STRING)); + } catch (CharacterCodingException cce) { + Assert.fail(); + } + } + return System.nanoTime() - start; + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org