/**
   * Checks for Benchmark Tags. If there are not tags events are passed through. If there are, there
   * are three kinds - 'start' which instantiates a benchmark; 'first' which starts a benchmark; and
   * 'stop' which ends a benchmark. These are consumed by this decorator.
   */
  @Override
  public void append(Event e) throws IOException, InterruptedException {
    byte[] bench = e.get(BenchmarkInjectDecorator.ATTR_BENCHMARK);
    if (bench == null) {
      // This is the normal case -- a regular message
      getSink().append(e);
      return;
    }

    // All of these messages are silently consumed and not forwarded
    byte[] tagbytes = e.get(BenchmarkInjectDecorator.ATTR_BENCHMARK_TAG);
    Preconditions.checkNotNull(tagbytes);
    String tag = new String(tagbytes, CharEncUtils.RAW);

    if (Arrays.equals(bench, BenchmarkInjectDecorator.BENCH_START)) {
      StringWriter out = new StringWriter();
      PrintWriter pw = new PrintWriter(out);
      Benchmark b = new Benchmark(tag, pw, pw);
      b.mark("benchmarkStart");
      benchmarks.put(tag, new Pair<Benchmark, StringWriter>(b, out));
    } else if (Arrays.equals(bench, BenchmarkInjectDecorator.BENCH_FIRST)) {
      Benchmark b = benchmarks.get(tag).getLeft();
      b.mark("benchmarkFirst");
    } else if (Arrays.equals(bench, BenchmarkInjectDecorator.BENCH_STOP)) {
      Benchmark b = benchmarks.get(tag).getLeft();
      b.mark("benchmarkDone");
      b.done();

      ReportEvent rpt = getMetrics();
      LOG.info(rpt.toText());
      reportSink.append(rpt);

    } else if (Arrays.equals(bench, BenchmarkInjectDecorator.BENCH_ERROR)) {
      Benchmark b = benchmarks.get(tag).getLeft();
      b.mark("benchmarkError");
      b.done();
      LOG.info(getMetrics().toText());

      ReportEvent rpt = getMetrics();
      LOG.info(rpt.toText());
      reportSink.append(rpt);
    } else {
      String msg = "Unexpected Benchmark event type: " + tag;
      LOG.error(msg);
      throw new IllegalArgumentException(msg);
    }
  }
Esempio n. 2
0
  @Override
  public void append(Event e) throws IOException, InterruptedException {
    byte[] btyp = e.get(CheckpointDeco.ATTR_CK_TYPE);

    if (btyp == null) {
      super.append(e);
      return;
    }

    byte[] btag = e.get(CheckpointDeco.ATTR_CK_TAG);
    String k = new String(btag);

    if (Arrays.equals(btyp, CheckpointDeco.CK_START)) {
      listener.start(k);
      return;
    } else if (Arrays.equals(btyp, CheckpointDeco.CK_END)) {
      listener.end(k);
      return;
    }
    super.append(e);
  }