Path: blob/master/test/micro/org/openjdk/bench/java/util/Base64Encode.java
41161 views
/*1* Copyright (c) 2020, 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 Base64Encode {3738private Base64.Encoder encoder;39private ArrayList<byte[]> unencoded;40private byte[] encoded;4142private static final int TESTSIZE = 1000;4344@Param({"1", "2", "3", "6", "7", "9", "10", "48", "512", "1000", "20000"})45private int maxNumBytes;4647@Setup48public void setup() {49Random r = new Random(1123);5051int dstLen = ((maxNumBytes + 16) / 3) * 4;5253encoder = Base64.getEncoder();54unencoded = new ArrayList<byte[]> ();55encoded = new byte[dstLen];5657for (int i = 0; i < TESTSIZE; i++) {58int srcLen = 1 + r.nextInt(maxNumBytes);59byte[] src = new byte[srcLen];60r.nextBytes(src);61unencoded.add(src);62}63}6465@Benchmark66@OperationsPerInvocation(TESTSIZE)67public void testBase64Encode(Blackhole bh) {68for (byte[] s : unencoded) {69encoder.encode(s, encoded);70bh.consume(encoded);71}72}73}747576