-
Notifications
You must be signed in to change notification settings - Fork 8
/
benchmark_test.go
114 lines (98 loc) · 2.38 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package amqprpc
import (
"context"
"testing"
"time"
"github.com/google/uuid"
amqp "github.com/rabbitmq/amqp091-go"
)
func Benchmark(b *testing.B) {
s := NewServer(testURL)
queueName := uuid.New().String()
s.Bind(DirectBinding(queueName, func(_ context.Context, _ *ResponseWriter, _ amqp.Delivery) {}))
go s.ListenAndServe()
time.Sleep(1 * time.Second)
confirmingClient := NewClient(testURL).
WithTimeout(3 * time.Minute)
defer confirmingClient.Stop()
fastClient := NewClient(testURL).
WithTimeout(3 * time.Minute).
WithConfirmMode(false)
defer fastClient.Stop()
// Send a request to ensure the client have started.
_, err := confirmingClient.Send(NewRequest().WithRoutingKey(queueName))
if err != nil {
b.Fatal("client/server not working")
}
// Send a request to ensure the client have started.
_, err = fastClient.Send(NewRequest().WithRoutingKey(queueName))
if err != nil {
b.Fatal("client/server not working")
}
benchmarks := []struct {
name string
withResponse bool
returned bool
client *Client
}{
{
name: "WithResponse-NoConfirmMode",
withResponse: true,
client: fastClient,
},
{
name: "WithResponse-ConfirmMode",
withResponse: true,
client: confirmingClient,
},
{
name: "WithResponse-Returned",
withResponse: false,
returned: true,
client: confirmingClient,
},
{
name: "NoResponse-NoConfirmMode",
withResponse: false,
client: fastClient,
},
{
name: "NoResponse-ConfirmMode",
withResponse: false,
client: confirmingClient,
},
{
name: "NoResponse-Returned",
withResponse: false,
returned: true,
client: confirmingClient,
},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
routingKey := queueName
if bm.returned {
routingKey = "does-not-exist"
}
time.Sleep(2 * time.Second) // Let the amqp-server calm down between the tests.
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
request := NewRequest().
WithRoutingKey(routingKey).
WithResponse(bm.withResponse)
_, err := bm.client.Send(request)
if bm.returned {
if err == nil {
b.Fatal("Expected err to be non nil")
}
} else {
if err != nil {
b.Fatal(err.Error())
}
}
}
})
})
}
}