-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_test.go
103 lines (87 loc) · 2.1 KB
/
serialize_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
package benchmark
import (
"testing"
"time"
"github.com/golang-queue/queue/job"
"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)
type mockMessage struct {
message string
}
func (m mockMessage) Bytes() []byte {
return []byte(m.message)
}
func BenchmarkEncode(b *testing.B) {
m := job.NewMessage(&mockMessage{
message: "foo",
}, job.AllowOption{
RetryCount: job.Int64(100),
RetryDelay: job.Time(30 * time.Millisecond),
Timeout: job.Time(3 * time.Millisecond),
})
b.Run("JSON", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(m)
}
})
b.Run("UnsafeCast", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = job.Encode(m)
}
})
}
func BenchmarkDecode(b *testing.B) {
m := job.NewMessage(&mockMessage{
message: "foo",
}, job.AllowOption{
RetryCount: job.Int64(100),
RetryDelay: job.Time(30 * time.Millisecond),
Timeout: job.Time(3 * time.Millisecond),
})
b.Run("JSON", func(b *testing.B) {
data, _ := json.Marshal(m)
out := &job.Message{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = json.Unmarshal(data, out)
}
})
b.Run("UnsafeCast", func(b *testing.B) {
data := job.Encode(m)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = job.Decode(data)
}
})
}
func TestEncodeAndDecode(t *testing.T) {
m := job.NewMessage(&mockMessage{
message: "foo",
}, job.AllowOption{
RetryCount: job.Int64(100),
RetryDelay: job.Time(30 * time.Millisecond),
Timeout: job.Time(3 * time.Millisecond),
})
t.Run("JSON", func(t *testing.T) {
data, _ := json.Marshal(m)
out := &job.Message{}
_ = json.Unmarshal(data, out)
assert.Equal(t, int64(100), out.RetryCount)
assert.Equal(t, 30*time.Millisecond, out.RetryDelay)
assert.Equal(t, 3*time.Millisecond, out.Timeout)
})
t.Run("UnsafeCast", func(t *testing.T) {
data := job.Encode(m)
out := job.Decode(data)
assert.Equal(t, int64(100), out.RetryCount)
assert.Equal(t, 30*time.Millisecond, out.RetryDelay)
assert.Equal(t, 3*time.Millisecond, out.Timeout)
})
}