github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/tools/sideinput_microbenchmark.py (about) 1 # Licensed to the Apache Software Foundation (ASF) under one or more 2 # contributor license agreements. See the NOTICE file distributed with 3 # this work for additional information regarding copyright ownership. 4 # The ASF licenses this file to You under the Apache License, Version 2.0 5 # (the "License"); you may not use this file except in compliance with 6 # the License. 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 """A microbenchmark for measuring performance of Side Inputs iterable. 18 19 Runs side input iterable that fetches from multiple FakeSources, and 20 measures the time to fetch all elements from all sources. 21 22 Run as 23 python -m apache_beam.tools.sideinput_microbenchmark 24 """ 25 26 # pytype: skip-file 27 28 import logging 29 import time 30 31 from apache_beam.runners.worker import opcounters 32 from apache_beam.runners.worker import sideinputs 33 from apache_beam.runners.worker import statesampler 34 from apache_beam.runners.worker.sideinputs_test import FakeSource 35 from apache_beam.tools import utils 36 from apache_beam.utils.counters import CounterFactory 37 38 39 def long_generator(value, elements): 40 for _ in range(elements): 41 yield value 42 43 44 def run_benchmark(num_runs=50, input_per_source=4000, num_sources=4): 45 print("Number of runs:", num_runs) 46 print("Input size:", num_sources * input_per_source) 47 print("Sources:", num_sources) 48 49 times = [] 50 for _ in range(num_runs): 51 counter_factory = CounterFactory() 52 state_sampler = statesampler.StateSampler('basic', counter_factory) 53 state_sampler.start() 54 with state_sampler.scoped_state('step1', 'state'): 55 si_counter = opcounters.SideInputReadCounter( 56 counter_factory, state_sampler, 'step1', 1) 57 si_counter = opcounters.NoOpTransformIOCounter() 58 sources = [ 59 FakeSource(long_generator(i, input_per_source)) 60 for i in range(num_sources) 61 ] 62 iterator_fn = sideinputs.get_iterator_fn_for_sources( 63 sources, read_counter=si_counter) 64 start = time.time() 65 list(iterator_fn()) 66 time_cost = time.time() - start 67 times.append(time_cost) 68 state_sampler.stop() 69 70 print("Runtimes:", times) 71 72 avg_runtime = sum(times) / len(times) 73 print("Average runtime:", avg_runtime) 74 print("Time per element:", avg_runtime / (input_per_source * num_sources)) 75 76 77 if __name__ == '__main__': 78 logging.basicConfig() 79 utils.check_compiled('apache_beam.runners.worker.opcounters') 80 run_benchmark()