github.com/Jeffail/benthos/v3@v3.65.0/internal/integration/stream_benchmark_definitions.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // StreamBenchSend benchmarks the speed at which messages are sent over the
    14  // templated output and then subsequently received from the input with a given
    15  // batch size and parallelism.
    16  func StreamBenchSend(batchSize, parallelism int) StreamBenchDefinition {
    17  	return namedBench(
    18  		fmt.Sprintf("send message batches %v with parallelism %v", batchSize, parallelism),
    19  		func(b *testing.B, env *streamTestEnvironment) {
    20  			require.Greater(b, parallelism, 0)
    21  
    22  			tranChan := make(chan types.Transaction)
    23  			input, output := initConnectors(b, tranChan, env)
    24  			b.Cleanup(func() {
    25  				closeConnectors(b, input, output)
    26  			})
    27  
    28  			sends := b.N / batchSize
    29  
    30  			set := map[string][]string{}
    31  			for j := 0; j < sends; j++ {
    32  				for i := 0; i < batchSize; i++ {
    33  					payload := fmt.Sprintf("hello world %v", j*sends+i)
    34  					set[payload] = nil
    35  				}
    36  			}
    37  
    38  			b.ResetTimer()
    39  
    40  			batchChan := make(chan []string)
    41  
    42  			var wg sync.WaitGroup
    43  			for k := 0; k < parallelism; k++ {
    44  				wg.Add(1)
    45  				go func() {
    46  					defer wg.Done()
    47  					for {
    48  						batch, open := <-batchChan
    49  						if !open {
    50  							return
    51  						}
    52  						assert.NoError(b, sendBatch(env.ctx, b, tranChan, batch))
    53  					}
    54  				}()
    55  			}
    56  
    57  			wg.Add(1)
    58  			go func() {
    59  				defer wg.Done()
    60  				for len(set) > 0 {
    61  					messageInSet(b, true, true, receiveMessage(env.ctx, b, input.TransactionChan(), nil), set)
    62  				}
    63  			}()
    64  
    65  			for j := 0; j < sends; j++ {
    66  				payloads := []string{}
    67  				for i := 0; i < batchSize; i++ {
    68  					payload := fmt.Sprintf("hello world %v", j*sends+i)
    69  					payloads = append(payloads, payload)
    70  				}
    71  				batchChan <- payloads
    72  			}
    73  			close(batchChan)
    74  
    75  			wg.Wait()
    76  		},
    77  	)
    78  }
    79  
    80  // StreamBenchWrite benchmarks the speed at which messages can be written to the
    81  // output, with no attempt made to consume the written data.
    82  func StreamBenchWrite(batchSize int) StreamBenchDefinition {
    83  	return namedBench(
    84  		fmt.Sprintf("write message batches %v without reading", batchSize),
    85  		func(b *testing.B, env *streamTestEnvironment) {
    86  			tranChan := make(chan types.Transaction)
    87  			output := initOutput(b, tranChan, env)
    88  			b.Cleanup(func() {
    89  				closeConnectors(b, nil, output)
    90  			})
    91  
    92  			sends := b.N / batchSize
    93  
    94  			b.ResetTimer()
    95  
    96  			batch := make([]string, batchSize)
    97  			for j := 0; j < sends; j++ {
    98  				for i := 0; i < batchSize; i++ {
    99  					batch[i] = fmt.Sprintf(`{"content":"hello world","id":%v}`, j*sends+i)
   100  				}
   101  				assert.NoError(b, sendBatch(env.ctx, b, tranChan, batch))
   102  			}
   103  		},
   104  	)
   105  }