Created
September 5, 2014 13:44
-
-
Save normanmaurer/d190c2c5eafcd80fc108 to your computer and use it in GitHub Desktop.
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
| @State(Scope.Benchmark) | |
| @Warmup(iterations = 10) | |
| @Measurement(iterations = 25) | |
| public class AbstractByteBufBenchmark extends AbstractMicrobenchmark { | |
| private ByteBuf asciiBuffer; | |
| private ByteBuf utf8Buffer; | |
| @Setup | |
| public void setup() { | |
| asciiBuffer = Unpooled.directBuffer(512); | |
| StringBuilder asciiSequence = new StringBuilder(128); | |
| for (int i = 0; i < 128; i++) { | |
| asciiSequence.append('a'); | |
| } | |
| asciiBuffer.writeBytes(asciiSequence.toString().getBytes(CharsetUtil.US_ASCII)); | |
| // Generate some mixed UTF-8 String for benchmark | |
| utf8Buffer = Unpooled.directBuffer(512); | |
| StringBuilder utf8Sequence = new StringBuilder(128); | |
| char[] chars = "Some UTF-8 like äÄ∏ŒŒ".toCharArray(); | |
| for (int i = 0; i < 128; i++) { | |
| utf8Sequence.append(chars[i % chars.length]); | |
| } | |
| utf8Buffer.writeBytes(utf8Sequence.toString().getBytes(CharsetUtil.UTF_8)); | |
| } | |
| @TearDown | |
| public void teardown() { | |
| asciiBuffer.release(); | |
| utf8Buffer.release(); | |
| } | |
| @Benchmark | |
| public String toStringUS_ASCII() { | |
| return asciiBuffer.toString(CharsetUtil.US_ASCII); | |
| } | |
| @Benchmark | |
| public String toStringUTF_8() { | |
| return utf8Buffer.toString(CharsetUtil.UTF_8); | |
| } | |
| @Benchmark | |
| public String toStringUTF_8_US_ASCII_DATA() { | |
| return asciiBuffer.toString(CharsetUtil.UTF_8); | |
| } | |
| @Benchmark | |
| public String toStringOldUS_ASCII() { | |
| return ByteBufUtil0.toString(asciiBuffer, CharsetUtil.US_ASCII); | |
| } | |
| @Benchmark | |
| public String toStringOldUTF_8() { | |
| return ByteBufUtil0.toString(utf8Buffer, CharsetUtil.UTF_8); | |
| } | |
| @Benchmark | |
| public String toStringOldUTF_8_US_ASCII_DATA() { | |
| return ByteBufUtil0.toString(asciiBuffer, CharsetUtil.UTF_8); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment