github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/pkg/field/fields_chain_test.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  // Copyright (c) Facebook, Inc. and its affiliates.
    14  //
    15  // This source code is licensed under the MIT license found in the
    16  // LICENSE file in the root directory of this source tree.
    17  
    18  package field
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  var (
    28  	testContextField = &FieldsChain{
    29  		parent: &FieldsChain{
    30  			parent: &FieldsChain{
    31  				oneField: Field{
    32  					Key:   "5",
    33  					Value: 6,
    34  				},
    35  			},
    36  			multipleFields: Fields{{
    37  				Key:   "3",
    38  				Value: 4,
    39  			}},
    40  		},
    41  		oneField: Field{
    42  			Key:   "1",
    43  			Value: 2,
    44  		},
    45  	}
    46  
    47  	_v *FieldsChain
    48  )
    49  
    50  func dummyFields(count uint) Fields {
    51  	result := make(Fields, count)
    52  	for idx := range result {
    53  		result[idx] = Field{Key: fmt.Sprintf("%d", idx), Value: idx}
    54  	}
    55  	return result
    56  }
    57  
    58  func BenchmarkFields_CopyAndAddOne(b *testing.B) {
    59  	fields := Gather(testContextField)
    60  	for _, cloneDepth := range []uint{1, 2, 4, 8, 16, 32, 64} {
    61  		_fields := dummyFields(cloneDepth)
    62  		b.Run(fmt.Sprintf("cloneDepth%d", cloneDepth), func(b *testing.B) {
    63  			initialFields := fields.Copy()
    64  			b.ReportAllocs()
    65  			b.ResetTimer()
    66  			for i := 0; i < b.N; i++ {
    67  				f := initialFields
    68  				for i := 0; i < int(cloneDepth); i++ {
    69  					f = f.Copy()
    70  					f = append(f, _fields[i])
    71  				}
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func BenchmarkFieldsChain_CloneAndAddOneAnd(b *testing.B) {
    78  	// Last benchmark: BenchmarkFieldsChain_CloneAndAddOneAnd/cloneDepth1/withGather-false-16  40.44 ns/op  96 B/op  1 allocs/op
    79  	//
    80  	// This allocation and CPU consumption could be reduced via sync.Pool,
    81  	// but for now it is a premature optimization (there was no request on
    82  	// such performance).
    83  	for _, cloneDepth := range []uint{1, 2, 4, 8, 16, 32, 64} {
    84  		keys := make([]string, cloneDepth)
    85  		for idx := range keys {
    86  			keys[idx] = fmt.Sprintf("%d", idx)
    87  		}
    88  		b.Run(fmt.Sprintf("cloneDepth%d", cloneDepth), func(b *testing.B) {
    89  			for _, withGather := range []bool{false, true} {
    90  				b.Run(fmt.Sprintf("withGather-%v", withGather), func(b *testing.B) {
    91  					b.ReportAllocs()
    92  					b.ResetTimer()
    93  					for i := 0; i < b.N; i++ {
    94  						f := testContextField
    95  						for i := 0; i < int(cloneDepth); i++ {
    96  							f = f.WithField(keys[i], i)
    97  						}
    98  						if withGather {
    99  							_ = Gather(f)
   100  						}
   101  					}
   102  				})
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func BenchmarkFieldsChain_CloneAndAddOneAsMultiple(b *testing.B) {
   109  	fields := Fields{{
   110  		Key:   "1",
   111  		Value: "2",
   112  	}}
   113  	b.ReportAllocs()
   114  	b.ResetTimer()
   115  	for i := 0; i < b.N; i++ {
   116  		_v = testContextField.WithFields(fields)
   117  	}
   118  }
   119  
   120  func BenchmarkFieldsChain_CloneAndAddXAsMap(b *testing.B) {
   121  	for x := 1; x < 32; x++ {
   122  		b.Run(fmt.Sprintf("%d", x), func(b *testing.B) {
   123  			m := map[Key]interface{}{}
   124  			for i := 0; i < x; i++ {
   125  				m[fmt.Sprintf("%d", i)] = i
   126  			}
   127  			b.ReportAllocs()
   128  			b.ResetTimer()
   129  			for i := 0; i < b.N; i++ {
   130  				_v = testContextField.WithMap(m)
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func BenchmarkFieldsChain_Gather(b *testing.B) {
   137  	// Last benchmark: BenchmarkFieldsChain_Compile-8  4787438  251 ns/op  336 B/op  2 allocs
   138  	//
   139  	// Could be performed the same optimization as for BenchmarkFieldsChain_CloneAndAddOne
   140  	b.ReportAllocs()
   141  	b.ResetTimer()
   142  	for i := 0; i < b.N; i++ {
   143  		_ = Gather(testContextField)
   144  	}
   145  }
   146  
   147  func TestFieldsChain_Compile(t *testing.T) {
   148  	require.Equal(t, Fields{
   149  		{
   150  			Key:   "1",
   151  			Value: 2,
   152  		},
   153  		Field{
   154  			Key:   "3",
   155  			Value: 4,
   156  		},
   157  		Field{
   158  			Key:   "5",
   159  			Value: 6,
   160  		},
   161  	}, Gather(testContextField))
   162  }