|
|
@@ -4,6 +4,7 @@ import (
|
|
|
"fmt"
|
|
|
"testing"
|
|
|
"github.com/json-iterator/go/require"
|
|
|
+ "encoding/json"
|
|
|
)
|
|
|
|
|
|
func Test_bind_api_demo(t *testing.T) {
|
|
|
@@ -22,3 +23,65 @@ func Test_iterator_api_demo(t *testing.T) {
|
|
|
}
|
|
|
fmt.Println(total)
|
|
|
}
|
|
|
+
|
|
|
+type People struct {
|
|
|
+ Name string
|
|
|
+ Gender string
|
|
|
+ Age int
|
|
|
+ Address string
|
|
|
+ Mobile string
|
|
|
+ Country string
|
|
|
+ Height int
|
|
|
+}
|
|
|
+
|
|
|
+func jsoniterMarshal(p *People) error {
|
|
|
+ _, err := Marshal(p)
|
|
|
+ if nil != err {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+func stdMarshal(p *People) error {
|
|
|
+ _, err := json.Marshal(p)
|
|
|
+ if nil != err {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func BenchmarkJosniterMarshal(b *testing.B) {
|
|
|
+ var p People
|
|
|
+ p.Address = "上海市徐汇区漕宝路"
|
|
|
+ p.Age = 30
|
|
|
+ p.Country = "中国"
|
|
|
+ p.Gender = "male"
|
|
|
+ p.Height = 170
|
|
|
+ p.Mobile = "18502120533"
|
|
|
+ p.Name = "Elvin"
|
|
|
+ b.ReportAllocs()
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
+ err := jsoniterMarshal(&p)
|
|
|
+ if nil != err {
|
|
|
+ b.Error(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func BenchmarkStdMarshal(b *testing.B) {
|
|
|
+ var p People
|
|
|
+ p.Address = "上海市徐汇区漕宝路"
|
|
|
+ p.Age = 30
|
|
|
+ p.Country = "中国"
|
|
|
+ p.Gender = "male"
|
|
|
+ p.Height = 170
|
|
|
+ p.Mobile = "18502120533"
|
|
|
+ p.Name = "Elvin"
|
|
|
+ b.ReportAllocs()
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
+ err := stdMarshal(&p)
|
|
|
+ if nil != err {
|
|
|
+ b.Error(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|