Skip to content

Commit

Permalink
bench: skip_fast vs skip_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Aug 26, 2024
1 parent 5672c89 commit 8a0e155
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package decoder

import (
"encoding/json"
"fmt"
"runtime"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -100,7 +101,7 @@ func BenchmarkSkipValidate(b *testing.B) {
expTime float64
}
var sam = map[int]interface{}{}
for i := 0; i < 10; i++ {
for i := 0; i < 1; i++ {
sam[i] = _BindingValue
}
comptd, err := json.Marshal(sam)
Expand All @@ -109,49 +110,47 @@ func BenchmarkSkipValidate(b *testing.B) {
}
compt := string(comptd)
var cases = []C{
{"mismatched", `{"a":`+compt+`}`, 2},
// {"ommited", `{"b":`+compt+`}`, 2},
{"fast int", `{"c":[`+strings.Repeat("-1.23456e-19,", 2000)+`1]}`, 1.2},
{"unknown", `{"unknown":`+compt+`}`, 2},
{"empty", `{"d":`+compt+`}`, 2},
{"mismatched elem", `{"e":`+compt+`}`, 2},
{"mismatched", `{"a":`+compt+`}`, 5},
{"ommited", `{"b":`+compt+`}`, 5},
{"number", `{"c":[`+strings.Repeat("-1.23456e-19,", 1000)+`1]}`, 1.5},
{"unknown", `{"unknown":`+compt+`}`, 5},
{"empty", `{"d":`+compt+`}`, 5},
{"mismatched elem", `{"e":`+compt+`}`, 5},
}
_ = NewDecoder(`{}`).Decode(&skiptype{})

var avg1, avg2 time.Duration
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
b.Run("validate", func(b *testing.B) {
b.ResetTimer()
t1 := time.Now()
for i := 0; i < b.N; i++ {
var obj1 = &skiptype{}
// validate skip
d := NewDecoder(c.json)
// t1 := time.Now()
err1 := d.Decode(obj1)
_ = err1
_ = d.Decode(obj1)
}
d1 := time.Since(t1)
avg1 = d1/time.Duration(b.N)
})
b.Run("fast", func(b *testing.B) {
b.ResetTimer()
t2 := time.Now()
for i := 0; i < b.N; i++ {
var obj2 = &skiptype{}
// d1 := time.Since(t1)
// fask skip
d := NewDecoder(c.json)
d.SetOptions(OptionNoValidateJSON)

Check failure on line 144 in decoder/decoder_test.go

View workflow job for this annotation

GitHub Actions / build (1.16.x, arm)

undefined: OptionNoValidateJSON

Check failure on line 144 in decoder/decoder_test.go

View workflow job for this annotation

GitHub Actions / build (1.17.x, arm)

undefined: OptionNoValidateJSON

Check failure on line 144 in decoder/decoder_test.go

View workflow job for this annotation

GitHub Actions / build (1.18.x, arm)

undefined: OptionNoValidateJSON
// t2 := time.Now()
err2 := d.Decode(obj2)
// d2 := time.Since(t2)
_ = err2
_ = d.Decode(obj2)
}
d2 := time.Since(t2)
avg2 = d2/time.Duration(b.N)
})
// require.Equal(t, err1 == nil, err2 == nil)
// require.Equal(t, obj1, obj2)
// // fast skip must be 5x faster
// println(d1, d2)
// require.True(t, float64(d1)/float64(d2) > c.expTime, fmt.Sprintf("%v/%v=%v", d1, d2, float64(d1)/float64(d2)))
// fast skip must be expTime x faster
require.True(b, float64(avg1)/float64(avg2) > c.expTime, fmt.Sprintf("%v/%v=%v", avg1, avg2, float64(avg1)/float64(avg2)))
})
}
// var data = `{"a":`+TwitterJson+`,"b":`+TwitterJson+`,"c":[`+strings.Repeat("1,", 1024)+`1], "d":`+TwitterJson+`, "UNKNOWN":`+TwitterJson+`}`
}


Expand Down

0 comments on commit 8a0e155

Please sign in to comment.