-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
125 lines (119 loc) · 3.29 KB
/
json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package golangUtil
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"sync"
"testing"
"time"
)
type testStruct struct {
name string
value string
mapping int
err error
maps map[string]interface{}
}
func TestCopy(t *testing.T) {
s := *JsonEncoder
fmt.Println(s.load(uintptr(0)))
}
func TestFunctionMarsahl(t *testing.T) {
bytes, err := Marshal(&testStruct{name: "xiyanggou", value: "xiyangValuesagou", mapping: rand.Int(), err: fmt.Errorf("hello error"), maps: map[string]interface{}{"xiyang": testStruct{name: "xiyang"}}})
if err != nil {
return
}
fmt.Println(json.Valid(bytes))
fmt.Println(string(bytes))
rightResult, err := json.Marshal(&testStruct{name: "xiyanggou", value: "xiyangValuesagou", mapping: rand.Int(), err: fmt.Errorf("hello error"), maps: map[string]interface{}{"xiyang": testStruct{name: "xiyang"}}})
if err != nil {
return
}
fmt.Println("---------------")
fmt.Println(string(rightResult))
fmt.Println("---------------")
fmt.Println(json.Valid([]byte(`{"testStruct": {"name": "xiyanggou"
,"value": "xiyangValuesagou"
,"mapping": 5577006791947779410
,"err": {"errorString": {"s": "hello error"}}
,"maps": {"xiyang": {"testStruct": {"name": "xiyang"}}}}`)))
}
func TestMarshal(t *testing.T) {
const testTimes int = 100
for i := 0; i <= testTimes; i++ {
var temp = testStruct{name: "xiyang", value: "xiyangValue", mapping: rand.Int(), err: fmt.Errorf("hello error")}
bytes, err := Marshal(temp)
if err != nil {
return
}
fmt.Println(string(bytes))
if !json.Valid(bytes) {
fmt.Println("error .....")
return
}
}
fmt.Println(JsonEncoder.cacheSuccessNums)
}
func TestGolangUtilMarshal(t *testing.T) {
const testTimes int = 100000
group := sync.WaitGroup{}
group.Add(testTimes)
now := time.Now()
for i := 0; i < testTimes; i++ {
go func() {
defer group.Done()
bytes, err := Marshal(&testStruct{name: "xiyanggou", value: "xiyangValuesagou", mapping: rand.Int(), err: fmt.Errorf("hello error"), maps: map[string]interface{}{"xiyang": testStruct{name: "xiyang"}}})
if err != nil {
fmt.Println(err.Error())
return
}
if !json.Valid(bytes) {
fmt.Println("analy error")
os.Exit(1)
}
}()
}
group.Wait()
since := time.Since(now)
fmt.Println(since)
fmt.Println(JsonEncoder.cacheNums)
fmt.Println(JsonEncoder.cacheSuccessNums)
fmt.Println(len(JsonEncoder.cache))
}
func TestJsonMarshal(t *testing.T) {
const testTimes int = 10000
group := sync.WaitGroup{}
group.Add(testTimes)
now := time.Now()
for i := 0; i < testTimes; i++ {
go func() {
defer group.Done()
_, err := json.Marshal(testStruct{name: "xiyang", value: "xiyangValue", mapping: rand.Int(), err: fmt.Errorf("hello error")})
if err != nil {
fmt.Println(err.Error())
return
}
}()
}
group.Wait()
since := time.Since(now)
fmt.Println(since)
}
func BenchmarkGolangUtilMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := Marshal(&testStruct{name: "xiyang", value: "xiyangValue", mapping: rand.Int() * 20, err: fmt.Errorf("hello error")})
if err != nil {
fmt.Println(err.Error())
return
}
}
}
func BenchmarkJSONMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := json.Marshal(&testStruct{name: "xiyang", value: "xiyangValue", mapping: rand.Int() * 20, err: fmt.Errorf("hello error")})
if err != nil {
return
}
}
}