Skip to content

Commit

Permalink
Change the WorkerSpec that's sent to workers to an abstract class (Wo…
Browse files Browse the repository at this point in the history
…rkerRequest) with three subtypes:

- TrialRequest: what WorkerSpec previously was; a request for running a single trial
- BenchmarkInfoRequest: a request for the info on a benchmark class (methods,
  parameters, etc.)
- DryRunRequest: a request for the worker to do a dry run of the benchmark

Handling for BenchmarkInfoRequest and DryRunRequest is NYI.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163854733
  • Loading branch information
cgdecker authored and ronshapiro committed Aug 3, 2017
1 parent 88b253a commit 24ce9b2
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.caliper.bridge;

import static com.google.common.base.Preconditions.checkArgument;

/**
* {@link WorkerRequest} for telling a worker to send the runner the information it needs on the
* benchmark, such as what methods and parameters it has.
*
* @author Colin Decker
*/
public final class BenchmarkInfoRequest extends WorkerRequest {
private static final long serialVersionUID = 1L;

private final String benchmarkClass;

public BenchmarkInfoRequest(String benchmarkClass, int port) {
super(port);
checkArgument(!benchmarkClass.isEmpty());
this.benchmarkClass = benchmarkClass;
}

/**
* Returns the name of the benchmark class to get info for.
*/
public String benchmarkClass() {
return benchmarkClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.io.ObjectOutputStream;

/**
* Serializes and deserializes WorkerSpecs as base64 encoded strings so they can be passed on the
* command line to the worker.
* Serializes and deserializes {@link WorkerRequest}s as base64 encoded strings so they can be
* passed on the command line to the worker.
*
* <p>Java serialization is a appropriate in this usecase because there are no compatibility
* <p>Java serialization is a appropriate in this use-case because there are no compatibility
* concerns. The messages encoded/decoded by this class are only used to communicate with another
* JVM that is running with the same version of the java classes. Also, it should be lighter weight
* and faster than other common serialization solutions.
Expand All @@ -38,7 +38,7 @@ public final class CommandLineSerializer {
private CommandLineSerializer() {}

/** Returns the given serializable object as a base64 encoded String. */
public static String render(WorkerSpec message) {
public static String render(WorkerRequest message) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream(bytes);
Expand All @@ -51,11 +51,11 @@ public static String render(WorkerSpec message) {
}

/** Parses the given base64 encoded string as an object of the given type. */
public static WorkerSpec parse(String arg) {
public static WorkerRequest parse(String arg) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(BaseEncoding.base64().decode(arg));
ObjectInputStream in = new ObjectInputStream(bais);
WorkerSpec instance = (WorkerSpec) in.readObject();
WorkerRequest instance = (WorkerRequest) in.readObject();
in.close();
checkState(bais.read() == -1, "Message %s contains more than one object.", arg);
return instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.caliper.bridge;

/**
* {@link WorkerRequest} for telling a worker to do a dry-run of a benchmark.
*
* @author Colin Decker
*/
public final class DryRunRequest extends WorkerRequest {
private static final long serialVersionUID = 1L;

public DryRunRequest(int port) {
super(port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,70 @@
import com.google.caliper.model.InstrumentType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.UUID;

/**
* This object is sent from the parent process to the child to tell it what to do. If the child does
* not do it, it will not get its allowance this week.
* {@link WorkerRequest} for telling the worker to run a trial of the benchmark.
*
* @author Colin Decker
*/
public final class WorkerSpec implements Serializable {
public final class TrialRequest extends WorkerRequest {
private static final long serialVersionUID = 1L;

public final UUID trialId;
public final InstrumentType instrumentType;
public final ImmutableMap<String, String> workerOptions;
public final BenchmarkSpec benchmarkSpec;

/** The names of the benchmark method parameters so that the method can be uniquely identified. */
public final ImmutableList<Class<?>> methodParameterClasses;
private final UUID trialId;
private final InstrumentType instrumentType;
private final ImmutableMap<String, String> workerOptions;
private final BenchmarkSpec benchmarkSpec;
private final ImmutableList<Class<?>> methodParameterClasses;

public final int port;

public WorkerSpec(
public TrialRequest(
UUID trialId,
InstrumentType instrumentType,
ImmutableMap<String, String> workerOptions,
BenchmarkSpec benchmarkSpec,
ImmutableList<Class<?>> methodParameterClasses,
int port) {
super(port);
this.trialId = trialId;
this.instrumentType = instrumentType;
this.workerOptions = workerOptions;
this.benchmarkSpec = benchmarkSpec;
this.methodParameterClasses = methodParameterClasses;
this.port = port;
}

/**
* Returns the ID of the trial to run.
*/
public UUID trialId() {
return trialId;
}

/**
* Returns the instrument to use for the trial.
*/
public InstrumentType instrumentType() {
return instrumentType;
}

/**
* Returns the worker options to use.
*/
public ImmutableMap<String, String> workerOptions() {
return workerOptions;
}

/**
* Returns the spec of the benchmark to run for the trial.
*/
public BenchmarkSpec benchmarkSpec() {
return benchmarkSpec;
}

/**
* Returns the parameter types for the benchmark method to run so that it can be uniquely
* identified.
*/
public ImmutableList<Class<?>> methodParameterClasses() {
return methodParameterClasses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.caliper.bridge;

import java.io.Serializable;

/**
* Base class for classes the runner sends to the worker to tell it what to do.
*
* @author Colin Decker
*/
public abstract class WorkerRequest implements Serializable {

private final int port;

protected WorkerRequest(int port) {
this.port = port;
}

/**
* Returns the port the worker should open a socket connection to for communication.
*/
public int port() {
return port;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.caliper.bridge.WorkerSpec;
import com.google.caliper.bridge.TrialRequest;
import com.google.caliper.core.Running.Benchmark;
import com.google.caliper.core.Running.BenchmarkMethod;
import com.google.caliper.util.Util;
Expand All @@ -44,13 +44,13 @@ public static ExperimentModule forExperiment(Experiment experiment) {
return new ExperimentModule(benchmarkMethod, experiment.userParameters());
}

public static ExperimentModule forWorkerSpec(WorkerSpec spec) throws ClassNotFoundException {
Class<?> benchmarkClass = Util.loadClass(spec.benchmarkSpec.className());
public static ExperimentModule forTrialRequest(TrialRequest req) throws ClassNotFoundException {
Class<?> benchmarkClass = Util.loadClass(req.benchmarkSpec().className());
Method benchmarkMethod =
findBenchmarkMethod(
benchmarkClass, spec.benchmarkSpec.methodName(), spec.methodParameterClasses);
benchmarkClass, req.benchmarkSpec().methodName(), req.methodParameterClasses());
benchmarkMethod.setAccessible(true);
return new ExperimentModule(benchmarkMethod, spec.benchmarkSpec.parameters());
return new ExperimentModule(benchmarkMethod, req.benchmarkSpec().parameters());
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.google.caliper.bridge.CommandLineSerializer;
import com.google.caliper.bridge.OpenedSocket;
import com.google.caliper.bridge.WorkerSpec;
import com.google.caliper.bridge.TrialRequest;
import com.google.caliper.config.VmConfig;
import com.google.caliper.model.BenchmarkSpec;
import com.google.caliper.platform.Platform;
Expand Down Expand Up @@ -165,8 +165,8 @@ static ProcessBuilder buildProcess(
// TODO(lukes): it would be nice to split this method into a few smaller more targeted methods
Instrumentation instrumentation = experiment.instrumentation();
Instrument instrument = instrumentation.instrument();
WorkerSpec request =
new WorkerSpec(
TrialRequest request =
new TrialRequest(
trialId,
instrumentation.type(),
instrumentation.workerOptions(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.caliper.worker;

import com.google.caliper.bridge.WorkerSpec;
import com.google.caliper.bridge.TrialRequest;
import com.google.caliper.runner.ExperimentModule;

/**
Expand All @@ -31,10 +31,10 @@ public static void main(String[] args) throws Exception {
}

@Override
protected WorkerComponent createWorkerComponent(WorkerSpec request)
protected WorkerComponent createWorkerComponent(TrialRequest request)
throws ClassNotFoundException {
return DaggerDalvikWorkerComponent.builder()
.experimentModule(ExperimentModule.forWorkerSpec(request))
.experimentModule(ExperimentModule.forTrialRequest(request))
.workerModule(new WorkerModule(request))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.caliper.worker;

import com.google.caliper.bridge.WorkerSpec;
import com.google.caliper.bridge.TrialRequest;
import com.google.caliper.runner.ExperimentModule;

/**
Expand All @@ -31,10 +31,10 @@ public static void main(String[] args) throws Exception {
}

@Override
protected WorkerComponent createWorkerComponent(WorkerSpec request)
protected WorkerComponent createWorkerComponent(TrialRequest request)
throws ClassNotFoundException {
return DaggerJvmWorkerComponent.builder()
.experimentModule(ExperimentModule.forWorkerSpec(request))
.experimentModule(ExperimentModule.forTrialRequest(request))
.workerModule(new WorkerModule(request))
.build();
}
Expand Down
Loading

0 comments on commit 24ce9b2

Please sign in to comment.