-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmu_benchmark_test.go
89 lines (86 loc) · 2.12 KB
/
mu_benchmark_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
package jsmu_test
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"testing"
"github.com/nofeaturesonlybugs/jsmu"
"github.com/nofeaturesonlybugs/jsmu/data"
)
func BenchmarkMU(b *testing.B) {
people := strings.Split(strings.Trim(data.PeopleStream, "\r\n "), "\n")
animals := strings.Split(strings.Trim(data.AnimalStream, "\r\n "), "\n")
all := make([]string, len(people)+len(animals))
copy(all, people)
copy(all[len(people):], animals)
rand.Shuffle(len(all), func(i, j int) {
all[i], all[j] = all[j], all[i]
})
//
type Person struct {
jsmu.TypeName `js:"-" jsmu:"person"`
//
Name string `json:"name"`
Age int `json:"age"`
}
type Animal struct {
jsmu.TypeName `js:"-" jsmu:"animal"`
//
Name string `json:"name"`
Says string `json:"says"`
}
//
mu := &jsmu.MU{
// EnveloperFn: nil, // jsmu.DefaultEnveloperFunc is the default.
// StructTag : "", // "jsmu" is the default.
}
mu.MustRegister(&Person{})
mu.MustRegister(&Animal{})
//
limits := []int{
5,
100,
250,
500,
1000,
}
for _, limit := range limits {
b.Run(fmt.Sprintf("2-pass unmarshal limit %v", limit), func(b *testing.B) {
type E struct {
Type string `json:"type"`
Id string `json:"id"`
Message json.RawMessage `json:"message"`
V interface{} `json:"-"`
}
var err error
for k := 0; k < b.N; k++ {
for n, max := 0, 2*limit; n < max; n++ {
env := E{}
if err = json.Unmarshal([]byte(all[n]), &env); err != nil {
b.Fatalf("during first json.Unmarshal: %v", err.Error())
}
switch env.Type {
case "animal":
env.V = &Animal{}
case "person":
env.V = &Person{}
}
if err = json.Unmarshal(env.Message, env.V); err != nil {
b.Fatalf("during second json.Unmarshal: %v", err.Error())
}
}
}
})
b.Run(fmt.Sprintf("jsmu unmarshal limit %v", limit), func(b *testing.B) {
var err error
for k := 0; k < b.N; k++ {
for n, max := 0, 2*limit; n < max; n++ {
if _, err = mu.Unmarshal([]byte(all[n])); err != nil {
b.Fatalf("during MU.Unmarshal: %v", err.Error())
}
}
}
})
}
}