Skip to content

Commit

Permalink
test: benchmark CompareJSON against Compare
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Mar 25, 2022
1 parent fdd6214 commit b393df1
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package jsondiff

import (
"encoding/json"
"io/ioutil"
"testing"
)

func BenchmarkCompareJSONOpts(b *testing.B) {
func BenchmarkCompare(b *testing.B) {
beforeBytes, err := ioutil.ReadFile("testdata/benchs/before.json")
if err != nil {
b.Fatal(err)
}
var before interface{}
err = json.Unmarshal(beforeBytes, &before)
if err != nil {
b.Fatal(err)
}
afterBytes, err := ioutil.ReadFile("testdata/benchs/after.json")
if err != nil {
b.Fatal(err)
}
var after interface{}
err = json.Unmarshal(afterBytes, &after)
if err != nil {
b.Fatal(err)
}
makeopts := func(opts ...Option) []Option { return opts }

for _, bb := range []struct {
Expand All @@ -27,7 +38,16 @@ func BenchmarkCompareJSONOpts(b *testing.B) {
{"factor+ratio", makeopts(Factorize(), Rationalize())},
{"all-options", makeopts(Factorize(), Rationalize(), Invertible())},
} {
b.Run(bb.name, func(b *testing.B) {
b.Run("Compare/"+bb.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
patch, err := CompareOpts(before, after, bb.opts...)
if err != nil {
b.Error(err)
}
_ = patch
}
})
b.Run("CompareJSON/"+bb.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
patch, err := CompareJSONOpts(beforeBytes, afterBytes, bb.opts...)
if err != nil {
Expand Down

0 comments on commit b393df1

Please sign in to comment.