...

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

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

     1  package jwriter
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/launchdarkly/go-jsonstream/v3/internal/commontest"
     8  )
     9  
    10  func BenchmarkWriteNull(b *testing.B) {
    11  	expected := []byte("null")
    12  	b.ResetTimer()
    13  	for i := 0; i < b.N; i++ {
    14  		w := NewWriter()
    15  		w.Null()
    16  		benchmarkExpectWriterOutput(b, &w, expected)
    17  	}
    18  }
    19  
    20  func BenchmarkWriteBoolean(b *testing.B) {
    21  	expected := []byte("true")
    22  	b.ResetTimer()
    23  	for i := 0; i < b.N; i++ {
    24  		w := NewWriter()
    25  		w.Bool(true)
    26  		benchmarkExpectWriterOutput(b, &w, expected)
    27  	}
    28  }
    29  
    30  func BenchmarkWriteNumberInt(b *testing.B) {
    31  	expected := []byte("123")
    32  	b.ResetTimer()
    33  	for i := 0; i < b.N; i++ {
    34  		w := NewWriter()
    35  		w.Int(123)
    36  		benchmarkExpectWriterOutput(b, &w, expected)
    37  	}
    38  }
    39  
    40  func BenchmarkWriteNumberFloat(b *testing.B) {
    41  	expected := []byte("1234.5")
    42  	b.ResetTimer()
    43  	for i := 0; i < b.N; i++ {
    44  		w := NewWriter()
    45  		w.Float64(1234.5)
    46  		benchmarkExpectWriterOutput(b, &w, expected)
    47  	}
    48  }
    49  
    50  func BenchmarkWriteString(b *testing.B) {
    51  	expected := []byte(`"abc"`)
    52  	b.ResetTimer()
    53  	for i := 0; i < b.N; i++ {
    54  		w := NewWriter()
    55  		w.String("abc")
    56  		benchmarkExpectWriterOutput(b, &w, expected)
    57  	}
    58  }
    59  
    60  func BenchmarkWriteArrayOfBools(b *testing.B) {
    61  	vals := commontest.MakeBools()
    62  	expected := commontest.MakeBoolsJSON(vals)
    63  	b.ResetTimer()
    64  	for i := 0; i < b.N; i++ {
    65  		w := NewWriter()
    66  		arr := w.Array()
    67  		for _, val := range vals {
    68  			arr.Bool(val)
    69  		}
    70  		arr.End()
    71  		benchmarkExpectWriterOutput(b, &w, expected)
    72  	}
    73  }
    74  
    75  func BenchmarkWriteArrayOfStrings(b *testing.B) {
    76  	vals := commontest.MakeStrings()
    77  	expected := commontest.MakeStringsJSON(vals)
    78  	b.ResetTimer()
    79  	for i := 0; i < b.N; i++ {
    80  		w := NewWriter()
    81  		arr := w.Array()
    82  		for _, val := range vals {
    83  			arr.String(val)
    84  		}
    85  		arr.End()
    86  		benchmarkExpectWriterOutput(b, &w, expected)
    87  	}
    88  }
    89  
    90  func BenchmarkWriteObject(b *testing.B) {
    91  	for i := 0; i < b.N; i++ {
    92  		w := NewWriter()
    93  		ExampleStructWrapper(commontest.ExampleStructValue).WriteToJSONWriter(&w)
    94  		benchmarkExpectWriterOutput(b, &w, commontest.ExampleStructData)
    95  	}
    96  }
    97  
    98  func benchmarkExpectWriterOutput(b *testing.B, w *Writer, expectedJSON []byte) {
    99  	if err := w.Error(); err != nil {
   100  		b.Error(err)
   101  		b.FailNow()
   102  	}
   103  	if !bytes.Equal(expectedJSON, w.Bytes()) {
   104  		b.FailNow()
   105  	}
   106  }
   107  
   108  func BenchmarkWriteObjectToNoOpWriterNoAllocs(b *testing.B) {
   109  	// The purpose of this benchmark is to ensure that nothing is escaping to the heap simply
   110  	// as result of calling the Name or Maybe methods (as it might if we hadn't been
   111  	// careful about our use of pointers). We're preinitializing the Writer to already have an
   112  	// error, so it won't produce any output.
   113  	w := NewWriter()
   114  	obj := w.Object()
   115  	w.AddError(noOpWriterError{})
   116  	b.ResetTimer()
   117  	for i := 0; i < b.N; i++ {
   118  		obj.Name("prop1").Int(1)
   119  		obj.Maybe("prop2", true).Int(2)
   120  		obj.Maybe("prop3", false).Int(3)
   121  	}
   122  }
   123  
   124  func BenchmarkStreamingWriterArrayOfStrings(b *testing.B) {
   125  	vals := commontest.MakeStrings()
   126  	expected := commontest.MakeStringsJSON(vals)
   127  	b.ResetTimer()
   128  	for i := 0; i < b.N; i++ {
   129  		var buf bytes.Buffer
   130  		w := NewStreamingWriter(&buf, 50)
   131  		arr := w.Array()
   132  		for _, val := range vals {
   133  			arr.String(val)
   134  		}
   135  		arr.End()
   136  		w.Flush()
   137  		output := buf.Bytes()
   138  		if !bytes.Equal(expected, output) {
   139  			b.FailNow()
   140  		}
   141  	}
   142  }
   143  

View as plain text