Home | History | Annotate | Download | only in worker
      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.FailureLogMessage;
     20 import com.google.caliper.bridge.OpenedSocket;
     21 import com.google.caliper.bridge.ShouldContinueMessage;
     22 import com.google.caliper.bridge.StartMeasurementLogMessage;
     23 import com.google.caliper.bridge.StartupAnnounceMessage;
     24 import com.google.caliper.bridge.StopMeasurementLogMessage;
     25 import com.google.caliper.bridge.VmPropertiesLogMessage;
     26 import com.google.caliper.model.Measurement;
     27 
     28 import java.io.Closeable;
     29 import java.io.IOException;
     30 import java.util.UUID;
     31 
     32 /** The worker's interface for communicating with the runner. */
     33 final class WorkerEventLog implements Closeable {
     34   private final OpenedSocket.Writer writer;
     35   private final OpenedSocket.Reader reader;
     36 
     37   WorkerEventLog(OpenedSocket socket) {
     38     this.writer = socket.writer();
     39     this.reader = socket.reader();
     40   }
     41 
     42   void notifyWorkerStarted(UUID trialId) throws IOException {
     43     writer.write(new StartupAnnounceMessage(trialId));
     44     writer.write(new VmPropertiesLogMessage());
     45     writer.flush();
     46   }
     47 
     48   void notifyBootstrapPhaseStarting() throws IOException {
     49     writer.write("Bootstrap phase starting.");
     50     writer.flush();
     51   }
     52 
     53   void notifyMeasurementPhaseStarting() throws IOException {
     54     writer.write("Measurement phase starting (includes warmup and actual measurement).");
     55     writer.flush();
     56   }
     57 
     58   void notifyMeasurementStarting() throws IOException {
     59     writer.write("About to measure.");
     60     writer.write(new StartMeasurementLogMessage());
     61     writer.flush();
     62   }
     63 
     64   /**
     65    * Report the measurements and wait for it to be ack'd by the runner. Returns a message received
     66    * from the runner, which lets us know whether to continue measuring and whether we're in the
     67    * warmup or measurement phase.
     68    */
     69   ShouldContinueMessage notifyMeasurementEnding(Iterable<Measurement> measurements) throws
     70       IOException {
     71     writer.write(new StopMeasurementLogMessage(measurements));
     72     writer.flush();
     73     return (ShouldContinueMessage) reader.read();
     74   }
     75 
     76   void notifyFailure(Exception e) throws IOException {
     77     writer.write(new FailureLogMessage(e));
     78     writer.flush();
     79   }
     80 
     81   @Override public void close() throws IOException {
     82     try {
     83       reader.close();
     84     } finally {
     85       writer.close();
     86     }
     87   }
     88 }
     89