github.com/apache/beam/sdks/v2@v2.48.2/go/test/load/pardo/pardo.go (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 package main 17 18 import ( 19 "context" 20 "flag" 21 "fmt" 22 23 "github.com/apache/beam/sdks/v2/go/pkg/beam" 24 "github.com/apache/beam/sdks/v2/go/pkg/beam/io/synthetic" 25 "github.com/apache/beam/sdks/v2/go/pkg/beam/log" 26 "github.com/apache/beam/sdks/v2/go/pkg/beam/register" 27 "github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx" 28 "github.com/apache/beam/sdks/v2/go/test/load" 29 ) 30 31 var ( 32 iterations = flag.Int( 33 "iterations", 34 1, 35 "A number of subsequent ParDo transforms to be performed.") 36 numCounters = flag.Int( 37 "number_of_counters", 38 0, 39 "A number of counter metrics to be created.") 40 operations = flag.Int( 41 "number_of_counter_operations", 42 0, 43 "A number of increments to be performed for each counter.") 44 syntheticConfig = flag.String( 45 "input_options", 46 "", 47 "A JSON object that describes the configuration for synthetic source.") 48 ) 49 50 func init() { 51 register.DoFn4x0[context.Context, []byte, []byte, func([]byte, []byte)]((*counterOperationFn)(nil)) 52 register.Emitter2[[]byte, []byte]() 53 } 54 55 type counterOperationFn struct { 56 Operations, NumCounters int 57 counters []beam.Counter 58 } 59 60 func newCounterOperationFn(operations, numCounters int) *counterOperationFn { 61 return &counterOperationFn{ 62 Operations: operations, 63 NumCounters: numCounters, 64 } 65 } 66 67 func (fn *counterOperationFn) Setup() { 68 fn.counters = make([]beam.Counter, fn.NumCounters) 69 for i := 0; i < fn.NumCounters; i++ { 70 fn.counters[i] = beam.NewCounter("counterOperationFn", fmt.Sprint("counter-", i)) 71 } 72 } 73 74 func (fn *counterOperationFn) ProcessElement(ctx context.Context, key []byte, value []byte, emit func([]byte, []byte)) { 75 for i := 0; i < fn.Operations; i++ { 76 for _, counter := range fn.counters { 77 counter.Inc(ctx, 1) 78 } 79 } 80 emit(key, value) 81 } 82 83 func parseSyntheticConfig() synthetic.SourceConfig { 84 if *syntheticConfig == "" { 85 panic("--input_options not provided") 86 } else { 87 encoded := []byte(*syntheticConfig) 88 return synthetic.DefaultSourceConfig().BuildFromJSON(encoded) 89 } 90 } 91 92 func main() { 93 flag.Parse() 94 beam.Init() 95 ctx := context.Background() 96 97 p, s := beam.NewPipelineWithRoot() 98 99 src := synthetic.SourceSingle(s, parseSyntheticConfig()) 100 101 pcoll := beam.ParDo(s, &load.RuntimeMonitor{}, src) 102 103 for i := 0; i < *iterations; i++ { 104 pcoll = beam.ParDo(s, newCounterOperationFn(*operations, *numCounters), pcoll) 105 } 106 107 presult, err := beamx.RunWithMetrics(ctx, p) 108 if err != nil { 109 log.Fatalf(ctx, "Failed to execute job: %v", err) 110 } 111 112 if presult != nil { 113 metrics := presult.Metrics().AllMetrics() 114 load.PublishMetrics(metrics) 115 } 116 }