1 /* 2 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.java.net; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Param; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 import org.openjdk.jmh.infra.Blackhole; 36 37 import java.io.UnsupportedEncodingException; 38 import java.net.URLDecoder; 39 import java.util.Random; 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * Tests java.net.URLEncoder.encode and Decoder.decode. 44 */ 45 @BenchmarkMode(Mode.AverageTime) 46 @OutputTimeUnit(TimeUnit.MILLISECONDS) 47 @State(Scope.Thread) 48 public class URLEncodeDecode { 49 50 @Param("1024") 51 public int count; 52 53 @Param("1024") 54 public int maxLength; 55 56 @Param("3") 57 public long mySeed; 58 59 public String[] testStringsEncode; 60 public String[] testStringsDecode; 61 public String[] toStrings; 62 63 @Setup 64 public void setupStrings() { 65 char[] tokens = new char[((int) 'Z' - (int) 'A' + 1) + ((int) 'z' - (int) 'a' + 1) + ((int) '9' - (int) '1' + 1) + 5]; 66 int n = 0; 67 tokens[n++] = '0'; 68 for (int i = (int) '1'; i <= (int) '9'; i++) { 69 tokens[n++] = (char) i; 70 } 71 for (int i = (int) 'A'; i <= (int) 'Z'; i++) { 72 tokens[n++] = (char) i; 73 } 74 for (int i = (int) 'a'; i <= (int) '<'; i++) { 75 tokens[n++] = (char) i; 76 } 77 tokens[n++] = '-'; 78 tokens[n++] = '_'; 79 tokens[n++] = '.'; 80 tokens[n++] = '*'; 81 82 Random r = new Random(mySeed); 83 testStringsEncode = new String[count]; 84 testStringsDecode = new String[count]; 85 toStrings = new String[count]; 86 for (int i = 0; i < count; i++) { 87 int l = r.nextInt(maxLength); 88 StringBuilder sb = new StringBuilder(); 89 for (int j = 0; j < l; j++) { 90 int c = r.nextInt(tokens.length); 91 sb.append(tokens[c]); 92 } 93 testStringsEncode[i] = sb.toString(); 94 } 95 96 for (int i = 0; i < count; i++) { 97 int l = r.nextInt(maxLength); 98 StringBuilder sb = new StringBuilder(); 99 for (int j = 0; j < l; j++) { 100 int c = r.nextInt(tokens.length + 5); 101 if (c >= tokens.length) { 102 sb.append("%").append(tokens[r.nextInt(16)]).append(tokens[r.nextInt(16)]); 103 } else { 104 sb.append(tokens[c]); 105 } 106 } 107 testStringsDecode[i] = sb.toString(); 108 } 109 } 110 111 @Benchmark 112 public void testEncodeUTF8(Blackhole bh) throws UnsupportedEncodingException { 113 for (String s : testStringsEncode) { 114 bh.consume(java.net.URLEncoder.encode(s, "UTF-8")); 115 } 116 } 117 118 @Benchmark 119 public void testDecodeUTF8(Blackhole bh) throws UnsupportedEncodingException { 120 for (String s : testStringsDecode) { 121 bh.consume(URLDecoder.decode(s, "UTF-8")); 122 } 123 } 124 125 126 }