Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(job): serialize a struct to bytes #103

Merged
merged 1 commit into from
Jan 30, 2023
Merged

chore(job): serialize a struct to bytes #103

merged 1 commit into from
Jan 30, 2023

Conversation

appleboy
Copy link
Member

@appleboy appleboy commented Jan 29, 2023

See the reference: Serialize a struct to bytes to send it through the network in Go — Part I

BenchmarkEncode
BenchmarkEncode/JSON
BenchmarkEncode/JSON-2                               	 6139400	       189.3 ns/op	      96 B/op	       1 allocs/op
BenchmarkEncode/JSON-2                               	 6316482	       189.1 ns/op	      96 B/op	       1 allocs/op
BenchmarkEncode/JSON-2                               	 6304340	       189.5 ns/op	      96 B/op	       1 allocs/op
BenchmarkEncode/JSON-2                               	 6285771	       189.5 ns/op	      96 B/op	       1 allocs/op
BenchmarkEncode/JSON-2                               	 6304476	       188.7 ns/op	      96 B/op	       1 allocs/op
BenchmarkEncode/UnsafeCast
BenchmarkEncode/UnsafeCast-2                         	1000000000	         0.5096 ns/op	       0 B/op	       0 allocs/op
BenchmarkEncode/UnsafeCast-2                         	1000000000	         0.5098 ns/op	       0 B/op	       0 allocs/op
BenchmarkEncode/UnsafeCast-2                         	1000000000	         0.5101 ns/op	       0 B/op	       0 allocs/op
BenchmarkEncode/UnsafeCast-2                         	1000000000	         0.5099 ns/op	       0 B/op	       0 allocs/op
BenchmarkEncode/UnsafeCast-2                         	1000000000	         0.5097 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecode
BenchmarkDecode/JSON
BenchmarkDecode/JSON-2                               	 3469248	       344.1 ns/op	      99 B/op	       2 allocs/op
BenchmarkDecode/JSON-2                               	 3480768	       343.9 ns/op	      99 B/op	       2 allocs/op
BenchmarkDecode/JSON-2                               	 3455664	       346.0 ns/op	      99 B/op	       2 allocs/op
BenchmarkDecode/JSON-2                               	 3488086	       343.6 ns/op	      99 B/op	       2 allocs/op
BenchmarkDecode/JSON-2                               	 3478113	       343.5 ns/op	      99 B/op	       2 allocs/op
BenchmarkDecode/UnsafeCast
BenchmarkDecode/UnsafeCast-2                         	1000000000	         0.4021 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecode/UnsafeCast-2                         	1000000000	         0.4021 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecode/UnsafeCast-2                         	1000000000	         0.4023 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecode/UnsafeCast-2                         	1000000000	         0.4022 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecode/UnsafeCast-2                         	1000000000	         0.4023 ns/op	       0 B/op	       0 allocs/op

Test Code:

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)
	})
}

Signed-off-by: Bo-Yi.Wu [email protected]

@codecov-commenter
Copy link

codecov-commenter commented Jan 29, 2023

Codecov Report

Merging #103 (321f27e) into master (bd08b4a) will increase coverage by 1.32%.
The diff coverage is 90.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #103      +/-   ##
==========================================
+ Coverage   89.44%   90.76%   +1.32%     
==========================================
  Files           7        7              
  Lines         398      390       -8     
==========================================
- Hits          356      354       -2     
+ Misses         33       29       -4     
+ Partials        9        7       -2     
Flag Coverage Δ
go-1.17 90.76% <90.00%> (+1.32%) ⬆️
go-1.18 90.76% <90.00%> (+1.32%) ⬆️
go-1.19 90.76% <90.00%> (+1.32%) ⬆️
macos-latest 90.76% <90.00%> (+1.32%) ⬆️
ubuntu-latest 90.76% <90.00%> (+1.32%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
queue.go 91.54% <90.00%> (+2.54%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@appleboy appleboy merged commit e64d4a0 into master Jan 30, 2023
@appleboy appleboy deleted the json branch January 30, 2023 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants