...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldcontext/context_benchmark_test.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/ldcontext

     1  package ldcontext
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/launchdarkly/go-sdk-common/v3/internal/sharedtest"
     8  	"github.com/launchdarkly/go-sdk-common/v3/ldattr"
     9  	"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
    10  )
    11  
    12  // If a benchmark's name ends in NoAlloc, our CI will enforce that it does not cause any heap allocations.
    13  
    14  func BenchmarkContextMinimumAllocationSize(b *testing.B) {
    15  	// This just measures the minimum number of bytes taken up by a Context.
    16  	for i := 0; i < b.N; i++ {
    17  		benchmarkContextPtr = makeEmptyContextPointer()
    18  	}
    19  }
    20  
    21  func makeEmptyContextPointer() *Context {
    22  	return &Context{}
    23  }
    24  
    25  func BenchmarkContextGetCustomAttrNoAlloc(b *testing.B) {
    26  	for _, n := range []int{sharedtest.SmallNumberOfCustomAttributes, sharedtest.LargeNumberOfCustomAttributes} {
    27  		b.Run(fmt.Sprintf("with %d attributes", n), func(b *testing.B) {
    28  			builder := NewBuilder("key")
    29  			attrs := sharedtest.MakeCustomAttributeNamesAndValues(n)
    30  			for _, a := range attrs {
    31  				builder.SetValue(a.Name, a.Value)
    32  			}
    33  			c := builder.Build()
    34  			lastAttr := attrs[n-1].Name
    35  			b.ResetTimer()
    36  			for i := 0; i < b.N; i++ {
    37  				benchmarkValue = c.GetValue(lastAttr)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  func BenchmarkContextGetCustomAttrNestedPropertyNoAlloc(b *testing.B) {
    44  	targetValue := ldvalue.String("17 Highbrow Street")
    45  	objectValue := ldvalue.ObjectBuild().Set("street", ldvalue.ObjectBuild().Set("line1", targetValue).Build()).Build()
    46  	c := makeBasicBuilder().SetValue("address", objectValue)
    47  	attrRef := ldattr.NewRef("/address/street/line1")
    48  	b.ResetTimer()
    49  	for i := 0; i < b.N; i++ {
    50  		benchmarkValue = c.Build().GetValueForRef(attrRef)
    51  	}
    52  }
    53  

View as plain text