Skip to content

Instantly share code, notes, and snippets.

@normanmaurer
Created September 5, 2014 13:44
Show Gist options
  • Select an option

  • Save normanmaurer/d190c2c5eafcd80fc108 to your computer and use it in GitHub Desktop.

Select an option

Save normanmaurer/d190c2c5eafcd80fc108 to your computer and use it in GitHub Desktop.
@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