性能测试
package unittest
import (
"encoding/json"
"testing"
"time"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Gender bool `json:"gender"`
Weight float32 `json:"weight"`
Height float32 `json:"height"`
Birthdate time.Time `json:"birthdate"`
}
func BenchmarkMarshalJson(b *testing.B) {
me := Person{
Name: "viva",
Age: 26,
Gender: true,
}
for i := 0; i < b.N; i++ {
json.Marshal(&me)
}
}
func BenchmarkUnmarshalJson(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Unmarshal([]byte(`{
"name": "viva",
"age": 26,
"gender": true,
}`),
new(Person))
}
}最后更新于