@@ -0,0 +1,2768 @@
+/*-------------------------------------------------------------------------%0A*%0A* Copyright (c) 2004-2014, PostgreSQL Global Development Group%0A* Copyright (c) 2004, Open Cloud Limited.%0A*%0A*%0A*-------------------------------------------------------------------------%0A*/%0Apackage org.postgresql.benchmark.encoding;%0A%0Aimport org.openjdk.jmh.annotations.*;%0Aimport org.openjdk.jmh.runner.Runner;%0Aimport org.openjdk.jmh.runner.RunnerException;%0Aimport org.openjdk.jmh.runner.options.Options;%0Aimport org.openjdk.jmh.runner.options.OptionsBuilder;%0Aimport org.postgresql.core.Utils;%0A%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.CharBuffer;%0Aimport java.nio.charset.CharacterCodingException;%0Aimport java.nio.charset.Charset;%0Aimport java.nio.charset.CharsetEncoder;%0Aimport java.util.concurrent.TimeUnit;%0A%0A/**%0A * Tests the performance of UTF-8 encoding.%0A * UTF-8 is used a lot, so we need to know the performance%0A */%0A@Fork(value = 1, jvmArgsPrepend = %22-Xmx128m%22)%0A@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)%0A@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)%0A@State(Scope.Thread)%0A@Threads(1)%0A@BenchmarkMode(Mode.AverageTime)%0A@OutputTimeUnit(TimeUnit.NANOSECONDS)%0Apublic class UTF8Encoding %7B%0A%0A    @Param(%7B%221%22, %225%22, %2210%22, %2250%22, %22100%22%7D)%0A    public int length;%0A%0A    private String source;%0A    private CharsetEncoder encoder;%0A    private ByteBuffer buf;%0A    private static final Charset UTF_8 = Charset.forName(%22UTF-8%22);%0A%0A    @Setup%0A    public void setup() %7B%0A        StringBuilder sb = new StringBuilder();%0A        for (int i = 0; i %3C length; i++) %7B%0A            sb.append(%22Hello %D0%BC%D0%B8%D1%80,%22);%0A        %7D%0A        source = sb.toString();%0A        encoder = UTF_8.newEncoder();%0A        buf = ByteBuffer.allocate(10240);%0A    %7D%0A%0A    @Benchmark%0A    public byte%5B%5D utilsEncodeUTF8_old() %7B%0A        ByteBuffer buf = UTF_8.encode(CharBuffer.wrap(source));%0A        byte%5B%5D b = new byte%5Bbuf.limit()%5D;%0A        buf.get(b, 0, buf.limit());%0A        return b;%0A    %7D%0A%0A    @Benchmark%0A    public byte%5B%5D utilsEncodeUTF8_current() %7B%0A        return Utils.encodeUTF8(source);%0A    %7D%0A%0A    @Benchmark%0A    public byte%5B%5D string_getBytes() %7B%0A        return source.getBytes(UTF_8);%0A    %7D%0A%0A    @Benchmark%0A    public ByteBuffer charset_encode() %7B%0A        return UTF_8.encode(source);%0A    %7D%0A%0A    @Benchmark%0A    public Object encoder_byteBufferReuse() throws CharacterCodingException %7B%0A        buf.clear();%0A        return encoder.encode(CharBuffer.wrap(source), buf, true);%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws RunnerException %7B%0A        Options opt = new OptionsBuilder()%0A                .include(UTF8Encoding.class.getSimpleName())%0A//                .addProfiler(GCProfiler.class)%0A                .detectJvmArgs()%0A                .build();%0A%0A        new Runner(opt).run();%0A    %7D%0A%7D%0A

add test for utf8-encoder performance