/*
 * Copyright (C) 2010 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 examples;

import com.google.caliper.Benchmark;
import com.google.caliper.Param;
import com.google.caliper.Runner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class ContainsBenchmark extends Benchmark {
  @Param({"0", "25", "50", "75", "100"}) private int percentNulls;
  @Param({"100", "1000", "10000"}) private int containsPerRep;

  /** the set under test */
  private final Set<String> set = new HashSet<String>();

  /** twenty-five percent nulls */
  private final List<Object> queries = new ArrayList<Object>();

  @Override protected void setUp() {
    set.addAll(Arrays.asList("str1", "str2", "str3", "str4"));
    int nullThreshold = percentNulls * containsPerRep / 100;
    for (int i = 0; i < nullThreshold; i++) {
      queries.add(null);
    }
    for (int i = nullThreshold; i < containsPerRep; i++) {
      queries.add(new Object());
    }
    Collections.shuffle(queries, new Random(0));
  }

  @Override public Map<String, Integer> getTimeUnitNames() {
    Map<String, Integer> unitNames = new HashMap<String, Integer>();
    unitNames.put("ns/contains", 1);
    unitNames.put("us/contains", 1000);
    unitNames.put("ms/contains", 1000000);
    unitNames.put("s/contains", 1000000000);
    return unitNames;
  }

  @Override public double nanosToUnits(double nanos) {
    return nanos / containsPerRep;
  }

  public void timeContains(int reps) {
    for (int i = 0; i < reps; i++) {
      for (Object query : queries) {
        set.contains(query);
      }
    }
  }

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

@@ -712,16 +712,17 @@
 Runner;%0A
+%0A
 import j
@@ -800,34 +800,8 @@
 ns;%0A
-import java.util.HashMap;%0A
 impo
@@ -849,30 +849,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -1655,425 +1655,8 @@
  %7D%0A%0A
-  @Override public Map%3CString, Integer%3E getTimeUnitNames() %7B%0A    Map%3CString, Integer%3E unitNames = new HashMap%3CString, Integer%3E();%0A    unitNames.put(%22ns/contains%22, 1);%0A    unitNames.put(%22us/contains%22, 1000);%0A    unitNames.put(%22ms/contains%22, 1000000);%0A    unitNames.put(%22s/contains%22, 1000000000);%0A    return unitNames;%0A  %7D%0A%0A  @Override public double nanosToUnits(double nanos) %7B%0A    return nanos / containsPerRep;%0A  %7D%0A%0A
   pu

Delete legacy benchmark methods. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=41345104