/* * Copyright (c) 2018, Oracle America, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Oracle nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ package org.openjdk; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; import java.io.*; import java.nio.*; import java.nio.charset.*; import static java.nio.charset.StandardCharsets.UTF_8; import java.util.*; import java.util.concurrent.*; import java.util.jar.*; import java.util.stream.*; import java.util.zip.*; import java.time.*; import javax.mail.internet.MimeUtility; import javax.mail.MessagingException; @State(Scope.Benchmark) @BenchmarkMode(Mode.AverageTime) //@BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS) @Fork(2) public class CoderResultBM { private static byte[][] malformedUTF8 = new byte[][] { // malformed 1 new byte[] { 'a', 'b', 'c', (byte)0xc0, }, // malformed 2 new byte[] { 'a', 'b', 'c', (byte)0xc2, (byte)0xcc }, // malformed 3 new byte[] { 'a', 'b', 'c', (byte)0xe0, (byte)0xa0, (byte)0xcc }, new byte[] { 'a', 'b', 'c', (byte)0xf0, (byte)0x90, (byte)0x80, (byte)0xcc }, }; private static char[][] unmappableMS932 = new char[][] { new char[] { 'a', 'b', 'c', (char)0xfd }, new char[] { 'a', 'b', 'c', (char)0xe0, 0x20 }, }; private static Random r = new Random(); private static Charset MS932; @Setup public void setup() throws Throwable { MS932 = Charset.forName("MS932"); } @CompilerControl(CompilerControl.Mode.DONT_INLINE) private CoderResult decodeUTF8(Blackhole bh) { ByteBuffer bb = ByteBuffer.wrap(malformedUTF8[r.nextInt(4)]); CharBuffer cb = CharBuffer.allocate(10); CoderResult cr = null; for (int i = 0; i < 100; i++) { bb.rewind(); cb.clear(); cr = UTF_8.newDecoder().decode(bb, cb, true); bh.consume(cr); } return cr; } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(1) public CoderResult decodeUTF8_t1(Blackhole bh) { return decodeUTF8(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(5) public CoderResult decodeUTF8_t5(Blackhole bh) { return decodeUTF8(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(10) public CoderResult decodeUTF8_t10(Blackhole bh) { return decodeUTF8(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(20) public CoderResult decodeUTF8_t20(Blackhole bh) { return decodeUTF8(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(30) public CoderResult decodeUTF8_t30(Blackhole bh) { return decodeUTF8(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(50) public CoderResult decodeUTF8_t50(Blackhole bh) { return decodeUTF8(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(100) public CoderResult decodeUTF8_t100(Blackhole bh) { return decodeUTF8(bh); } @CompilerControl(CompilerControl.Mode.DONT_INLINE) private CoderResult encodeMS932(Blackhole bh) { CharBuffer cb = CharBuffer.wrap(unmappableMS932[r.nextInt(2)]); ByteBuffer bb = ByteBuffer.allocate(10); CoderResult cr = null; for (int i = 0; i < 100; i++) { bb.rewind(); cb.clear(); cr = MS932.newEncoder().encode(cb, bb, true); bh.consume(cr); } return cr; } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(1) public CoderResult encodeMS932_t1(Blackhole bh) { return encodeMS932(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(10) public CoderResult encodeMS932_t10(Blackhole bh) { return encodeMS932(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(50) public CoderResult encodeMS932_t50(Blackhole bh) { return encodeMS932(bh); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) @Threads(100) public CoderResult encodeMS932_t100(Blackhole bh) { return encodeMS932(bh); } }