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
17package benchmarks.regression;
18
19import com.google.caliper.Param;
20
21public class CharsetBenchmark {
22    @Param({ "1", "10", "100", "1000", "10000" })
23    private int length;
24
25    // canonical    => canonical charset name
26    // built-in     => guaranteed-present charset
27    // special-case => libcore treats this charset specially for performance
28    @Param({
29        "UTF-16",     //     canonical,     built-in, non-special-case
30        "UTF-8",      //     canonical,     built-in,     special-case
31        "UTF8",       // non-canonical,     built-in,     special-case
32        "ISO-8859-1", //     canonical,     built-in,     special-case
33        "8859_1",     // non-canonical,     built-in,     special-case
34        "ISO-8859-2", //     canonical, non-built-in, non-special-case
35        "8859_2",     // non-canonical, non-built-in, non-special-case
36        "US-ASCII",   //     canonical,     built-in,     special-case
37        "ASCII"       // non-canonical,     built-in,     special-case
38    })
39    private String name;
40
41    public void time_new_String_BString(int reps) throws Exception {
42        byte[] bytes = makeBytes(makeString(length));
43        for (int i = 0; i < reps; ++i) {
44            new String(bytes, name);
45        }
46    }
47
48    public void time_new_String_BII(int reps) throws Exception {
49      byte[] bytes = makeBytes(makeString(length));
50      for (int i = 0; i < reps; ++i) {
51        new String(bytes, 0, bytes.length);
52      }
53    }
54
55    public void time_new_String_BIIString(int reps) throws Exception {
56      byte[] bytes = makeBytes(makeString(length));
57      for (int i = 0; i < reps; ++i) {
58        new String(bytes, 0, bytes.length, name);
59      }
60    }
61
62    public void time_String_getBytes(int reps) throws Exception {
63        String string = makeString(length);
64        for (int i = 0; i < reps; ++i) {
65            string.getBytes(name);
66        }
67    }
68
69    private static String makeString(int length) {
70        StringBuilder result = new StringBuilder(length);
71        for (int i = 0; i < length; ++i) {
72            result.append('A' + (i % 26));
73        }
74        return result.toString();
75    }
76
77    private static byte[] makeBytes(String s) {
78        try {
79            return s.getBytes("US-ASCII");
80        } catch (Exception ex) {
81            throw new RuntimeException(ex);
82        }
83    }
84}
85