/
bench.go
158 lines (130 loc) · 3.68 KB
/
bench.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"github.com/zencoder/disque-go/disque"
"golang.org/x/net/context"
"log"
"math/rand"
"sync"
"time"
)
var p *disque.DisquePool
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
const queueName = "myqueue"
var msgLength int
var src []rand.Source
// RandStringBytesMaskImprSrc retruns fast random string
// See: http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
func RandStringBytesMaskImprSrc(randSource, n int) string {
b := make([]byte, n)
s := src[randSource]
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, s.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = s.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func init() {
hosts := []string{"127.0.0.1:7711"} // array of 1 or more Disque servers
cycle := 1000 // check connection stats every 1000 Fetch's
capacity := 10 // initial capacity of the pool
maxCapacity := 50 // max capacity that the pool can be resized to
idleTimeout := 15 * time.Minute // timeout for idle connections
p = disque.NewDisquePool(hosts, cycle, capacity, maxCapacity, idleTimeout)
}
// Put gets a randSoruce idx to ensure some thread safety
func Put(randSource int) {
var d *disque.Disque
var err error
d, err = p.Get(context.Background()) // get a connection from the pool
if err != nil {
log.Println(err)
}
_, err = d.Push(queueName, RandStringBytesMaskImprSrc(randSource, msgLength), time.Second*20)
if err != nil {
log.Fatal(err)
}
p.Put(d) // return a connection to the pool
}
// Get gets from given queue
func Get() {
var d *disque.Disque
var err error
d, err = p.Get(context.Background()) // get a connection from the pool
if err != nil {
log.Println(err)
}
_, err = d.Fetch(queueName, time.Second*1)
if err != nil {
log.Fatal(err)
}
p.Put(d) // return a connection to the pool
}
func PutTest(threads, N int) {
var wg sync.WaitGroup
start := time.Now()
for j := 0; j < threads; j++ {
wg.Add(1)
a := rand.NewSource(time.Now().UnixNano())
src = append(src, a)
go func(idx int) {
defer wg.Done()
for i := 0; i < N; i++ {
Put(idx)
}
}(j)
}
wg.Wait()
duration := time.Since(start)
total := N * threads
speed := float64(total) / duration.Seconds()
fmt.Printf("[SET] %12s %8d %7.3f/s\n", duration, total, speed)
}
func GetTest(threads, N int) {
var wg sync.WaitGroup
start := time.Now()
for j := 0; j < threads; j++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < N; i++ {
Get()
}
}()
}
wg.Wait()
duration := time.Since(start)
total := N * threads
speed := float64(total) / duration.Seconds()
fmt.Printf("[GET] %12s %8d %7.3f/s\n", duration, total, speed)
}
func main() {
Ns := []int{10000, 20000, 40000}
messageLens := []int{1, 4, 8, 16, 20, 200, 1000}
ThreadCounts := []int{1, 2, 4, 8, 10, 16}
for _, n := range Ns {
for _, threads := range ThreadCounts {
for _, messageLen := range messageLens {
msgLength = messageLen
fmt.Printf("Msg: %4d N: %7d Threads: %4d \n", msgLength, n, threads)
PutTest(threads, n)
GetTest(threads, n)
fmt.Println("======================================")
time.Sleep(time.Second * 1)
}
}
}
}