From 8a0e155081ec4bedbeb80400b1e2ef74db28d432 Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Mon, 26 Aug 2024 14:17:53 +0800 Subject: [PATCH] bench: skip_fast vs skip_validate --- decoder/decoder_test.go | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/decoder/decoder_test.go b/decoder/decoder_test.go index e4067ccb8..19af2986f 100644 --- a/decoder/decoder_test.go +++ b/decoder/decoder_test.go @@ -18,6 +18,7 @@ package decoder import ( "encoding/json" + "fmt" "runtime" "runtime/debug" "strings" @@ -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) @@ -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) - // 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+`}` }