Paul Duffin | e236301 | 2015-11-30 16:20:41 +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 com.google.caliper.worker; |
| 18 | |
| 19 | import com.google.caliper.bridge.CommandLineSerializer; |
| 20 | import com.google.caliper.bridge.OpenedSocket; |
| 21 | import com.google.caliper.bridge.ShouldContinueMessage; |
| 22 | import com.google.caliper.bridge.WorkerSpec; |
| 23 | import com.google.caliper.runner.ExperimentModule; |
| 24 | import com.google.common.net.InetAddresses; |
| 25 | |
| 26 | import java.net.InetSocketAddress; |
| 27 | import java.nio.channels.SocketChannel; |
| 28 | |
| 29 | /** |
| 30 | * This class is invoked as a subprocess by the Caliper runner parent process; it re-stages |
| 31 | * the benchmark and hands it off to the instrument's worker. |
| 32 | */ |
| 33 | public final class WorkerMain { |
| 34 | private WorkerMain() {} |
| 35 | |
| 36 | public static void main(String[] args) throws Exception { |
| 37 | // TODO(lukes): instead of parsing the spec from the command line pass the port number on the |
| 38 | // command line and then receive the spec from the socket. This way we can start JVMs prior |
| 39 | // to starting experiments and thus get better experiment latency. |
| 40 | WorkerSpec request = CommandLineSerializer.parse(args[0]); |
| 41 | // nonblocking connect so we can interleave the system call with injector creation. |
| 42 | SocketChannel channel = SocketChannel.open(); |
| 43 | channel.configureBlocking(false); |
| 44 | channel.connect(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), request.port)); |
| 45 | |
| 46 | WorkerComponent workerComponent = DaggerWorkerComponent.builder() |
| 47 | .experimentModule(ExperimentModule.forWorkerSpec(request)) |
| 48 | .workerModule(new WorkerModule(request)) |
| 49 | .build(); |
| 50 | Worker worker = workerComponent.getWorker(); |
| 51 | WorkerEventLog log = new WorkerEventLog(OpenedSocket.fromSocket(channel)); |
| 52 | |
| 53 | log.notifyWorkerStarted(request.trialId); |
| 54 | try { |
| 55 | worker.setUpBenchmark(); |
| 56 | log.notifyBootstrapPhaseStarting(); |
| 57 | worker.bootstrap(); |
| 58 | log.notifyMeasurementPhaseStarting(); |
| 59 | boolean keepMeasuring = true; |
| 60 | boolean isInWarmup = true; |
| 61 | while (keepMeasuring) { |
| 62 | worker.preMeasure(isInWarmup); |
| 63 | log.notifyMeasurementStarting(); |
| 64 | try { |
| 65 | ShouldContinueMessage message = log.notifyMeasurementEnding(worker.measure()); |
| 66 | keepMeasuring = message.shouldContinue(); |
| 67 | isInWarmup = !message.isWarmupComplete(); |
| 68 | } finally { |
| 69 | worker.postMeasure(); |
| 70 | } |
| 71 | } |
| 72 | } catch (Exception e) { |
| 73 | log.notifyFailure(e); |
| 74 | } finally { |
| 75 | System.out.flush(); // ? |
| 76 | worker.tearDownBenchmark(); |
| 77 | log.close(); |
| 78 | } |
| 79 | } |
| 80 | } |