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