blob: 3313eeebd65f99dea4644d4875aaf21b125bf701 [file] [log] [blame]
Paul Duffin7fc0b452015-11-10 17:45:15 +00001/*
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
17package examples;
18
Paul Duffine2363012015-11-30 16:20:41 +000019import com.google.caliper.AfterExperiment;
20import com.google.caliper.BeforeExperiment;
21import com.google.caliper.Benchmark;
Paul Duffin7fc0b452015-11-10 17:45:15 +000022import com.google.caliper.Param;
Paul Duffin7fc0b452015-11-10 17:45:15 +000023import com.google.caliper.api.SkipThisScenarioException;
Paul Duffine2363012015-11-30 16:20:41 +000024import com.google.caliper.api.VmOptions;
Paul Duffin7fc0b452015-11-10 17:45:15 +000025import com.google.caliper.util.ShortDuration;
26
27import java.math.BigDecimal;
28
Paul Duffine2363012015-11-30 16:20:41 +000029@VmOptions("-server")
30public class DemoBenchmark {
Paul Duffin7fc0b452015-11-10 17:45:15 +000031 @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 Duffin7fc0b452015-11-10 17:45:15 +000037
38 enum Foo {
39 FOO, BAR, BAZ, QUX;
40 }
41
42 DemoBenchmark() {
43// System.out.println("I should not do this.");
44 }
45
Paul Duffine2363012015-11-30 16:20:41 +000046 @BeforeExperiment void setUp() throws Exception {
Paul Duffin7fc0b452015-11-10 17:45:15 +000047 if (string.equals("abc") && number == 1) {
48 throw new SkipThisScenarioException();
49 }
50 }
51
Paul Duffine2363012015-11-30 16:20:41 +000052 @Benchmark int something(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000053 int dummy = 0;
54 for (int i = 0; i < reps; i++) {
55 dummy += i;
56 }
57 return dummy;
58 }
59
Paul Duffine2363012015-11-30 16:20:41 +000060 @Benchmark int somethingElse(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000061 int dummy = 0;
62 for (int i = 0; i < reps; i++) {
63 dummy -= i;
64 }
65 return dummy;
66 }
67
Paul Duffine2363012015-11-30 16:20:41 +000068 @AfterExperiment void tearDown() throws Exception {
Paul Duffin7fc0b452015-11-10 17:45:15 +000069// System.out.println("Hey, I'm tearing up the joint.");
70 }
Paul Duffin7fc0b452015-11-10 17:45:15 +000071}