blob: 1f7e2011b00b490cf7d7c9ab6a181bf5aa6a3f13 [file] [log] [blame]
Paul Duffine2363012015-11-30 16:20:41 +00001/*
2 * Copyright (C) 2012 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.worker;
18
19import com.google.caliper.Param;
20import com.google.caliper.bridge.WorkerSpec;
21import com.google.caliper.runner.Running;
22import com.google.caliper.util.InvalidCommandException;
23import com.google.caliper.util.Util;
24import com.google.common.base.Ticker;
25import com.google.common.collect.ImmutableMap;
26import dagger.MapKey;
27import dagger.Module;
28import dagger.Provides;
29import dagger.Provides.Type;
30
31import java.util.Map;
32import java.util.Random;
33
34import javax.inject.Provider;
35
36/**
37 * Binds classes necessary for the worker. Also manages the injection of {@link Param parameters}
38 * from the {@link WorkerSpec} into the benchmark.
39 *
40 * <p>TODO(gak): Ensure that each worker only has bindings for the objects it needs and not the
41 * objects required by different workers. (i.e. don't bind a Ticker if the worker is an allocation
42 * worker).
43 */
44@Module
45final class WorkerModule {
46 private final Class<? extends Worker> workerClass;
47 private final ImmutableMap<String, String> workerOptions;
48
49 private final Class<?> benchmarkClassObject;
50
51 WorkerModule(WorkerSpec workerSpec) throws ClassNotFoundException {
52 this.workerClass = workerSpec.workerClass.asSubclass(Worker.class);
53 this.workerOptions = workerSpec.workerOptions;
54
55 benchmarkClassObject = Util.loadClass(workerSpec.benchmarkSpec.className());
56 }
57
58 @Provides
59 @Running.BenchmarkClass
60 Class<?> provideBenchmarkClassObject() {
61 return benchmarkClassObject;
62 }
63
64 @Provides
65 Worker provideWorker(Map<Class<? extends Worker>, Provider<Worker>> availableWorkers) {
66 Provider<Worker> workerProvider = availableWorkers.get(workerClass);
67 if (workerProvider == null) {
68 throw new InvalidCommandException("%s is not a supported worker (%s).",
69 workerClass, availableWorkers);
70 }
71 return workerProvider.get();
72 }
73
74 /**
75 * Specifies the {@link Class} object to use as a key in the map of available
76 * {@link Worker workers} passed to {@link #provideWorker(Map)}.
77 */
78 @MapKey(unwrapValue = true)
79 public @interface WorkerClassKey {
80 Class<? extends Worker> value();
81 }
82
83 @Provides(type = Type.MAP)
84 @WorkerClassKey(ArbitraryMeasurementWorker.class)
85 static Worker provideArbitraryMeasurementWorker(ArbitraryMeasurementWorker impl) {
86 return impl;
87 }
88
89 @Provides(type = Type.MAP)
90 @WorkerClassKey(MicrobenchmarkAllocationWorker.class)
91 static Worker provideMicrobenchmarkAllocationWorker(MicrobenchmarkAllocationWorker impl) {
92 return impl;
93 }
94
95 @Provides(type = Type.MAP)
96 @WorkerClassKey(MacrobenchmarkWorker.class)
97 static Worker provideMacrobenchmarkWorker(MacrobenchmarkWorker impl) {
98 return impl;
99 }
100
101 @Provides(type = Type.MAP)
102 @WorkerClassKey(MacrobenchmarkAllocationWorker.class)
103 static Worker provideMacrobenchmarkAllocationWorker(MacrobenchmarkAllocationWorker impl) {
104 return impl;
105 }
106
107 @Provides(type = Type.MAP)
108 @WorkerClassKey(RuntimeWorker.Micro.class)
109 static Worker provideRuntimeWorkerMicro(RuntimeWorker.Micro impl) {
110 return impl;
111 }
112
113 @Provides(type = Type.MAP)
114 @WorkerClassKey(RuntimeWorker.Pico.class)
115 static Worker provideRuntimeWorkerPico(RuntimeWorker.Pico impl) {
116 return impl;
117 }
118
119 @Provides
120 static Ticker provideTicker() {
121 return Ticker.systemTicker();
122 }
123
124 @Provides
125 AllocationRecorder provideAllocationRecorder(
126 Provider<AllAllocationsRecorder> allAllocationsRecorderProvider,
127 Provider<AggregateAllocationsRecorder> aggregateAllocationsRecorderProvider) {
128
129 return Boolean.valueOf(workerOptions.get("trackAllocations"))
130 ? allAllocationsRecorderProvider.get()
131 : aggregateAllocationsRecorderProvider.get();
132 }
133
134 @Provides
135 static Random provideRandom() {
136 return new Random();
137 }
138
139 @Provides
140 @WorkerOptions
141 Map<String, String> provideWorkerOptions() {
142 return workerOptions;
143 }
144}