aboutsummaryrefslogtreecommitdiff
path: root/test/com/google/caliper/examples/EnumSetContainsBenchmarkSuite.java
blob: da8f0f5221156887a1b30a85285fbbc5fb5027e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.caliper.examples;

import com.google.caliper.Benchmark;
import com.google.caliper.DefaultBenchmarkSuite;
import com.google.caliper.Param;
import com.google.caliper.Runner;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;

/**
 * Measures EnumSet#contains().
 */
public class EnumSetContainsBenchmarkSuite extends DefaultBenchmarkSuite {

  @Param private SetMaker setMaker;

  private static Collection<SetMaker> setMakerValues = EnumSet.allOf(SetMaker.class);

  enum SetMaker {
    ENUM_SET {
      @Override Set<?> newSet() {
        return EnumSet.allOf(RegularSize.class);
      }
      @Override Object[] testValues() {
        return new Object[] { RegularSize.E1, RegularSize.E2, RegularSize.E20,
            RegularSize.E39, RegularSize.E40, "A", LargeSize.E40, null };
      }
    },
    LARGE_ENUM_SET {
      @Override Set<?> newSet() {
        return EnumSet.allOf(LargeSize.class);
      }
      @Override Object[] testValues() {
        return new Object[] { LargeSize.E1, LargeSize.E63, LargeSize.E64,
            LargeSize.E65, LargeSize.E140, "A", RegularSize.E40, null };
      }
    };

    abstract Set<?> newSet();
    abstract Object[] testValues();
  }

  enum RegularSize {
    E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17,
    E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32,
    E33, E34, E35, E36, E37, E38, E39, E40,
  }

  enum LargeSize {
    E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17,
    E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32,
    E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E47,
    E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62,
    E63, E64, E65, E66, E67, E68, E69, E70, E71, E72, E73, E74, E75, E76, E77,
    E78, E79, E80, E81, E82, E83, E84, E85, E86, E87, E88, E89, E90, E91, E92,
    E93, E94, E95, E96, E97, E98, E99, E100, E101, E102, E103, E104, E105, E106,
    E107, E108, E109, E110, E111, E112, E113, E114, E115, E116, E117, E118,
    E119, E120, E121, E122, E123, E124, E125, E126, E127, E128, E129, E130,
    E131, E132, E133, E134, E135, E136, E137, E138, E139, E140,
  }

  private Set<?> set;
  private Object[] testValues;

  @Override protected void setUp() throws Exception {
    this.set = setMaker.newSet();
    this.testValues = setMaker.testValues();
  }

  class ContainsBenchmark extends Benchmark {
    @Override public Object run(int trials) throws Exception {
      int count = 0;
      for (int i = 0; i < trials; i++) {
        for (Object value : testValues) {
          count ^= (set.contains(value) ? i : 0);
        }
      }
      return count > 0;
    }
  }

  public static void main(String[] args) {
    Runner.main(EnumSetContainsBenchmarkSuite.class, args);
  }
}