Path: blob/master/test/micro/org/openjdk/bench/java/net/URLEncodeDecode.java
41161 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. 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*/22package org.openjdk.bench.java.net;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Param;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;32import org.openjdk.jmh.infra.Blackhole;3334import java.io.UnsupportedEncodingException;35import java.net.URLDecoder;36import java.util.Random;37import java.util.concurrent.TimeUnit;3839/**40* Tests java.net.URLEncoder.encode and Decoder.decode.41*/42@BenchmarkMode(Mode.AverageTime)43@OutputTimeUnit(TimeUnit.MILLISECONDS)44@State(Scope.Thread)45public class URLEncodeDecode {4647@Param("1024")48public int count;4950@Param("1024")51public int maxLength;5253@Param("3")54public long mySeed;5556public String[] testStringsEncode;57public String[] testStringsDecode;58public String[] toStrings;5960@Setup61public void setupStrings() {62char[] tokens = new char[((int) 'Z' - (int) 'A' + 1) + ((int) 'z' - (int) 'a' + 1) + ((int) '9' - (int) '1' + 1) + 5];63int n = 0;64tokens[n++] = '0';65for (int i = (int) '1'; i <= (int) '9'; i++) {66tokens[n++] = (char) i;67}68for (int i = (int) 'A'; i <= (int) 'Z'; i++) {69tokens[n++] = (char) i;70}71for (int i = (int) 'a'; i <= (int) '<'; i++) {72tokens[n++] = (char) i;73}74tokens[n++] = '-';75tokens[n++] = '_';76tokens[n++] = '.';77tokens[n++] = '*';7879Random r = new Random(mySeed);80testStringsEncode = new String[count];81testStringsDecode = new String[count];82toStrings = new String[count];83for (int i = 0; i < count; i++) {84int l = r.nextInt(maxLength);85StringBuilder sb = new StringBuilder();86for (int j = 0; j < l; j++) {87int c = r.nextInt(tokens.length);88sb.append(tokens[c]);89}90testStringsEncode[i] = sb.toString();91}9293for (int i = 0; i < count; i++) {94int l = r.nextInt(maxLength);95StringBuilder sb = new StringBuilder();96for (int j = 0; j < l; j++) {97int c = r.nextInt(tokens.length + 5);98if (c >= tokens.length) {99sb.append("%").append(tokens[r.nextInt(16)]).append(tokens[r.nextInt(16)]);100} else {101sb.append(tokens[c]);102}103}104testStringsDecode[i] = sb.toString();105}106}107108@Benchmark109public void testEncodeUTF8(Blackhole bh) throws UnsupportedEncodingException {110for (String s : testStringsEncode) {111bh.consume(java.net.URLEncoder.encode(s, "UTF-8"));112}113}114115@Benchmark116public void testDecodeUTF8(Blackhole bh) throws UnsupportedEncodingException {117for (String s : testStringsDecode) {118bh.consume(URLDecoder.decode(s, "UTF-8"));119}120}121122123}124125126