1/*
2 * Copyright (C) 2011 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;
20import com.google.caliper.SimpleBenchmark;
21import java.net.URI;
22import java.net.URL;
23
24public final class EqualsHashCodeBenchmark extends SimpleBenchmark {
25    private enum Type {
26        URI() {
27            @Override Object newInstance(String text) throws Exception {
28                return new URI(text);
29            }
30        },
31        URL() {
32            @Override Object newInstance(String text) throws Exception {
33                return new URL(text);
34            }
35        };
36        abstract Object newInstance(String text) throws Exception;
37    }
38
39    @Param Type type;
40
41    Object a1;
42    Object a2;
43    Object b1;
44    Object b2;
45
46    @Override protected void setUp() throws Exception {
47        a1 = type.newInstance("https://mail.google.com/mail/u/0/?shva=1#inbox");
48        a2 = type.newInstance("https://mail.google.com/mail/u/0/?shva=1#inbox");
49        b1 = type.newInstance("http://developer.android.com/reference/java/net/URI.html");
50        b2 = type.newInstance("http://developer.android.com/reference/java/net/URI.html");
51    }
52
53    public void timeEquals(int reps) {
54        for (int i = 0; i < reps; i+=3) {
55            a1.equals(b1);
56            a1.equals(a2);
57            b1.equals(b2);
58        }
59    }
60
61    public void timeHashCode(int reps) {
62        for (int i = 0; i < reps; i+=2) {
63            a1.hashCode();
64            b1.hashCode();
65        }
66    }
67}
68