Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package benchmarks.regression;
     18 
     19 import java.nio.charset.Charset;
     20 import com.google.caliper.Param;
     21 import com.google.caliper.SimpleBenchmark;
     22 
     23 public class CharsetBenchmark extends SimpleBenchmark {
     24     @Param({ "1", "10", "100", "1000", "10000" })
     25     private int length;
     26 
     27     // canonical    => canonical charset name
     28     // built-in     => guaranteed-present charset
     29     // special-case => libcore treats this charset specially for performance
     30     @Param({
     31         "UTF-16",     //     canonical,     built-in, non-special-case
     32         "UTF-8",      //     canonical,     built-in,     special-case
     33         "UTF8",       // non-canonical,     built-in,     special-case
     34         "ISO-8859-1", //     canonical,     built-in,     special-case
     35         "8859_1",     // non-canonical,     built-in,     special-case
     36         "ISO-8859-2", //     canonical, non-built-in, non-special-case
     37         "8859_2",     // non-canonical, non-built-in, non-special-case
     38         "US-ASCII",   //     canonical,     built-in,     special-case
     39         "ASCII"       // non-canonical,     built-in,     special-case
     40     })
     41     private String name;
     42 
     43     public void time_new_String_BString(int reps) throws Exception {
     44         byte[] bytes = makeBytes(makeString(length));
     45         for (int i = 0; i < reps; ++i) {
     46             new String(bytes, name);
     47         }
     48     }
     49 
     50     public void time_new_String_BII(int reps) throws Exception {
     51       byte[] bytes = makeBytes(makeString(length));
     52       for (int i = 0; i < reps; ++i) {
     53         new String(bytes, 0, bytes.length);
     54       }
     55     }
     56 
     57     public void time_new_String_BIIString(int reps) throws Exception {
     58       byte[] bytes = makeBytes(makeString(length));
     59       for (int i = 0; i < reps; ++i) {
     60         new String(bytes, 0, bytes.length, name);
     61       }
     62     }
     63 
     64     public void time_String_getBytes(int reps) throws Exception {
     65         String string = makeString(length);
     66         for (int i = 0; i < reps; ++i) {
     67             string.getBytes(name);
     68         }
     69     }
     70 
     71     private static String makeString(int length) {
     72         StringBuilder result = new StringBuilder(length);
     73         for (int i = 0; i < length; ++i) {
     74             result.append('A' + (i % 26));
     75         }
     76         return result.toString();
     77     }
     78 
     79     private static byte[] makeBytes(String s) {
     80         try {
     81             return s.getBytes("US-ASCII");
     82         } catch (Exception ex) {
     83             throw new RuntimeException(ex);
     84         }
     85     }
     86 }
     87