Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 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 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 19 | import com.google.caliper.AfterExperiment; |
| 20 | import com.google.caliper.BeforeExperiment; |
| 21 | import com.google.caliper.Benchmark; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 22 | import com.google.caliper.Param; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 23 | import com.google.caliper.api.SkipThisScenarioException; |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 24 | import com.google.caliper.api.VmOptions; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 25 | import com.google.caliper.util.ShortDuration; |
| 26 | |
| 27 | import java.math.BigDecimal; |
| 28 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 29 | @VmOptions("-server") |
| 30 | public class DemoBenchmark { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 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; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 37 | |
| 38 | enum Foo { |
| 39 | FOO, BAR, BAZ, QUX; |
| 40 | } |
| 41 | |
| 42 | DemoBenchmark() { |
| 43 | // System.out.println("I should not do this."); |
| 44 | } |
| 45 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 46 | @BeforeExperiment void setUp() throws Exception { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 47 | if (string.equals("abc") && number == 1) { |
| 48 | throw new SkipThisScenarioException(); |
| 49 | } |
| 50 | } |
| 51 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 52 | @Benchmark int something(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 53 | int dummy = 0; |
| 54 | for (int i = 0; i < reps; i++) { |
| 55 | dummy += i; |
| 56 | } |
| 57 | return dummy; |
| 58 | } |
| 59 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 60 | @Benchmark int somethingElse(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 61 | int dummy = 0; |
| 62 | for (int i = 0; i < reps; i++) { |
| 63 | dummy -= i; |
| 64 | } |
| 65 | return dummy; |
| 66 | } |
| 67 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame] | 68 | @AfterExperiment void tearDown() throws Exception { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 69 | // System.out.println("Hey, I'm tearing up the joint."); |
| 70 | } |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 71 | } |