forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_writer.go
111 lines (100 loc) · 2.53 KB
/
bench_writer.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
package main
import (
"bufio"
"flag"
"log"
"net"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/nsqio/go-nsq"
)
var (
runfor = flag.Duration("runfor", 10*time.Second, "duration of time to run")
tcpAddress = flag.String("nsqd-tcp-address", "127.0.0.1:4150", "<addr>:<port> to connect to nsqd")
topic = flag.String("topic", "sub_bench", "topic to receive messages on")
size = flag.Int("size", 200, "size of messages")
batchSize = flag.Int("batch-size", 200, "batch size of messages")
deadline = flag.String("deadline", "", "deadline to start the benchmark run")
)
var totalMsgCount int64
func main() {
flag.Parse()
var wg sync.WaitGroup
log.SetPrefix("[bench_writer] ")
msg := make([]byte, *size)
batch := make([][]byte, *batchSize)
for i := range batch {
batch[i] = msg
}
goChan := make(chan int)
rdyChan := make(chan int)
for j := 0; j < runtime.GOMAXPROCS(0); j++ {
wg.Add(1)
go func() {
pubWorker(*runfor, *tcpAddress, *batchSize, batch, *topic, rdyChan, goChan)
wg.Done()
}()
<-rdyChan
}
if *deadline != "" {
t, err := time.Parse("2006-01-02 15:04:05", *deadline)
if err != nil {
log.Fatal(err)
}
d := t.Sub(time.Now())
log.Printf("sleeping until %s (%s)", t, d)
time.Sleep(d)
}
start := time.Now()
close(goChan)
wg.Wait()
end := time.Now()
duration := end.Sub(start)
tmc := atomic.LoadInt64(&totalMsgCount)
log.Printf("duration: %s - %.03fmb/s - %.03fops/s - %.03fus/op",
duration,
float64(tmc*int64(*size))/duration.Seconds()/1024/1024,
float64(tmc)/duration.Seconds(),
float64(duration/time.Microsecond)/float64(tmc))
}
func pubWorker(td time.Duration, tcpAddr string, batchSize int, batch [][]byte, topic string, rdyChan chan int, goChan chan int) {
conn, err := net.DialTimeout("tcp", tcpAddr, time.Second)
if err != nil {
panic(err.Error())
}
conn.Write(nsq.MagicV2)
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
rdyChan <- 1
<-goChan
var msgCount int64
endTime := time.Now().Add(td)
for {
cmd, _ := nsq.MultiPublish(topic, batch)
_, err := cmd.WriteTo(rw)
if err != nil {
panic(err.Error())
}
err = rw.Flush()
if err != nil {
panic(err.Error())
}
resp, err := nsq.ReadResponse(rw)
if err != nil {
panic(err.Error())
}
frameType, data, err := nsq.UnpackResponse(resp)
if err != nil {
panic(err.Error())
}
if frameType == nsq.FrameTypeError {
panic(string(data))
}
msgCount += int64(len(batch))
if time.Now().After(endTime) {
break
}
}
atomic.AddInt64(&totalMsgCount, msgCount)
}