/
client.go
78 lines (62 loc) · 1.62 KB
/
client.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
package main
import (
"context"
"flag"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/smallnest/rpcx/client"
)
var (
addr = flag.String("addr", "tcp@localhost:8972", "snowflake service addressed")
concurrency = flag.Int("c", 256, "concurrent clients")
count = flag.Int("n", 10000000, "fetched ids per client")
batch = flag.Int("b", 1, "batch number")
)
func main() {
flag.Parse()
addrs := strings.Split(*addr, ",")
peers := make([]*client.KVPair, 0, len(addrs))
for _, a := range addrs {
peers = append(peers, &client.KVPair{Key: a})
}
var wg, latch sync.WaitGroup
wg.Add(*concurrency)
latch.Add(*concurrency)
for i := 0; i < *concurrency; i++ {
go func() {
var id int64
var ids []int64
d := client.NewMultipleServersDiscovery(peers)
xclient := client.NewXClient("snowflake", client.Failover, client.RoundRobin, d, client.DefaultOption)
defer xclient.Close()
//warmup
xclient.Call(context.Background(), "Get", 0, &id)
// start
latch.Done()
latch.Wait()
n := *count / (*batch)
for j := 0; j < n; j++ {
if *batch <= 1 {
err := xclient.Call(context.Background(), "Get", 0, &id)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
} else {
err := xclient.Call(context.Background(), "GetBatch", *batch, &ids)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
}
}
wg.Done()
}()
}
latch.Wait()
start := time.Now()
wg.Wait()
dur := time.Since(start)
fmt.Printf("total IDs: %d, duration: %v, id/s: %d", (*concurrency)*(*count), dur, int64(*concurrency)*int64(*count)*1000000/dur.Microseconds())
}