...

Source file src/gomodules.xyz/jsonpatch/v2/bench_test.go

Documentation: gomodules.xyz/jsonpatch/v2

     1  package jsonpatch_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"gomodules.xyz/jsonpatch/v2"
     8  )
     9  
    10  func BenchmarkCreatePatch(b *testing.B) {
    11  	cases := []struct {
    12  		name string
    13  		a, b string
    14  	}{
    15  		{
    16  			"complex",
    17  			superComplexBase,
    18  			superComplexA,
    19  		},
    20  		{
    21  			"large array",
    22  			largeArray(1000, "a"),
    23  			largeArray(1000, "b"),
    24  		},
    25  		{
    26  			"simple",
    27  			simpleA,
    28  			simpleB,
    29  		},
    30  	}
    31  
    32  	for _, tt := range cases {
    33  		b.Run(tt.name, func(b *testing.B) {
    34  			at := []byte(tt.a)
    35  			bt := []byte(tt.b)
    36  			for n := 0; n < b.N; n++ {
    37  				_, _ = jsonpatch.CreatePatch(at, bt)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  func largeArray(size int, val string) string {
    44  	type nested struct {
    45  		A, B string
    46  	}
    47  	type example struct {
    48  		Objects []nested
    49  	}
    50  	a := example{}
    51  	for i := 0; i < size; i++ {
    52  		a.Objects = append(a.Objects, nested{A: "a", B: val})
    53  	}
    54  	res, _ := json.Marshal(a)
    55  	return string(res)
    56  }
    57  

View as plain text