Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 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 | |
| 17 | package examples; |
| 18 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 19 | import com.google.caliper.BeforeExperiment; |
| 20 | import com.google.caliper.Benchmark; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 21 | import com.google.caliper.Param; |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 22 | |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.Collections; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 26 | import java.util.HashSet; |
| 27 | import java.util.List; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 28 | import java.util.Random; |
| 29 | import java.util.Set; |
| 30 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 31 | public class ContainsBenchmark { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 32 | @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 Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 41 | @BeforeExperiment void setUp() { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 42 | 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 Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 53 | @Benchmark void contains(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 54 | for (int i = 0; i < reps; i++) { |
| 55 | for (Object query : queries) { |
| 56 | set.contains(query); |
| 57 | } |
| 58 | } |
| 59 | } |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 60 | } |