forked from Azure/azure-service-bus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefetch_example_test.go
110 lines (93 loc) · 2.48 KB
/
prefetch_example_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
package servicebus_test
import (
"context"
"fmt"
"math/rand"
"os"
"time"
"github.com/Azure/azure-service-bus-go"
)
func Example_prefetch() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
connStr := os.Getenv("SERVICEBUS_CONNECTION_STRING")
if connStr == "" {
fmt.Println("FATAL: expected environment variable SERVICEBUS_CONNECTION_STRING not set")
return
}
// Create a client to communicate with a Service Bus Namespace.
ns, err := servicebus.NewNamespace(servicebus.NamespaceWithConnectionString(connStr))
if err != nil {
fmt.Println(err)
return
}
qm := ns.NewQueueManager()
prefetch1, err := ensureQueue(ctx, qm, "Prefetch1Example")
if err != nil {
fmt.Println(err)
return
}
prefetch1000, err := ensureQueue(ctx, qm, "Prefetch1000Example")
if err != nil {
fmt.Println(err)
return
}
// sendAndReceive will send to the queue and read from the queue
sendAndReceive := func(ctx context.Context, name string, opt servicebus.QueueOption) error {
messageCount := 200
q, err := ns.NewQueue(name, opt, servicebus.QueueWithReceiveAndDelete())
if err != nil {
return err
}
buffer := make([]byte, 1000)
if _, err := rand.Read(buffer); err != nil {
return err
}
for i := 0; i < messageCount; i++ {
if err := q.Send(ctx, servicebus.NewMessage(buffer)); err != nil {
return err
}
}
innerCtx, cancel := context.WithCancel(ctx)
count := 0
err = q.Receive(innerCtx, servicebus.HandlerFunc(func(ctx context.Context, msg *servicebus.Message) error {
count++
if count == messageCount-1 {
defer cancel()
}
return msg.Complete(ctx)
}))
if err != nil {
if err.Error() != "context canceled" {
return err
}
}
return nil
}
// run send and receive concurrently and compare the times
totalPrefetch1 := make(chan time.Duration)
go func() {
start := time.Now()
if err := sendAndReceive(ctx, prefetch1.Name, servicebus.QueueWithPrefetchCount(1)); err != nil {
fmt.Println(err)
return
}
totalPrefetch1 <- time.Now().Sub(start)
}()
totalPrefetch1000 := make(chan time.Duration)
go func() {
start := time.Now()
if err := sendAndReceive(ctx, prefetch1000.Name, servicebus.QueueWithPrefetchCount(1000)); err != nil {
fmt.Println(err)
return
}
totalPrefetch1000 <- time.Now().Sub(start)
}()
tp1 := <-totalPrefetch1
tp2 := <-totalPrefetch1000
if tp1 > tp2 {
fmt.Println("prefetch of 1000 took less time!")
}
// Output:
// prefetch of 1000 took less time!
}