Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      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 com.google.caliper.Param;
     20 
     21 import java.nio.charset.StandardCharsets;
     22 
     23 public class StringToBytesBenchmark {
     24     static enum StringLengths {
     25         EMPTY(""),
     26         L_16(makeString(16)),
     27         L_64(makeString(64)),
     28         L_256(makeString(256)),
     29         L_512(makeString(512));
     30 
     31         private final String value;
     32 
     33         private StringLengths(String s) {
     34             this.value = s;
     35         }
     36     }
     37 
     38     private static final String makeString(int length) {
     39         char[] chars = new char[length];
     40         for (int i = 0; i < length; ++i) {
     41             chars[i] = (char) i;
     42         }
     43         return new String(chars);
     44     }
     45 
     46     @Param StringLengths string;
     47 
     48     public void timeGetBytesUtf8(int nreps) {
     49         for (int i = 0; i < nreps; ++i) {
     50             string.value.getBytes(StandardCharsets.UTF_8);
     51         }
     52     }
     53 
     54     public void timeGetBytesIso88591(int nreps) {
     55         for (int i = 0; i < nreps; ++i) {
     56             string.value.getBytes(StandardCharsets.ISO_8859_1);
     57         }
     58     }
     59 
     60     public void timeGetBytesAscii(int nreps) {
     61         for (int i = 0; i < nreps; ++i) {
     62             string.value.getBytes(StandardCharsets.US_ASCII);
     63         }
     64     }
     65 }
     66