Skip to content

Commit c1a27f5

Browse files
committedJan 30, 2023
chore(serialize): add json vs Unsafe Cast
1 parent a6a0768 commit c1a27f5

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed
 

‎go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/golang-queue/benchmark
22

33
go 1.19
44

5-
require github.com/golang-queue/queue v0.1.4-0.20230128040859-bf40424b0f6e
5+
require github.com/golang-queue/queue v0.1.4-0.20230129131029-321f27ee1387
66

7-
require github.com/goccy/go-json v0.10.0 // indirect
7+
require github.com/goccy/go-json v0.10.0

‎go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
33
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
4-
github.com/golang-queue/queue v0.1.4-0.20230116141530-10c111be7eed h1:iYHd+xRf8zznIa6A1M9XK2gONQWANh8jrGkxAxYWDV0=
5-
github.com/golang-queue/queue v0.1.4-0.20230116141530-10c111be7eed/go.mod h1:8P7IgwdxwKh0/W1I9yCuQQGI8OHIuc7fIHi4OYr1COU=
6-
github.com/golang-queue/queue v0.1.4-0.20230128040859-bf40424b0f6e h1:pJwqxn8r7buMeOmvbMoUtyfzDhXIMlIjmDI6Y/jQY4o=
7-
github.com/golang-queue/queue v0.1.4-0.20230128040859-bf40424b0f6e/go.mod h1:8P7IgwdxwKh0/W1I9yCuQQGI8OHIuc7fIHi4OYr1COU=
4+
github.com/golang-queue/queue v0.1.4-0.20230129131029-321f27ee1387 h1:qAj1tqpV3BW7Ll4N9xM4L+sIPeJDH3dulJ6s4txt8Yg=
5+
github.com/golang-queue/queue v0.1.4-0.20230129131029-321f27ee1387/go.mod h1:iFgyvS8kKQ3Mh+diQ6ZqF6yFaYFm6gA8W0NW2CMQ8KI=
86
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
97
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
108
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
File renamed without changes.

‎serialize_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package benchmark
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/golang-queue/queue/job"
8+
9+
"github.com/goccy/go-json"
10+
)
11+
12+
type mockMessage struct {
13+
message string
14+
}
15+
16+
func (m mockMessage) Bytes() []byte {
17+
return []byte(m.message)
18+
}
19+
20+
func BenchmarkEncode(b *testing.B) {
21+
m := job.NewMessage(&mockMessage{
22+
message: "foo",
23+
}, job.AllowOption{
24+
RetryCount: job.Int64(100),
25+
RetryDelay: job.Time(30 * time.Millisecond),
26+
Timeout: job.Time(3 * time.Millisecond),
27+
})
28+
29+
b.Run("JSON", func(b *testing.B) {
30+
b.ReportAllocs()
31+
b.ResetTimer()
32+
for i := 0; i < b.N; i++ {
33+
_, _ = json.Marshal(m)
34+
}
35+
})
36+
37+
b.Run("UnsafeCast", func(b *testing.B) {
38+
b.ReportAllocs()
39+
b.ResetTimer()
40+
for i := 0; i < b.N; i++ {
41+
_ = job.Encode(m)
42+
}
43+
})
44+
}
45+
46+
func BenchmarkDecode(b *testing.B) {
47+
m := job.NewMessage(&mockMessage{
48+
message: "foo",
49+
}, job.AllowOption{
50+
RetryCount: job.Int64(100),
51+
RetryDelay: job.Time(30 * time.Millisecond),
52+
Timeout: job.Time(3 * time.Millisecond),
53+
})
54+
55+
b.Run("JSON", func(b *testing.B) {
56+
data, _ := json.Marshal(m)
57+
var out *job.Message
58+
b.ReportAllocs()
59+
b.ResetTimer()
60+
for i := 0; i < b.N; i++ {
61+
_ = json.Unmarshal(data, out)
62+
}
63+
})
64+
65+
b.Run("UnsafeCast", func(b *testing.B) {
66+
data := job.Encode(m)
67+
b.ReportAllocs()
68+
b.ResetTimer()
69+
for i := 0; i < b.N; i++ {
70+
_ = job.Decode(data)
71+
}
72+
})
73+
}

0 commit comments

Comments
 (0)