transfer_yl/untils/rsa/sonic_test.go

106 lines
2.2 KiB
Go
Raw Normal View History

2024-07-16 16:51:39 +08:00
package rsa
import (
"encoding/json"
"fmt"
"github.com/bytedance/sonic"
"testing"
"time"
)
type Book struct {
Name string `json:"title"`
Price float64
Tags []string
Press string
Author People
Social
}
type People struct {
Name string
Age int
School string
Company string
Title string
}
type Social struct {
A [][]string
B [][][]string
C [][][][]string
D string
E string
}
var (
people = People{
Name: "张三",
Age: 18,
School: "社会大学",
Company: "大乔乔教育",
Title: "开发工程师",
}
book = Book{
Name: "高性能golang",
Price: 58.0,
Tags: []string{"golang", "编程", "计算机"},
Press: "机械工业出版社",
Author: people,
Social: social,
}
social = Social{
A: [][]string{{"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}},
B: [][][]string{{{"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}}, {{"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}}},
C: [][][][]string{{{{"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}}, {{"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}}}, {{{"golang", "编程", "计算机"}, {"golang", "编程", "计算机"}}}},
D: "大乔乔教育",
E: "开发工程师",
}
)
func TestStdJson(t *testing.T) {
start := time.Now()
bs, _ := sonic.Marshal(book)
var book2 Book
if err := sonic.Unmarshal(bs, &book2); err != nil {
fmt.Println(err)
t.Fail()
} else {
fmt.Printf("%+v\n", book2)
}
elapsed := time.Since(start)
fmt.Printf("TestSomething took %v\n", elapsed)
}
func TestSonic(t *testing.T) {
start := time.Now()
bs, _ := sonic.Marshal(book)
var book2 Book
if err := sonic.Unmarshal(bs, &book2); err != nil {
fmt.Println(err)
t.Fail()
} else {
fmt.Printf("%+v\n", book2)
}
elapsed := time.Since(start)
fmt.Printf("TestSomething took %v\n", elapsed)
}
func BenchmarkStdJson(b *testing.B) {
for i := 0; i < b.N; i++ {
bs, _ := json.Marshal(book)
var book2 Book
json.Unmarshal(bs, &book2)
}
}
func BenchmarkSonic(b *testing.B) {
for i := 0; i < b.N; i++ {
bs, _ := sonic.Marshal(book)
var book2 Book
sonic.Unmarshal(bs, &book2)
}
}