Path: blob/master/test/micro/org/openjdk/bench/java/util/Base64VarLenDecode.java
41161 views
/*1* Copyright (c) 2020, Oracle America, Inc.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions are met:6*7* * Redistributions of source code must retain the above copyright notice,8* this list of conditions and the following disclaimer.9*10* * Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* * Neither the name of Oracle nor the names of its contributors may be used15* to endorse or promote products derived from this software without16* specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE22* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF28* THE POSSIBILITY OF SUCH DAMAGE.29*/3031package org.openjdk.micro.bench.java.util;3233import org.openjdk.jmh.annotations.*;34import java.util.*;3536public class Base64VarLenDecode {3738@State(Scope.Thread)39public static class MyState {4041@Setup(Level.Trial)42public void doSetupTrial() {43ran = new Random(10101); // fixed seed for repeatability44encoder = Base64.getEncoder();45decoder = Base64.getDecoder();46System.out.println("Do Trial Setup");47}4849@Setup(Level.Invocation)50public void doSetupInvocation() {51bin_src_len = 8 + ran.nextInt(20000);52base64_len = ((bin_src_len + 2) / 3) * 4;53unencoded = new byte[bin_src_len];54encoded = new byte[base64_len];55decoded = new byte[bin_src_len];56ran.nextBytes(unencoded);57encoder.encode(unencoded, encoded);58}5960@TearDown(Level.Invocation)61public void doTearDownInvocation() {62// This isn't really a teardown. It's a check for correct functionality.63// Each iteration should produce a correctly decoded buffer that's equal64// to the unencoded data.65if (!Arrays.equals(unencoded, decoded)) {66System.out.println("Original data and decoded data are not equal!");67for (int j = 0; j < unencoded.length; j++) {68if (unencoded[j] != decoded[j]) {69System.out.format("%06x: %02x %02x\n", j, unencoded[j], decoded[j]);70}71}72System.exit(1);73}74}7576public Random ran;77public Base64.Encoder encoder;78public Base64.Decoder decoder;79public int bin_src_len;80public int base64_len;81public byte[] unencoded;82public byte[] encoded;83public byte[] decoded;84}8586@Benchmark87public void decodeMethod(MyState state) {88state.decoder.decode(state.encoded, state.decoded);89}90}919293