forked from influxdata/influx-stress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
222 lines (182 loc) · 4.11 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package write
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/valyala/fasthttp"
)
const (
DefaultDatabase = "stress"
DefaultRetentionPolicy = "autogen"
)
type ClientConfig struct {
BaseURL string
WritePath string
Database string
RetentionPolicy string
User string
Pass string
Precision string
Consistency string
TLSSkipVerify bool
Gzip bool
}
type Client interface {
Create(string) error
Send([]byte) (latNs int64, statusCode int, body string, err error)
Close() error
}
type client struct {
url []byte
cfg ClientConfig
httpClient *fasthttp.Client
}
func NewClient(cfg ClientConfig) Client {
var httpClient *fasthttp.Client
if cfg.TLSSkipVerify {
httpClient = &fasthttp.Client{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
return &client{
url: []byte(writeURLFromConfig(cfg)),
cfg: cfg,
httpClient: httpClient,
}
}
func (c *client) Create(command string) error {
if command == "" {
command = "CREATE DATABASE " + c.cfg.Database
}
vals := url.Values{}
vals.Set("q", command)
u, err := url.Parse(c.cfg.BaseURL)
if err != nil {
return err
}
if c.cfg.User != "" && c.cfg.Pass != "" {
u.User = url.UserPassword(c.cfg.User, c.cfg.Pass)
}
resp, err := http.PostForm(u.String()+"/query", vals)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf(
"Bad status code during Create(%s): %d, body: %s",
command, resp.StatusCode, string(body),
)
}
return nil
}
func (c *client) Send(b []byte) (latNs int64, statusCode int, body string, err error) {
req := fasthttp.AcquireRequest()
req.Header.SetContentTypeBytes([]byte("text/plain"))
req.Header.SetMethodBytes([]byte("POST"))
req.Header.SetRequestURIBytes(c.url)
if c.cfg.Gzip {
req.Header.SetBytesKV([]byte("Content-Encoding"), []byte("gzip"))
}
req.Header.SetContentLength(len(b))
req.SetBody(b)
resp := fasthttp.AcquireResponse()
start := time.Now()
do := fasthttp.Do
if c.httpClient != nil {
do = c.httpClient.Do
}
err = do(req, resp)
latNs = time.Since(start).Nanoseconds()
statusCode = resp.StatusCode()
// Save the body.
if statusCode != http.StatusNoContent {
body = string(resp.Body())
}
fasthttp.ReleaseResponse(resp)
fasthttp.ReleaseRequest(req)
return
}
func (c *client) Close() error {
// Nothing to do.
return nil
}
type fileClient struct {
database string
mu sync.Mutex
f *os.File
batch uint
}
func NewFileClient(path string, cfg ClientConfig) (Client, error) {
c := &fileClient{}
var err error
c.f, err = os.Create(path)
if err != nil {
return nil, err
}
if _, err := c.f.WriteString("# " + writeURLFromConfig(cfg) + "\n"); err != nil {
return nil, err
}
return c, nil
}
func (c *fileClient) Create(command string) error {
if command == "" {
command = "CREATE DATABASE " + c.database
}
c.mu.Lock()
_, err := fmt.Fprintf(c.f, "# create: %s\n\n", command)
c.mu.Unlock()
return err
}
func (c *fileClient) Send(b []byte) (latNs int64, statusCode int, body string, err error) {
c.mu.Lock()
defer c.mu.Unlock()
statusCode = -1
start := time.Now()
defer func() {
latNs = time.Since(start).Nanoseconds()
}()
c.batch++
if _, err = fmt.Fprintf(c.f, "# Batch %d:\n", c.batch); err != nil {
return
}
if _, err = c.f.Write(b); err != nil {
return
}
if _, err = c.f.Write([]byte{'\n'}); err != nil {
return
}
statusCode = 204
return
}
func (c *fileClient) Close() error {
return c.f.Close()
}
func writeURLFromConfig(cfg ClientConfig) string {
params := url.Values{}
params.Set("db", cfg.Database)
if cfg.User != "" {
params.Set("u", cfg.User)
}
if cfg.Pass != "" {
params.Set("p", cfg.Pass)
}
if cfg.RetentionPolicy != "" {
params.Set("rp", cfg.RetentionPolicy)
}
if cfg.Precision != "n" && cfg.Precision != "" {
params.Set("precision", cfg.Precision)
}
if cfg.Consistency != "one" && cfg.Consistency != "" {
params.Set("consistency", cfg.Consistency)
}
return cfg.BaseURL + cfg.WritePath + "?" + params.Encode()
}