-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrunner.go
194 lines (172 loc) · 4.91 KB
/
runner.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"crypto/rand"
"encoding/json"
"fmt"
"log"
"os"
"runtime"
"strconv"
"sync"
"time"
"github.com/rcrowley/go-metrics"
)
var baseName string //This serves as the base for influx
var args []int
type MQRunner interface {
Name() string
// Producer defines a runner that writes x messages with
// body specifed on queue called name.
Produce(name, body string, messages int)
// Consumer defines a runner that gets x messages from
// queue called name.
Consume(name string, messages int)
setupQueues(queues []string)
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
var config Config
f, err := os.Create("errorlog")
if err != nil {
log.Println(err)
os.Exit(2)
}
log.SetOutput(f)
cfgFile, err := os.Open("influx.json")
if err != nil {
fmt.Fprintln(os.Stdout, "error: ", err)
return
}
err = json.NewDecoder(cfgFile).Decode(&config)
if err != nil {
return
}
go Influxdb(metrics.DefaultRegistry, 250*time.Millisecond, &config)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
var mqs []MQRunner
//mqs = append(mqs, new(IronRunner), new(RabbitRunner))
mqs = append(mqs, new(RabbitRunner))
if len(os.Args) < 6 {
fmt.Println("usage: ./iron-maiden [message_count] [messages_per_batch] [threads_per_queue] [amount_of_queues] [message_size]")
return
}
for _, v := range os.Args[1:] {
i, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("couldnt parse string", v)
}
args = append(args, i)
}
prodAndConsume(mqs, args[0], args[1], args[2], args[3], args[4])
metrics.WriteOnce(metrics.DefaultRegistry, os.Stdout)
}
func prodThenConsume(mqs []MQRunner, messages, atATime, threadperQ, queues, bytes int) {
baseName = fmt.Sprintf("%d_%d_%d_%d_ptc", args[0], args[1], args[3], args[4])
qnames := qnames(queues)
for _, mq := range mqs {
fmt.Println(mq.Name()+":", "concurrency benchmark with", messages, "message(s),",
atATime, "at a time, across", queues, "queue(s) using ", bytes, "bytes")
mq.setupQueues(qnames)
dur := produce(mq, messages, atATime, threadperQ, qnames, bytes)
fmt.Println("producer took", dur)
dur = consume(mq, messages, atATime, threadperQ, qnames)
fmt.Println("consumer took", dur)
}
}
func prodAndConsume(mqs []MQRunner, messages, atATime, threadperQ, queues, bytes int) {
baseName = fmt.Sprintf("%d_%d_%d_%d", args[0], args[1], args[3], args[4])
qnames := qnames(queues)
for _, mq := range mqs {
fmt.Println(mq.Name()+":", "concurrency benchmark with", messages, "message(s),",
atATime, "at a time, across", queues, "queue(s) using ", bytes, "bytes")
mq.setupQueues(qnames)
var wait sync.WaitGroup
wait.Add(2)
then := time.Now()
go func() {
defer wait.Done()
produce(mq, messages, atATime, threadperQ, qnames, bytes)
}()
go func() {
defer wait.Done()
consume(mq, messages, atATime, threadperQ, qnames)
}()
wait.Wait()
fmt.Println("producer and consumer took", time.Since(then))
}
}
// for each queue specified, produce x messages y at a time
func produce(mq MQRunner, messages, atATime, threadperQ int, qnames []string, bytes int) time.Duration {
produceTimerName := fmt.Sprintf("producer_%s_%s", mq.Name(), baseName)
timer := metrics.GetOrRegisterTimer(produceTimerName, metrics.DefaultRegistry)
payload := rand_str(bytes)
var wait sync.WaitGroup
wait.Add(len(qnames))
then := time.Now()
for _, name := range qnames {
go func(name string) {
defer wait.Done()
var waiter sync.WaitGroup
waiter.Add(threadperQ)
for i := 0; i < threadperQ; i++ {
go func() {
for j := 0; j < messages/atATime/threadperQ; j++ {
timer.Time(func() {
mq.Produce(name, payload, atATime)
})
}
waiter.Done()
}()
}
waiter.Wait()
}(name)
}
wait.Wait()
return time.Since(then)
}
// for each queue specified, consume x messages y at a time
func consume(mq MQRunner, messages, atATime, threadperQ int, qnames []string) time.Duration {
consumeTimerName := fmt.Sprintf("consumer_%s_%s", mq.Name(), baseName)
timer := metrics.GetOrRegisterTimer(consumeTimerName, metrics.DefaultRegistry)
var wait sync.WaitGroup
wait.Add(len(qnames))
then := time.Now()
for _, name := range qnames {
go func(name string) {
defer wait.Done()
var waiter sync.WaitGroup
waiter.Add(threadperQ)
for i := 0; i < threadperQ; i++ {
go func() {
for j := 0; j < messages/atATime/threadperQ; j++ {
timer.Time(func() {
mq.Consume(name, atATime)
})
}
waiter.Done()
}()
}
waiter.Wait()
}(name)
}
wait.Wait()
return time.Since(then)
}
func qnames(numQ int) []string {
qnames := make([]string, numQ)
for i := 0; i < numQ; i++ {
qnames[i] = rand_str(12)
}
return qnames
}
func rand_str(str_size int) string {
alphanum := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // rabbit doesn't do unicode so hot :(
var bytes = make([]byte, str_size)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}