...

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

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

     1  package ldcontext
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // BenchmarkJSONMarshal uses json.Marshal; BenchmarkJSONStreamMarshal uses the jsonstream API via
     8  // WriteToJSONWriter. They both end up calling the same underlying logic, but json.Marshal has some
     9  // extra indirection.
    10  //
    11  // Marshaling via EasyJSON is covered in the conditionally-compiled file context_easyjson_benchmark_test.go.
    12  
    13  func doMarshalBenchmark(b *testing.B, marshalFn func(*Context) ([]byte, error)) {
    14  	for _, p := range makeBenchmarkMarshalTestParams() {
    15  		b.Run(p.name, func(b *testing.B) {
    16  			for i := 0; i < b.N; i++ {
    17  				if benchmarkJSONResult, benchmarkErr = marshalFn(&p.context); benchmarkErr != nil {
    18  					b.Fatal(benchmarkErr)
    19  				}
    20  			}
    21  		})
    22  	}
    23  }
    24  
    25  func BenchmarkJSONMarshal(b *testing.B) {
    26  	doMarshalBenchmark(b, jsonMarshalTestFn)
    27  }
    28  
    29  func BenchmarkJSONStreamMarshal(b *testing.B) {
    30  	doMarshalBenchmark(b, jsonStreamMarshalTestFn)
    31  }
    32  

View as plain text