pabloem commented on a change in pull request #14046: URL: https://github.com/apache/beam/pull/14046#discussion_r582221371
########## File path: sdks/python/apache_beam/tools/teststream_microbenchmark.py ########## @@ -0,0 +1,129 @@ +# 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. +# + +"""A microbenchmark for measuring changes in the performance of TestStream +running locally. +This microbenchmark attempts to measure the overhead of the main data paths +for the TestStream. Specifically new elements, watermark changes and processing +time advances. + +This runs a series of N parallel pipelines with M parallel stages each. Each +stage does the following: + +1) Put all the PCollection elements in state +2) Set a timer for the future +3) When the timer fires, change the key and output all the elements downstream + +This executes the same codepaths that are run on the Fn API (and Dataflow) +workers, but is generally easier to run (locally) and more stable.. + +Run as + + python -m apache_beam.tools.teststream_microbenchmark + +""" + +# pytype: skip-file + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import argparse +import itertools +import logging +import random +from builtins import range + +import apache_beam as beam +import apache_beam.typehints.typehints as typehints +from apache_beam import WindowInto +from apache_beam.runners import DirectRunner +from apache_beam.testing.test_stream import TestStream +from apache_beam.tools import utils +from apache_beam.transforms.window import FixedWindows + +NUM_PARALLEL_STAGES = 7 + +NUM_SERIAL_STAGES = 6 + + +class RekeyElements(beam.DoFn): + def process(self, element): + _, values = element + return [(random.randint(0, 1000), v) for v in values] + + +def _build_serial_stages(input_pc, num_serial_stages, stage_count): + pc = (input_pc | ('gbk_start_stage%s' % stage_count) >> beam.GroupByKey()) + + for i in range(num_serial_stages): + pc = ( + pc + | ('stage%s_map%s' % (stage_count, i)) >> beam.ParDo( + RekeyElements()).with_output_types(typehints.KV[int, int]) + | ('stage%s_gbk%s' % (stage_count, i)) >> beam.GroupByKey()) + + return pc + + +def run_single_pipeline(size): + def _pipeline_runner(): + with beam.Pipeline(runner=DirectRunner()) as p: + ts = TestStream().advance_watermark_to(0) + all_elements = iter(range(size)) + watermark = 0 + while True: + next_batch = list(itertools.islice(all_elements, 100)) + if not next_batch: + break + ts = ts.add_elements([(i, random.randint(0, 1000)) for i in next_batch]) + watermark = watermark + 100 + ts = ts.advance_watermark_to(watermark) + ts = ts.advance_watermark_to_infinity() + + input_pc = p | ts | WindowInto(FixedWindows(100)) + for i in range(NUM_PARALLEL_STAGES): + _build_serial_stages(input_pc, NUM_SERIAL_STAGES, i) + + return _pipeline_runner + + +def run_benchmark( + starting_point=1, num_runs=10, num_elements_step=300, verbose=True): + suite = [ + utils.LinearRegressionBenchmarkConfig( Review comment: This runs a pipeline with 1 element, then 300, then 600, and so on until 3000. We use the linear regression to measure per-element overhead, and base overhead of the runner, and report it. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
