Home | History | Annotate | Download | only in examples
      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 
     17 package examples;
     18 
     19 import com.google.caliper.AfterExperiment;
     20 import com.google.caliper.BeforeExperiment;
     21 import com.google.caliper.Benchmark;
     22 import com.google.caliper.Param;
     23 import com.google.caliper.api.SkipThisScenarioException;
     24 import com.google.caliper.api.VmOptions;
     25 import com.google.caliper.util.ShortDuration;
     26 
     27 import java.math.BigDecimal;
     28 
     29 @VmOptions("-server")
     30 public class DemoBenchmark {
     31   @Param({"abc", "def", "xyz"}) String string;
     32   @Param({"1", "2"}) int number;
     33   @Param Foo foo;
     34 
     35   @Param({"0.00", "123.45"}) BigDecimal money;
     36   @Param({"1ns", "2 minutes"}) ShortDuration duration;
     37 
     38   enum Foo {
     39     FOO, BAR, BAZ, QUX;
     40   }
     41 
     42   DemoBenchmark() {
     43 //    System.out.println("I should not do this.");
     44   }
     45 
     46   @BeforeExperiment void setUp() throws Exception {
     47     if (string.equals("abc") && number == 1) {
     48       throw new SkipThisScenarioException();
     49     }
     50   }
     51 
     52   @Benchmark int something(int reps) {
     53     int dummy = 0;
     54     for (int i = 0; i < reps; i++) {
     55       dummy += i;
     56     }
     57     return dummy;
     58   }
     59 
     60   @Benchmark int somethingElse(int reps) {
     61     int dummy = 0;
     62     for (int i = 0; i < reps; i++) {
     63       dummy -= i;
     64     }
     65     return dummy;
     66   }
     67 
     68   @AfterExperiment void tearDown() throws Exception {
     69 //    System.out.println("Hey, I'm tearing up the joint.");
     70   }
     71 }
     72