...

Source file src/github.com/launchdarkly/go-jsonstream/v3/jwriter/json_marshal_comparative_benchmark_test.go

Documentation: github.com/launchdarkly/go-jsonstream/v3/jwriter

     1  package jwriter
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/launchdarkly/go-jsonstream/v3/internal/commontest"
     9  )
    10  
    11  // These benchmarks perform equivalent actions to the ones in writer_benchmark_test.go, but using
    12  // the default reflection-based mechanism from the json/encoding package, so we can see how much
    13  // less efficient that is than our default implementation and the easyjson implementation.
    14  
    15  func BenchmarkJSONMarshalComparatives(b *testing.B) {
    16  	b.Run("Null", benchmarkWriteNullJSONMarshal)
    17  	b.Run("Boolean", benchmarkWriteBooleanJSONMarshal)
    18  	b.Run("NumberInt", benchmarkWriteNumberIntJSONMarshal)
    19  	b.Run("NumberFloat", benchmarkWriteNumberFloatJSONMarshal)
    20  	b.Run("String", benchmarkWriteStringJSONMarshal)
    21  	b.Run("ArrayOfBools", benchmarkWriteArrayOfBoolsJSONMarshal)
    22  	b.Run("ArrayOfStrings", benchmarkWriteArrayOfStringsJSONMarshal)
    23  	b.Run("Object", benchmarkWriteObjectJSONMarshal)
    24  }
    25  
    26  func benchmarkWriteNullJSONMarshal(b *testing.B) {
    27  	expected := []byte("null")
    28  	b.ResetTimer()
    29  	for i := 0; i < b.N; i++ {
    30  		data, err := json.Marshal(nil)
    31  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    32  	}
    33  }
    34  
    35  func benchmarkWriteBooleanJSONMarshal(b *testing.B) {
    36  	expected := []byte("true")
    37  	b.ResetTimer()
    38  	for i := 0; i < b.N; i++ {
    39  		data, err := json.Marshal(true)
    40  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    41  	}
    42  }
    43  
    44  func benchmarkWriteNumberIntJSONMarshal(b *testing.B) {
    45  	expected := []byte("123")
    46  	b.ResetTimer()
    47  	for i := 0; i < b.N; i++ {
    48  		data, err := json.Marshal(123)
    49  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    50  	}
    51  }
    52  
    53  func benchmarkWriteNumberFloatJSONMarshal(b *testing.B) {
    54  	expected := []byte("1234.5")
    55  	b.ResetTimer()
    56  	for i := 0; i < b.N; i++ {
    57  		data, err := json.Marshal(1234.5)
    58  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    59  	}
    60  }
    61  
    62  func benchmarkWriteStringJSONMarshal(b *testing.B) {
    63  	expected := []byte(`"abc"`)
    64  	val := "abc"
    65  	b.ResetTimer()
    66  	for i := 0; i < b.N; i++ {
    67  		data, err := json.Marshal(val)
    68  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    69  	}
    70  }
    71  
    72  func benchmarkWriteArrayOfBoolsJSONMarshal(b *testing.B) {
    73  	vals := commontest.MakeBools()
    74  	expected := commontest.MakeBoolsJSON(vals)
    75  	b.ResetTimer()
    76  	for i := 0; i < b.N; i++ {
    77  		data, err := json.Marshal(vals)
    78  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    79  	}
    80  }
    81  
    82  func benchmarkWriteArrayOfStringsJSONMarshal(b *testing.B) {
    83  	vals := commontest.MakeStrings()
    84  	expected := commontest.MakeStringsJSON(vals)
    85  	b.ResetTimer()
    86  	for i := 0; i < b.N; i++ {
    87  		data, err := json.Marshal(vals)
    88  		benchmarkExpectJSONMarshalOutput(b, err, data, expected)
    89  	}
    90  }
    91  
    92  func benchmarkWriteObjectJSONMarshal(b *testing.B) {
    93  	for i := 0; i < b.N; i++ {
    94  		data, err := json.Marshal(ExampleStructWrapper(commontest.ExampleStructValue))
    95  		benchmarkExpectJSONMarshalOutput(b, err, data, commontest.ExampleStructData)
    96  	}
    97  }
    98  
    99  func benchmarkExpectJSONMarshalOutput(b *testing.B, err error, actualJSON []byte, expectedJSON []byte) {
   100  	if err != nil {
   101  		b.Error(err)
   102  		b.FailNow()
   103  	}
   104  	if !bytes.Equal(expectedJSON, actualJSON) {
   105  		b.FailNow()
   106  	}
   107  }
   108  

View as plain text