github.com/Jeffail/benthos/v3@v3.65.0/public/service/tracing_test.go (about) 1 package service_test 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "testing" 8 9 "github.com/Jeffail/benthos/v3/public/service" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 _ "github.com/Jeffail/benthos/v3/public/components/all" 14 ) 15 16 func TestTracing(t *testing.T) { 17 config := ` 18 input: 19 generate: 20 count: 5 21 interval: 1us 22 mapping: | 23 root.id = count("counting the number of messages in my tracing test") 24 25 pipeline: 26 processors: 27 - bloblang: | 28 root.count = if this.id % 2 == 0 { throw("nah %v".format(this.id)) } else { this.id } 29 30 output: 31 drop: {} 32 33 logger: 34 level: OFF 35 ` 36 37 strmBuilder := service.NewStreamBuilder() 38 require.NoError(t, strmBuilder.SetYAML(config)) 39 40 strm, trace, err := strmBuilder.BuildTraced() 41 require.NoError(t, err) 42 43 require.NoError(t, strm.Run(context.Background())) 44 45 assert.Equal(t, 5, int(trace.TotalInput())) 46 assert.Equal(t, 5, int(trace.TotalOutput())) 47 assert.Equal(t, 2, int(trace.TotalProcessorErrors())) 48 49 assert.Equal(t, map[string][]service.TracingEvent{ 50 "input": { 51 {Type: service.TracingEventProduce, Content: `{"id":1}`}, 52 {Type: service.TracingEventProduce, Content: `{"id":2}`}, 53 {Type: service.TracingEventProduce, Content: `{"id":3}`}, 54 {Type: service.TracingEventProduce, Content: `{"id":4}`}, 55 {Type: service.TracingEventProduce, Content: `{"id":5}`}, 56 }, 57 }, trace.InputEvents()) 58 59 assert.Equal(t, map[string][]service.TracingEvent{ 60 "pipeline.processor.0": { 61 {Type: service.TracingEventConsume, Content: `{"id":1}`}, 62 {Type: service.TracingEventProduce, Content: `{"count":1}`}, 63 {Type: service.TracingEventConsume, Content: `{"id":2}`}, 64 {Type: service.TracingEventProduce, Content: `{"id":2}`}, 65 {Type: service.TracingEventError, Content: `failed assignment (line 1): nah 2`}, 66 {Type: service.TracingEventConsume, Content: `{"id":3}`}, 67 {Type: service.TracingEventProduce, Content: `{"count":3}`}, 68 {Type: service.TracingEventConsume, Content: `{"id":4}`}, 69 {Type: service.TracingEventProduce, Content: `{"id":4}`}, 70 {Type: service.TracingEventError, Content: `failed assignment (line 1): nah 4`}, 71 {Type: service.TracingEventConsume, Content: `{"id":5}`}, 72 {Type: service.TracingEventProduce, Content: `{"count":5}`}, 73 }, 74 }, trace.ProcessorEvents()) 75 76 assert.Equal(t, map[string][]service.TracingEvent{ 77 "output": { 78 {Type: service.TracingEventConsume, Content: `{"count":1}`}, 79 {Type: service.TracingEventConsume, Content: `{"id":2}`}, 80 {Type: service.TracingEventConsume, Content: `{"count":3}`}, 81 {Type: service.TracingEventConsume, Content: `{"id":4}`}, 82 {Type: service.TracingEventConsume, Content: `{"count":5}`}, 83 }, 84 }, trace.OutputEvents()) 85 } 86 87 func BenchmarkStreamTracing(b *testing.B) { 88 config := ` 89 input: 90 generate: 91 count: 5 92 interval: "" 93 mapping: | 94 root.id = uuid_v4() 95 96 pipeline: 97 processors: 98 - bloblang: 'root = this' 99 100 output: 101 drop: {} 102 103 logger: 104 level: OFF 105 ` 106 107 strmBuilder := service.NewStreamBuilder() 108 strmBuilder.SetHTTPMux(disabledMux{}) 109 require.NoError(b, strmBuilder.SetYAML(config)) 110 111 b.ResetTimer() 112 b.ReportAllocs() 113 114 for i := 0; i < b.N; i++ { 115 strm, trace, err := strmBuilder.BuildTraced() 116 require.NoError(b, err) 117 118 require.NoError(b, strm.Run(context.Background())) 119 120 assert.Equal(b, 5, int(trace.TotalInput())) 121 assert.Equal(b, 5, int(trace.TotalOutput())) 122 assert.Equal(b, 0, int(trace.TotalProcessorErrors())) 123 } 124 } 125 126 func BenchmarkStreamTracingOutputN1(b *testing.B) { 127 benchmarkStreamTracingOutputNX(b, 1) 128 } 129 130 func BenchmarkStreamTracingOutputN10(b *testing.B) { 131 benchmarkStreamTracingOutputNX(b, 10) 132 } 133 134 func BenchmarkStreamTracingOutputN100(b *testing.B) { 135 benchmarkStreamTracingOutputNX(b, 100) 136 } 137 138 func benchmarkStreamTracingOutputNX(b *testing.B, size int) { 139 var outputsBuf bytes.Buffer 140 for i := 0; i < size; i++ { 141 outputsBuf.WriteString(" - custom: {}\n") 142 } 143 144 config := fmt.Sprintf(` 145 input: 146 generate: 147 count: 5 148 interval: "" 149 mapping: | 150 root.id = uuid_v4() 151 152 pipeline: 153 processors: 154 - bloblang: 'root = this' 155 156 output: 157 broker: 158 outputs: 159 %v 160 161 logger: 162 level: OFF 163 `, outputsBuf.String()) 164 165 env := service.NewEnvironment() 166 require.NoError(b, env.RegisterOutput( 167 "custom", 168 service.NewConfigSpec(), 169 func(conf *service.ParsedConfig, mgr *service.Resources) (out service.Output, maxInFlight int, err error) { 170 return &noopOutput{}, 1, nil 171 }, 172 )) 173 174 strmBuilder := env.NewStreamBuilder() 175 strmBuilder.SetHTTPMux(disabledMux{}) 176 require.NoError(b, strmBuilder.SetYAML(config)) 177 178 b.ResetTimer() 179 b.ReportAllocs() 180 181 for i := 0; i < b.N; i++ { 182 strm, trace, err := strmBuilder.BuildTraced() 183 require.NoError(b, err) 184 185 require.NoError(b, strm.Run(context.Background())) 186 187 assert.Equal(b, 5, int(trace.TotalInput())) 188 } 189 }