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
17package com.google.caliper.runner;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.caliper.runner.Instrument.Instrumentation;
22import com.google.common.base.MoreObjects;
23import com.google.common.base.Objects;
24import com.google.common.collect.ImmutableSortedMap;
25
26import java.util.Map;
27
28/**
29 * A single "premise" for making benchmark measurements: which class and method to invoke, which VM
30 * to use, which choices for user parameters and vmArguments to fill in and which instrument to use
31 * to measure. A caliper run will compute all possible scenarios using
32 * {@link FullCartesianExperimentSelector}, and will run one or more trials of each.
33 */
34final class Experiment {
35  private final Instrumentation instrumentation;
36  private final VirtualMachine vm;
37  private final ImmutableSortedMap<String, String> userParameters;
38
39  Experiment(
40      Instrumentation instrumentation,
41      Map<String, String> userParameters,
42      VirtualMachine vm) {
43    this.instrumentation = checkNotNull(instrumentation);
44    this.userParameters = ImmutableSortedMap.copyOf(userParameters);
45    this.vm = checkNotNull(vm);
46  }
47
48  Instrumentation instrumentation() {
49    return instrumentation;
50  }
51
52  ImmutableSortedMap<String, String> userParameters() {
53    return userParameters;
54  }
55
56  VirtualMachine vm() {
57    return vm;
58  }
59
60  @Override public boolean equals(Object object) {
61    if (object instanceof Experiment) {
62      Experiment that = (Experiment) object;
63      return this.instrumentation.equals(that.instrumentation)
64          && this.vm.equals(that.vm)
65          && this.userParameters.equals(that.userParameters);
66    }
67    return false;
68  }
69
70  @Override public int hashCode() {
71    return Objects.hashCode(instrumentation, vm, userParameters);
72  }
73
74  @Override public String toString() {
75    return MoreObjects.toStringHelper("")
76        .add("instrument", instrumentation.instrument())
77        .add("benchmarkMethod", instrumentation.benchmarkMethod.getName())
78        .add("vm", vm.name)
79        .add("parameters", userParameters)
80        .toString();
81  }
82}
83