reuvenlax commented on code in PR #41544: URL: https://github.com/apache/beam/pull/41544#discussion_r4672874614
########## runners/google-cloud-dataflow-java/worker/jmh/src/main/java/org/apache/beam/runners/dataflow/worker/jmh/ParDoDispatchBenchmark.java: ########## @@ -0,0 +1,405 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.beam.runners.dataflow.worker.jmh; + +import java.io.Closeable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import org.apache.beam.runners.core.DoFnRunner; +import org.apache.beam.runners.core.DoFnRunners; +import org.apache.beam.runners.core.ElementByteSizeObservable; +import org.apache.beam.runners.core.NullSideInputReader; +import org.apache.beam.runners.dataflow.worker.BatchModeExecutionContext; +import org.apache.beam.runners.dataflow.worker.DataflowOperationContext; +import org.apache.beam.runners.dataflow.worker.DataflowOutputCounter; +import org.apache.beam.runners.dataflow.worker.WorkerJmhAccess; +import org.apache.beam.runners.dataflow.worker.counters.CounterFactory; +import org.apache.beam.runners.dataflow.worker.counters.NameContext; +import org.apache.beam.runners.dataflow.worker.util.common.worker.OutputReceiver; +import org.apache.beam.runners.dataflow.worker.util.common.worker.ParDoFn; +import org.apache.beam.runners.dataflow.worker.util.common.worker.ParDoOperation; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.StringUtf8Coder; +import org.apache.beam.sdk.options.PipelineOptions; +import org.apache.beam.sdk.options.PipelineOptionsFactory; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.DoFnSchemaInformation; +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; +import org.apache.beam.sdk.transforms.windowing.FixedWindows; +import org.apache.beam.sdk.transforms.windowing.IntervalWindow; +import org.apache.beam.sdk.transforms.windowing.PaneInfo; +import org.apache.beam.sdk.util.WindowedValueMultiReceiver; +import org.apache.beam.sdk.util.common.ElementByteSizeObserver; +import org.apache.beam.sdk.values.TupleTag; +import org.apache.beam.sdk.values.WindowedValue; +import org.apache.beam.sdk.values.WindowedValues; +import org.apache.beam.sdk.values.WindowingStrategy; +import org.joda.time.Duration; +import org.joda.time.Instant; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; + +/** + * Microbenchmarks for {@link org.apache.beam.runners.core.SimpleDoFnRunner} and fused Dataflow + * Runner v1 {@link ParDoOperation} chains. + */ +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +public class ParDoDispatchBenchmark { + + private static final TupleTag<String> MAIN_TAG = new TupleTag<>("main"); + private static final TupleTag<String> SIDE_TAG = new TupleTag<>("side"); + + private static final class PassThroughDoFn extends DoFn<String, String> { + @ProcessElement + public void processElement(@Element String elem, DoFn.OutputReceiver<String> out) { + out.output(elem); + } + } + + private static final class MultiOutDoFn extends DoFn<String, String> { + @ProcessElement + public void processElement(@Element String elem, MultiOutputReceiver out) { + out.get(MAIN_TAG).output(elem); + out.get(SIDE_TAG).output(elem); + } + } + + private static final class WindowObservingDoFn extends DoFn<String, String> { + @ProcessElement + public void processElement( + @Element String elem, BoundedWindow window, DoFn.OutputReceiver<String> out) { + if (window != null) { + out.output(elem); + } + } + } + + private static final class SkewTimestampDoFn extends DoFn<String, String> { + private static final Duration SKEW = Duration.standardHours(1); + + @Override + public Duration getAllowedTimestampSkew() { + return SKEW; + } + + @ProcessElement + public void processElement( + @Element String elem, @Timestamp Instant ts, DoFn.OutputReceiver<String> out) { + out.outputWithTimestamp(elem, ts); + } + } + + private static final class CoderByteSizeObservable<T> implements ElementByteSizeObservable<T> { + private final Coder<T> coder; + + CoderByteSizeObservable(Coder<T> coder) { + this.coder = coder; + } + + @Override + public boolean isRegisterByteSizeObserverCheap(T value) { + return coder.isRegisterByteSizeObserverCheap(value); + } + + @Override + public void registerByteSizeObserver(T value, ElementByteSizeObserver observer) + throws Exception { + coder.registerByteSizeObserver(value, observer); + } + } + + private static ParDoOperation[] buildFusedChain( + PipelineOptions options, + BatchModeExecutionContext execCtx, + WindowingStrategy<?, ?> strategy, + int depth, + WindowedValue<String> warmElem, + long[] outputCounter) + throws Exception { + ParDoOperation[] ops = new ParDoOperation[depth]; + OutputReceiver[] receivers = new OutputReceiver[depth]; + Map<TupleTag<?>, Coder<?>> coders = Collections.singletonMap(MAIN_TAG, StringUtf8Coder.of()); + Map<TupleTag<?>, Integer> tagToIdx = Collections.singletonMap(MAIN_TAG, 0); + Coder<WindowedValue<String>> fullCoder = + WindowedValues.getFullCoder(StringUtf8Coder.of(), strategy.getWindowFn().windowCoder()); + ElementByteSizeObservable<WindowedValue<String>> observable = + new CoderByteSizeObservable<>(fullCoder); + + for (int i = 0; i < depth; i++) { + NameContext nc = NameContext.create("S1", "s" + i, "s" + i, "ParDo" + i); + DataflowOperationContext opCtx = execCtx.createOperationContext(nc); + DataflowOutputCounter counter = + DataflowOutputCounter.create( + "s" + i + ".out", observable, opCtx.counterFactory(), nc, false); + for (int w = 0; w < 1_000_010; w++) { Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
