-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
172 lines (155 loc) · 3.04 KB
/
client.go
File metadata and controls
172 lines (155 loc) · 3.04 KB
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
package tikv
import (
"io/ioutil"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"github.com/fperf/fperf"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv"
"github.com/sirupsen/logrus"
)
const seqPlaceHolder = "__seq_int__"
const randPlaceHolder = "__rand_int__"
var seq func() string = seqCreater(0)
var random func() string = randCreater(10000000000000000)
func seqCreater(begin int64) func() string {
// filled map, filled generated to 16 bytes
l := []string{
"",
"0",
"00",
"000",
"0000",
"00000",
"000000",
"0000000",
"00000000",
"000000000",
"0000000000",
"00000000000",
"000000000000",
"0000000000000",
"00000000000000",
"000000000000000",
}
v := begin
m := &sync.Mutex{}
return func() string {
m.Lock()
s := strconv.FormatInt(v, 10)
v += 1
m.Unlock()
filled := len(l) - len(s)
if filled <= 0 {
return s
}
return l[filled] + s
}
}
func randCreater(max int64) func() string {
// filled map, filled generated to 16 bytes
l := []string{
"",
"0",
"00",
"000",
"0000",
"00000",
"000000",
"0000000",
"00000000",
"000000000",
"0000000000",
"00000000000",
"000000000000",
"0000000000000",
"00000000000000",
"000000000000000",
}
var v int64
m := &sync.Mutex{}
r := rand.New(rand.NewSource(1))
return func() string {
m.Lock()
v = r.Int63n(max)
s := strconv.FormatInt(v, 10)
m.Unlock()
filled := len(l) - len(s)
if filled <= 0 {
return s
}
return l[filled] + s
}
}
func replaceSeq(s string) string {
return strings.Replace(s, seqPlaceHolder, seq(), -1)
}
func replaceRand(s string) string {
return strings.Replace(s, randPlaceHolder, random(), -1)
}
func replace(s string) string {
if strings.Index(s, seqPlaceHolder) >= 0 {
s = replaceSeq(s)
}
if strings.Index(s, randPlaceHolder) >= 0 {
s = replaceRand(s)
}
return s
}
type options struct {
raw bool
verbose bool
limit int
keyOnly bool
}
type Client struct {
opt options
s kv.Storage
raw *RawTiKVClient
args []string
}
func New(flag *fperf.FlagSet) fperf.Client {
c := &Client{}
flag.BoolVar(&c.opt.raw, "raw", false, "using the raw client, notice that the raw client and txn client must not be used together")
flag.IntVar(&c.opt.limit, "n", 0, "scan limit")
flag.BoolVar(&c.opt.verbose, "v", false, "verbose output")
flag.BoolVar(&c.opt.keyOnly, "keyonly", false, "fetch key only")
flag.Parse()
c.args = flag.Args()
if len(c.args) == 0 {
flag.Usage()
os.Exit(0)
}
return c
}
func (c *Client) Dial(addr string) error {
if !c.opt.verbose {
logrus.SetOutput(ioutil.Discard)
}
s, err := tikv.Driver{}.Open(addr)
if err != nil {
return err
}
raw, err := NewRawTiKVClient(addr, WithCodec(&TiKVCodec{}))
if err != nil {
return err
}
c.s = s
c.raw = raw
return nil
}
func (c *Client) Request() error {
args := make([]string, len(c.args))
for i := range c.args {
args[i] = replace(c.args[i])
}
return Call(c, args)
}
//Register to fperf
func init() {
//rand.Seed(time.Now().UnixNano())
fperf.Register("tikv", New, "tikv performance benchmark")
}