Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/sharedtest/custom_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

const (
SmallNumberOfCustomAttributes = 2 //nolint:revive
LargeNumberOfCustomAttributes = 20 //nolint:revive
SmallNumberOfCustomAttributes = 2 //nolint:revive
MiddleNumberOfCustomAttributes = 10 //nolint:revive
LargeNumberOfCustomAttributes = 50
)

type NameAndLDValue struct { //nolint:revive
Expand Down
26 changes: 25 additions & 1 deletion ldcontext/builder_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func BenchmarkBuildWithNoCustomAttrs(b *testing.B) {
}

func BenchmarkBuildWithCustomAttributes(b *testing.B) {
for _, n := range []int{sharedtest.SmallNumberOfCustomAttributes, sharedtest.LargeNumberOfCustomAttributes} {
for _, n := range []int{
sharedtest.SmallNumberOfCustomAttributes,
sharedtest.MiddleNumberOfCustomAttributes,
sharedtest.LargeNumberOfCustomAttributes,
} {
b.Run(fmt.Sprintf("with %d attributes", n), func(b *testing.B) {
attrs := sharedtest.MakeCustomAttributeNamesAndValues(n)
b.ResetTimer()
Expand All @@ -46,6 +50,26 @@ func BenchmarkBuildWithCustomAttributes(b *testing.B) {
}
}

func BenchmarkBuildWithCustomAttributesWithCapacity(b *testing.B) {
for _, n := range []int{
sharedtest.SmallNumberOfCustomAttributes,
sharedtest.MiddleNumberOfCustomAttributes,
sharedtest.LargeNumberOfCustomAttributes,
} {
b.Run(fmt.Sprintf("with %d attributes", n), func(b *testing.B) {
attrs := sharedtest.MakeCustomAttributeNamesAndValues(n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
builder := NewBuilderWithCapacity("key", n)
for _, a := range attrs {
builder.SetValue(a.Name, a.Value)
}
benchmarkContext = builder.Build()
}
})
}
}

func BenchmarkBuildWithPrivate(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkContext = NewBuilder("key").Name("name").Private("name").Build()
Expand Down
8 changes: 8 additions & 0 deletions ldcontext/builder_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ func NewBuilder(key string) *Builder {
return b.Key(key)
}

// NewBuilderWithCapacity extends the behavior of NewBuilder to pre-allocate capacity for attributes.
func NewBuilderWithCapacity(key string, capacity int) *Builder {
b := &Builder{
attributes: *ldvalue.ValueMapBuildWithCapacity(capacity),
}
return b.Key(key)
}

// NewBuilderFromContext creates a Builder whose properties are the same as an existing
// single context.
//
Expand Down