forked from vcabbage/amqp
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbenchmark_test.go
73 lines (64 loc) · 1.68 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
package amqp_test
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
amqp "github.com/Azure/go-amqp"
)
func BenchmarkSimple(b *testing.B) {
if localBrokerAddr == "" {
b.Skip()
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
client, err := amqp.Dial(ctx, localBrokerAddr, nil)
cancel()
if err != nil {
b.Fatal(err)
}
defer client.Close()
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
session, err := client.NewSession(ctx, nil)
cancel()
if err != nil {
b.Fatal(err)
}
// add a random suffix to the link name so the test broker always creates a new node
targetName := fmt.Sprintf("BenchmarkSimple %d", rand.Uint64())
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
sender, err := session.NewSender(ctx, targetName, nil)
cancel()
if err != nil {
b.Fatal(err)
}
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
receiver, err := session.NewReceiver(ctx, targetName, nil)
cancel()
if err != nil {
b.Fatal(err)
}
msg := amqp.NewMessage([]byte("test message"))
for i := 0; i < b.N; i++ {
// simple send and receive message, no concurrency
for j := 0; j < 10000; j++ {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
if err := sender.Send(ctx, msg, nil); err != nil {
b.Fatal(err)
}
cancel()
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
msg, err := receiver.Receive(ctx, nil)
cancel()
if err != nil {
b.Fatal(err)
}
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
err = receiver.AcceptMessage(ctx, msg)
cancel()
if err != nil {
b.Fatal(err)
}
}
}
}