blob: 26d2eb95609375b21811d5e089729bc32f0f37e5 [file] [log] [blame]
Paul Duffin7fc0b452015-11-10 17:45:15 +00001/*
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 examples;
18
Paul Duffine2363012015-11-30 16:20:41 +000019import com.google.caliper.BeforeExperiment;
20import com.google.caliper.Benchmark;
Paul Duffin7fc0b452015-11-10 17:45:15 +000021import com.google.caliper.Param;
Paul Duffine2363012015-11-30 16:20:41 +000022
Paul Duffin7fc0b452015-11-10 17:45:15 +000023import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.Collections;
Paul Duffin7fc0b452015-11-10 17:45:15 +000026import java.util.HashSet;
27import java.util.List;
Paul Duffin7fc0b452015-11-10 17:45:15 +000028import java.util.Random;
29import java.util.Set;
30
Paul Duffine2363012015-11-30 16:20:41 +000031public class ContainsBenchmark {
Paul Duffin7fc0b452015-11-10 17:45:15 +000032 @Param({"0", "25", "50", "75", "100"}) private int percentNulls;
33 @Param({"100", "1000", "10000"}) private int containsPerRep;
34
35 /** the set under test */
36 private final Set<String> set = new HashSet<String>();
37
38 /** twenty-five percent nulls */
39 private final List<Object> queries = new ArrayList<Object>();
40
Paul Duffine2363012015-11-30 16:20:41 +000041 @BeforeExperiment void setUp() {
Paul Duffin7fc0b452015-11-10 17:45:15 +000042 set.addAll(Arrays.asList("str1", "str2", "str3", "str4"));
43 int nullThreshold = percentNulls * containsPerRep / 100;
44 for (int i = 0; i < nullThreshold; i++) {
45 queries.add(null);
46 }
47 for (int i = nullThreshold; i < containsPerRep; i++) {
48 queries.add(new Object());
49 }
50 Collections.shuffle(queries, new Random(0));
51 }
52
Paul Duffine2363012015-11-30 16:20:41 +000053 @Benchmark void contains(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000054 for (int i = 0; i < reps; i++) {
55 for (Object query : queries) {
56 set.contains(query);
57 }
58 }
59 }
Paul Duffin7fc0b452015-11-10 17:45:15 +000060}