1/*
2 * Copyright (C) 2009 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.Benchmark;
20import com.google.caliper.Param;
21import com.google.caliper.Runner;
22import com.google.caliper.SimpleBenchmark;
23import java.text.*;
24import java.util.*;
25
26/**
27 * Benchmarks creation and cloning various expensive objects.
28 */
29public class ExpensiveObjectsBenchmark extends SimpleBenchmark {
30    public void timeNewDateFormatTimeInstance(int reps) {
31        for (int i = 0; i < reps; ++i) {
32            DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
33            df.format(System.currentTimeMillis());
34        }
35    }
36
37    public void timeClonedDateFormatTimeInstance(int reps) {
38        DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
39        for (int i = 0; i < reps; ++i) {
40            ((DateFormat) df.clone()).format(System.currentTimeMillis());
41        }
42    }
43
44    public void timeReusedDateFormatTimeInstance(int reps) {
45        DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
46        for (int i = 0; i < reps; ++i) {
47            synchronized (df) {
48                df.format(System.currentTimeMillis());
49            }
50        }
51    }
52
53    public void timeNewCollator(int reps) {
54        for (int i = 0; i < reps; ++i) {
55            Collator.getInstance(Locale.US);
56        }
57    }
58
59    public void timeClonedCollator(int reps) {
60        Collator c = Collator.getInstance(Locale.US);
61        for (int i = 0; i < reps; ++i) {
62            c.clone();
63        }
64    }
65
66    public void timeNewDateFormatSymbols(int reps) {
67        for (int i = 0; i < reps; ++i) {
68            new DateFormatSymbols(Locale.US);
69        }
70    }
71
72    public void timeClonedDateFormatSymbols(int reps) {
73        DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
74        for (int i = 0; i < reps; ++i) {
75            dfs.clone();
76        }
77    }
78
79    public void timeNewDecimalFormatSymbols(int reps) {
80        for (int i = 0; i < reps; ++i) {
81            new DecimalFormatSymbols(Locale.US);
82        }
83    }
84
85    public void timeClonedDecimalFormatSymbols(int reps) {
86        DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
87        for (int i = 0; i < reps; ++i) {
88            dfs.clone();
89        }
90    }
91
92    public void timeNewNumberFormat(int reps) {
93        for (int i = 0; i < reps; ++i) {
94            NumberFormat.getInstance(Locale.US);
95        }
96    }
97
98    public void timeClonedNumberFormat(int reps) {
99        NumberFormat nf = NumberFormat.getInstance(Locale.US);
100        for (int i = 0; i < reps; ++i) {
101            nf.clone();
102        }
103    }
104
105    public void timeNumberFormatTrivialFormatLong(int reps) {
106        NumberFormat nf = NumberFormat.getInstance(Locale.US);
107        for (int i = 0; i < reps; ++i) {
108            nf.format(1024L);
109        }
110    }
111
112    public void timeLongToString(int reps) {
113        for (int i = 0; i < reps; ++i) {
114            Long.toString(1024L);
115        }
116    }
117
118    public void timeNumberFormatTrivialFormatDouble(int reps) {
119        NumberFormat nf = NumberFormat.getInstance(Locale.US);
120        for (int i = 0; i < reps; ++i) {
121            nf.format(1024.0);
122        }
123    }
124
125    public void timeNewSimpleDateFormat(int reps) {
126        for (int i = 0; i < reps; ++i) {
127            new SimpleDateFormat();
128        }
129    }
130
131    public void timeClonedSimpleDateFormat(int reps) {
132        SimpleDateFormat sdf = new SimpleDateFormat();
133        for (int i = 0; i < reps; ++i) {
134            sdf.clone();
135        }
136    }
137
138    public void timeNewGregorianCalendar(int reps) {
139        for (int i = 0; i < reps; ++i) {
140            new GregorianCalendar();
141        }
142    }
143
144    public void timeClonedGregorianCalendar(int reps) {
145        GregorianCalendar gc = new GregorianCalendar();
146        for (int i = 0; i < reps; ++i) {
147            gc.clone();
148        }
149    }
150}
151