github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/archive/benchmarks_test.go (about)

     1  // Copyright (C) 2021-2022 Talos, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package archive
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/lirm/aeron-go/aeron/atomic"
    22  	"github.com/lirm/aeron-go/archive/codecs"
    23  )
    24  
    25  // check to see if this is worth avoiding in the fragment handlers
    26  // which are called quite frequently
    27  func BenchmarkCodecObjectCreation(b *testing.B) {
    28  	var recordingStarted *codecs.RecordingStarted
    29  	for n := 0; n < b.N; n++ {
    30  		recordingStarted = new(codecs.RecordingStarted)
    31  	}
    32  	// declared but not used
    33  	if recordingStarted == nil {
    34  		b.Logf("all done")
    35  	}
    36  }
    37  
    38  // Benchmark the Descriptor Fragment Handler using a recording descriptor as the example.
    39  // Used to evaluate whether optimizations are worthwhile here
    40  func BenchmarkDescriptorFragmentHandler(b *testing.B) {
    41  	rd := codecs.RecordingDescriptor{
    42  		ControlSessionId:  1,
    43  		CorrelationId:     2,
    44  		RecordingId:       3,
    45  		StartTimestamp:    4,
    46  		StopTimestamp:     5,
    47  		StartPosition:     6,
    48  		StopPosition:      7,
    49  		InitialTermId:     8,
    50  		SegmentFileLength: 9,
    51  		TermBufferLength:  10,
    52  		MtuLength:         11,
    53  		SessionId:         12,
    54  		StreamId:          13,
    55  		StrippedChannel:   []uint8("stripped"),
    56  		OriginalChannel:   []uint8("original"),
    57  		SourceIdentity:    []uint8("source")}
    58  
    59  	marshaller := codecs.NewSbeGoMarshaller()
    60  	buffer := new(bytes.Buffer)
    61  
    62  	header := codecs.MessageHeader{BlockLength: rd.SbeBlockLength(), TemplateId: rd.SbeTemplateId(), SchemaId: rd.SbeSchemaId(), Version: rd.SbeSchemaVersion()}
    63  
    64  	if err := header.Encode(marshaller, buffer); err != nil {
    65  		b.Logf("header encode failed")
    66  		b.FailNow()
    67  	}
    68  	if err := rd.Encode(marshaller, buffer, rangeChecking); err != nil {
    69  		b.Logf("header encode failed")
    70  		b.FailNow()
    71  	}
    72  
    73  	bytes := buffer.Bytes()
    74  	length := int32(len(bytes))
    75  	atomicbuffer := atomic.MakeBuffer(bytes, len(bytes))
    76  
    77  	// Mock the correlationId to Control map
    78  	correlations.Store(rd.CorrelationId, new(Control))
    79  
    80  	for n := 0; n < b.N; n++ {
    81  		DescriptorFragmentHandler(archive.SessionID, atomicbuffer, 0, length, nil)
    82  	}
    83  
    84  }