github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/msg/producer/writer/message_writer_benchmark_test.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package writer
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/msg/producer"
    27  	"github.com/m3db/m3/src/x/instrument"
    28  )
    29  
    30  func BenchmarkScanMessageQueue(b *testing.B) {
    31  	opts := testOptions().
    32  		SetMessageQueueScanBatchSize(128).
    33  		SetInstrumentOptions(
    34  			instrument.NewOptions().SetTimerOptions(
    35  				instrument.TimerOptions{
    36  					Type:               instrument.StandardTimerType,
    37  					StandardSampleRate: 0.05,
    38  				},
    39  			),
    40  		)
    41  
    42  	b.RunParallel(func(pb *testing.PB) {
    43  		w := newMessageWriter(
    44  			200,
    45  			newMessagePool(),
    46  			opts,
    47  			testMessageWriterMetrics(),
    48  		)
    49  
    50  		w.consumerWriters = append(w.consumerWriters, noopWriter{})
    51  
    52  		for i := 0; i < 1024; i++ {
    53  			w.Write(producer.NewRefCountedMessage(emptyMessage{}, nil))
    54  		}
    55  		b.ResetTimer()
    56  
    57  		for pb.Next() {
    58  			w.scanMessageQueue()
    59  			if w.QueueSize() != 1024 {
    60  				b.Fatalf("expected queue len to be 1024, got %v", w.QueueSize())
    61  			}
    62  		}
    63  	})
    64  }
    65  
    66  type noopWriter struct{}
    67  
    68  func (noopWriter) Address() string         { return "" }
    69  func (noopWriter) Write(int, []byte) error { return nil }
    70  func (noopWriter) Init()                   {}
    71  func (noopWriter) Close()                  {}
    72  
    73  func BenchmarkAck(b *testing.B) {
    74  	ack := newAckHelper(64)
    75  	for i := 0; i < b.N; i++ {
    76  		meta := metadata{metadataKey: metadataKey{200, uint64(b.N)}}
    77  		ack.add(meta, nil)
    78  		ack.remove(meta)
    79  	}
    80  }