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