forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_test.go
297 lines (272 loc) · 7.99 KB
/
point_test.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"bytes"
"fmt"
"math/rand"
"strconv"
"strings"
"testing"
"time"
lp "github.com/influxdata/line-protocol"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var points []*Point
func init() {
points = make([]*Point, 5000)
rand.Seed(321)
t := time.Now()
for i := 0; i < len(points); i++ {
points[i] = NewPoint(
"test",
map[string]string{
"id": fmt.Sprintf("rack_%v", i%100),
"vendor": "AWS",
"hostname": fmt.Sprintf("host_%v", i%10),
},
map[string]interface{}{
"temperature": rand.Float64() * 80.0,
"disk_free": rand.Float64() * 1000.0,
"disk_total": (i/10 + 1) * 1000000,
"mem_total": (i/100 + 1) * 10000000,
"mem_free": rand.Float64() * 10000000.0,
},
t)
if i%10 == 0 {
t = t.Add(time.Second)
}
}
}
// ToLineProtocol creates InfluxDB line protocol string from the Point, converting associated timestamp according to precision
// and write result to the string builder
func (m *Point) ToLineProtocolBuffer(sb *strings.Builder, precision time.Duration) {
escapeKey(sb, m.Name())
sb.WriteRune(',')
for i, t := range m.tags {
if i > 0 {
sb.WriteString(",")
}
escapeKey(sb, t.Key)
sb.WriteString("=")
escapeKey(sb, t.Value)
}
sb.WriteString(" ")
for i, f := range m.fields {
if i > 0 {
sb.WriteString(",")
}
escapeKey(sb, f.Key)
sb.WriteString("=")
switch f.Value.(type) {
case string:
sb.WriteString(`"`)
escapeValue(sb, f.Value.(string))
sb.WriteString(`"`)
default:
sb.WriteString(fmt.Sprintf("%v", f.Value))
}
switch f.Value.(type) {
case int64:
sb.WriteString("i")
case uint64:
sb.WriteString("u")
}
}
if !m.timestamp.IsZero() {
sb.WriteString(" ")
switch precision {
case time.Microsecond:
sb.WriteString(strconv.FormatInt(m.Time().UnixNano()/1000, 10))
case time.Millisecond:
sb.WriteString(strconv.FormatInt(m.Time().UnixNano()/1000000, 10))
case time.Second:
sb.WriteString(strconv.FormatInt(m.Time().Unix(), 10))
default:
sb.WriteString(strconv.FormatInt(m.Time().UnixNano(), 10))
}
}
sb.WriteString("\n")
}
// ToLineProtocol creates InfluxDB line protocol string from the Point, converting associated timestamp according to precision
func (m *Point) ToLineProtocol(precision time.Duration) string {
var sb strings.Builder
sb.Grow(1024)
m.ToLineProtocolBuffer(&sb, precision)
return sb.String()
}
func escapeKey(sb *strings.Builder, key string) {
for _, r := range key {
switch r {
case ' ', ',', '=':
sb.WriteString(`\`)
}
sb.WriteRune(r)
}
}
func escapeValue(sb *strings.Builder, value string) {
for _, r := range value {
switch r {
case '\\', '"':
sb.WriteString(`\`)
}
sb.WriteRune(r)
}
}
func TestNewPoint(t *testing.T) {
p := NewPoint(
"test",
map[string]string{
"id": "10ad=",
"ven=dor": "AWS",
`host"name`: `ho\st "a"`,
`x\" x`: "a b",
},
map[string]interface{}{
"float64": 80.1234567,
"float32": float32(80.0),
"int": -1234567890,
"int8": int8(-34),
"int16": int16(-3456),
"int32": int32(-34567),
"int64": int64(-1234567890),
"uint": uint(12345677890),
"uint8": uint8(34),
"uint16": uint16(3456),
"uint32": uint32(34578),
"uint 64": uint64(41234567890),
"bo\\ol": false,
`"string"`: `six, "seven", eight`,
"stri=ng": `six=seven\, eight`,
"time": time.Date(2020, time.March, 20, 10, 30, 23, 123456789, time.UTC),
"duration": 4*time.Hour + 24*time.Minute + 3*time.Second,
},
time.Unix(60, 70))
verifyPoint(t, p)
}
func verifyPoint(t *testing.T, p *Point) {
assert.Equal(t, p.Name(), "test")
require.Len(t, p.TagList(), 4)
assert.Equal(t, p.TagList(), []*lp.Tag{
{Key: `host"name`, Value: "ho\\st \"a\""},
{Key: "id", Value: "10ad="},
{Key: `ven=dor`, Value: "AWS"},
{Key: "x\\\" x", Value: "a b"}})
require.Len(t, p.FieldList(), 17)
assert.Equal(t, p.FieldList(), []*lp.Field{
{Key: `"string"`, Value: `six, "seven", eight`},
{Key: "bo\\ol", Value: false},
{Key: "duration", Value: "4h24m3s"},
{Key: "float32", Value: 80.0},
{Key: "float64", Value: 80.1234567},
{Key: "int", Value: int64(-1234567890)},
{Key: "int16", Value: int64(-3456)},
{Key: "int32", Value: int64(-34567)},
{Key: "int64", Value: int64(-1234567890)},
{Key: "int8", Value: int64(-34)},
{Key: "stri=ng", Value: `six=seven\, eight`},
{Key: "time", Value: "2020-03-20T10:30:23.123456789Z"},
{Key: "uint", Value: uint64(12345677890)},
{Key: "uint 64", Value: uint64(41234567890)},
{Key: "uint16", Value: uint64(3456)},
{Key: "uint32", Value: uint64(34578)},
{Key: "uint8", Value: uint64(34)},
})
line := p.ToLineProtocol(time.Nanosecond)
assert.True(t, strings.HasSuffix(line, "\n"))
//cut off last \n char
line = line[:len(line)-1]
assert.Equal(t, line, `test,host"name=ho\st\ "a",id=10ad\=,ven\=dor=AWS,x\"\ x=a\ b "string"="six, \"seven\", eight",bo\ol=false,duration="4h24m3s",float32=80,float64=80.1234567,int=-1234567890i,int16=-3456i,int32=-34567i,int64=-1234567890i,int8=-34i,stri\=ng="six=seven\\, eight",time="2020-03-20T10:30:23.123456789Z",uint=12345677890u,uint\ 64=41234567890u,uint16=3456u,uint32=34578u,uint8=34u 60000000070`)
}
func TestPointAdd(t *testing.T) {
p := NewPointWithMeasurement("test")
p.AddTag("id", "10ad=")
p.AddTag("ven=dor", "AWS")
p.AddTag(`host"name`, "host_a")
//test re-setting same tag
p.AddTag(`host"name`, `ho\st "a"`)
p.AddTag(`x\" x`, "a b")
p.SortTags()
p.AddField("float64", 80.1234567)
p.AddField("float32", float32(80.0))
p.AddField("int", -1234567890)
p.AddField("int8", int8(-34))
p.AddField("int16", int16(-3456))
p.AddField("int32", int32(-34567))
p.AddField("int64", int64(-1234567890))
p.AddField("uint", uint(12345677890))
p.AddField("uint8", uint8(34))
p.AddField("uint16", uint16(3456))
p.AddField("uint32", uint32(34578))
p.AddField("uint 64", uint64(0))
// test re-setting same field
p.AddField("uint 64", uint64(41234567890))
p.AddField("bo\\ol", false)
p.AddField(`"string"`, `six, "seven", eight`)
p.AddField("stri=ng", `six=seven\, eight`)
p.AddField("time", time.Date(2020, time.March, 20, 10, 30, 23, 123456789, time.UTC))
p.AddField("duration", time.Duration(4*time.Hour+24*time.Minute+3*time.Second))
p.SortFields()
p.SetTime(time.Unix(60, 70))
verifyPoint(t, p)
}
func TestPrecision(t *testing.T) {
p := NewPointWithMeasurement("test")
p.AddTag("id", "10")
p.AddField("float64", 80.1234567)
p.SetTime(time.Unix(60, 89))
line := p.ToLineProtocol(time.Nanosecond)
assert.Equal(t, line, "test,id=10 float64=80.1234567 60000000089\n")
p.SetTime(time.Unix(60, 56789))
line = p.ToLineProtocol(time.Microsecond)
assert.Equal(t, line, "test,id=10 float64=80.1234567 60000056\n")
p.SetTime(time.Unix(60, 123456789))
line = p.ToLineProtocol(time.Millisecond)
assert.Equal(t, line, "test,id=10 float64=80.1234567 60123\n")
p.SetTime(time.Unix(60, 123456789))
line = p.ToLineProtocol(time.Second)
assert.Equal(t, line, "test,id=10 float64=80.1234567 60\n")
}
var s string
func BenchmarkPointEncoderSingle(b *testing.B) {
for n := 0; n < b.N; n++ {
var buff strings.Builder
for _, p := range points {
var buffer bytes.Buffer
e := lp.NewEncoder(&buffer)
e.Encode(p)
buff.WriteString(buffer.String())
}
s = buff.String()
}
}
func BenchmarkPointEncoderMulti(b *testing.B) {
for n := 0; n < b.N; n++ {
var buffer bytes.Buffer
e := lp.NewEncoder(&buffer)
for _, p := range points {
e.Encode(p)
}
s = buffer.String()
}
}
func BenchmarkPointStringSingle(b *testing.B) {
for n := 0; n < b.N; n++ {
var buff strings.Builder
for _, p := range points {
buff.WriteString(p.ToLineProtocol(time.Nanosecond))
}
s = buff.String()
}
}
func BenchmarkPointStringMulti(b *testing.B) {
for n := 0; n < b.N; n++ {
var buff strings.Builder
for _, p := range points {
p.ToLineProtocolBuffer(&buff, time.Nanosecond)
}
s = buff.String()
}
}