Path: blob/master/test/micro/org/openjdk/bench/java/util/Base64Decode.java
41161 views
/*1* Copyright (c) 2021, Huawei Technologies Co., Ltd. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package org.openjdk.micro.bench.java.util;2425import org.openjdk.jmh.annotations.*;26import org.openjdk.jmh.infra.Blackhole;2728import java.util.Base64;29import java.util.Random;30import java.util.ArrayList;31import java.util.concurrent.TimeUnit;3233@BenchmarkMode(Mode.AverageTime)34@OutputTimeUnit(TimeUnit.NANOSECONDS)35@State(Scope.Thread)36public class Base64Decode {3738private Base64.Encoder encoder, mimeEncoder;39private Base64.Decoder decoder, mimeDecoder;40private ArrayList<byte[]> encoded, mimeEncoded, errorEncoded;41private byte[] decoded, mimeDecoded, errorDecoded;4243private static final int TESTSIZE = 1000;4445@Param({"1", "3", "7", "32", "64", "80", "96",46"112", "512", "1000", "20000", "50000"})47private int maxNumBytes;4849@Param({"4"})50private int lineSize;5152private byte[] lineSeparator = {'\r', '\n'};5354/* Other value can be tested by passing parameters to the JMH55tests: -p errorIndex=3,64,144,208,272,1000,20000. */56@Param({"144"})57private int errorIndex;5859@Setup60public void setup() {61Random r = new Random(1123);6263decoded = new byte[maxNumBytes + 1];64encoder = Base64.getEncoder();65decoder = Base64.getDecoder();66encoded = new ArrayList<byte[]> ();6768mimeDecoded = new byte[maxNumBytes + 1];69mimeEncoder = Base64.getMimeEncoder(lineSize, lineSeparator);70mimeDecoder = Base64.getMimeDecoder();71mimeEncoded = new ArrayList<byte[]> ();7273errorDecoded = new byte[errorIndex + 100];74errorEncoded = new ArrayList<byte[]> ();7576for (int i = 0; i < TESTSIZE; i++) {77int srcLen = 1 + r.nextInt(maxNumBytes);78byte[] src = new byte[srcLen];79byte[] dst = new byte[((srcLen + 2) / 3) * 4];80r.nextBytes(src);81encoder.encode(src, dst);82encoded.add(dst);8384int mimeSrcLen = 1 + r.nextInt(maxNumBytes);85byte[] mimeSrc = new byte[mimeSrcLen];86byte[] mimeDst = new byte[((mimeSrcLen + 2) / 3) * 4 * (lineSize + lineSeparator.length) / lineSize];87r.nextBytes(mimeSrc);88mimeEncoder.encode(mimeSrc, mimeDst);89mimeEncoded.add(mimeDst);9091int errorSrcLen = errorIndex + r.nextInt(100);92byte[] errorSrc = new byte[errorSrcLen];93byte[] errorDst = new byte[(errorSrcLen + 2) / 3 * 4];94r.nextBytes(errorSrc);95encoder.encode(errorSrc, errorDst);96errorEncoded.add(errorDst);97errorDst[errorIndex] = (byte) '?';98}99}100101@Benchmark102@OperationsPerInvocation(TESTSIZE)103public void testBase64Decode(Blackhole bh) {104for (byte[] s : encoded) {105decoder.decode(s, decoded);106bh.consume(decoded);107}108}109110@Benchmark111@OperationsPerInvocation(TESTSIZE)112public void testBase64MIMEDecode(Blackhole bh) {113for (byte[] s : mimeEncoded) {114mimeDecoder.decode(s, mimeDecoded);115bh.consume(mimeDecoded);116}117}118119@Benchmark120@OperationsPerInvocation(TESTSIZE)121public void testBase64WithErrorInputsDecode (Blackhole bh) {122for (byte[] s : errorEncoded) {123try {124decoder.decode(s, errorDecoded);125bh.consume(errorDecoded);126} catch (IllegalArgumentException e) {127bh.consume(e);128}129}130}131}132133134