points_test.go
61 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
package models_test
import (
"bytes"
"fmt"
"io"
"math"
"math/rand"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/influxdata/influxdb/models"
)
var (
tags = models.NewTags(map[string]string{"foo": "bar", "apple": "orange", "host": "serverA", "region": "uswest"})
fields = models.Fields{
"int64": int64(math.MaxInt64),
"uint32": uint32(math.MaxUint32),
"string": "String field that has a decent length, probably some log message or something",
"boolean": false,
"float64-tiny": float64(math.SmallestNonzeroFloat64),
"float64-large": float64(math.MaxFloat64),
}
maxFloat64 = strconv.FormatFloat(math.MaxFloat64, 'f', 1, 64)
minFloat64 = strconv.FormatFloat(-math.MaxFloat64, 'f', 1, 64)
sink interface{}
)
func TestMarshal(t *testing.T) {
got := tags.HashKey()
if exp := ",apple=orange,foo=bar,host=serverA,region=uswest"; string(got) != exp {
t.Log("got: ", string(got))
t.Log("exp: ", exp)
t.Error("invalid match")
}
}
func TestTags_HashKey(t *testing.T) {
tags = models.NewTags(map[string]string{"A FOO": "bar", "APPLE": "orange", "host": "serverA", "region": "uswest"})
got := tags.HashKey()
if exp := ",A\\ FOO=bar,APPLE=orange,host=serverA,region=uswest"; string(got) != exp {
t.Log("got: ", string(got))
t.Log("exp: ", exp)
t.Error("invalid match")
}
}
func BenchmarkMarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
tags.HashKey()
}
}
func TestPoint_StringSize(t *testing.T) {
testPoint_cube(t, func(p models.Point) {
l := p.StringSize()
s := p.String()
if l != len(s) {
t.Errorf("Incorrect length for %q. got %v, exp %v", s, l, len(s))
}
})
}
func TestPoint_AppendString(t *testing.T) {
testPoint_cube(t, func(p models.Point) {
got := p.AppendString(nil)
exp := []byte(p.String())
if !reflect.DeepEqual(exp, got) {
t.Errorf("AppendString() didn't match String(): got %v, exp %v", got, exp)
}
})
}
func testPoint_cube(t *testing.T, f func(p models.Point)) {
// heard of a table-driven test? let's make a cube-driven test...
tagList := []models.Tags{nil, {models.NewTag([]byte("foo"), []byte("bar"))}, tags}
fieldList := []models.Fields{{"a": 42.0}, {"a": 42, "b": "things"}, fields}
timeList := []time.Time{time.Time{}, time.Unix(0, 0), time.Unix(-34526, 0), time.Unix(231845, 0), time.Now()}
for _, tagSet := range tagList {
for _, fieldSet := range fieldList {
for _, pointTime := range timeList {
p, err := models.NewPoint("test", tagSet, fieldSet, pointTime)
if err != nil {
t.Errorf("unexpected error creating point: %v", err)
continue
}
f(p)
}
}
}
}
func TestTag_Clone(t *testing.T) {
tag := models.NewTag([]byte("key"), []byte("value"))
c := tag.Clone()
if &c.Key == &tag.Key || !bytes.Equal(c.Key, tag.Key) {
t.Fatalf("key %s should have been a clone of %s", c.Key, tag.Key)
}
if &c.Value == &tag.Value || !bytes.Equal(c.Value, tag.Value) {
t.Fatalf("value %s should have been a clone of %s", c.Value, tag.Value)
}
}
func TestTags_Clone(t *testing.T) {
tags := models.NewTags(map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"})
clone := tags.Clone()
for i := range tags {
tag := tags[i]
c := clone[i]
if &c.Key == &tag.Key || !bytes.Equal(c.Key, tag.Key) {
t.Fatalf("key %s should have been a clone of %s", c.Key, tag.Key)
}
if &c.Value == &tag.Value || !bytes.Equal(c.Value, tag.Value) {
t.Fatalf("value %s should have been a clone of %s", c.Value, tag.Value)
}
}
}
var p models.Point
func BenchmarkNewPoint(b *testing.B) {
ts := time.Now()
for i := 0; i < b.N; i++ {
p, _ = models.NewPoint("measurement", tags, fields, ts)
}
}
func BenchmarkNewPointFromBinary(b *testing.B) {
pts, err := models.ParsePointsString("cpu value1=1.0,value2=1.0,value3=3.0,value4=4,value5=\"five\" 1000000000")
if err != nil {
b.Fatalf("unexpected error ParsePointsString: %v", err)
}
bytes, err := pts[0].MarshalBinary()
if err != nil {
b.Fatalf("unexpected error MarshalBinary: %v", err)
}
for i := 0; i < b.N; i++ {
_, err := models.NewPointFromBytes(bytes)
if err != nil {
b.Fatalf("unexpected error NewPointsFromBytes: %v", err)
}
}
}
func BenchmarkParsePointNoTags5000(b *testing.B) {
var batch [5000]string
for i := 0; i < len(batch); i++ {
batch[i] = `cpu value=1i 1000000000`
}
lines := strings.Join(batch[:], "\n")
b.ResetTimer()
for i := 0; i < b.N; i++ {
models.ParsePoints([]byte(lines))
b.SetBytes(int64(len(lines)))
}
}
func BenchmarkParsePointNoTags(b *testing.B) {
line := `cpu value=1i 1000000000`
for i := 0; i < b.N; i++ {
models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
}
}
func BenchmarkParsePointWithPrecisionN(b *testing.B) {
line := `cpu value=1i 1000000000`
defaultTime := time.Now().UTC()
for i := 0; i < b.N; i++ {
models.ParsePointsWithPrecision([]byte(line), defaultTime, "n")
b.SetBytes(int64(len(line)))
}
}
func BenchmarkParsePointWithPrecisionU(b *testing.B) {
line := `cpu value=1i 1000000000`
defaultTime := time.Now().UTC()
for i := 0; i < b.N; i++ {
models.ParsePointsWithPrecision([]byte(line), defaultTime, "u")
b.SetBytes(int64(len(line)))
}
}
func BenchmarkParsePointsTagsSorted2(b *testing.B) {
line := `cpu,host=serverA,region=us-west value=1i 1000000000`
for i := 0; i < b.N; i++ {
models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
}
}
func BenchmarkParsePointsTagsSorted5(b *testing.B) {
line := `cpu,env=prod,host=serverA,region=us-west,target=servers,zone=1c value=1i 1000000000`
for i := 0; i < b.N; i++ {
models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
}
}
func BenchmarkParsePointsTagsSorted10(b *testing.B) {
line := `cpu,env=prod,host=serverA,region=us-west,tag1=value1,tag2=value2,tag3=value3,tag4=value4,tag5=value5,target=servers,zone=1c value=1i 1000000000`
for i := 0; i < b.N; i++ {
models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
}
}
func BenchmarkParsePointsTagsUnSorted2(b *testing.B) {
line := `cpu,region=us-west,host=serverA value=1i 1000000000`
for i := 0; i < b.N; i++ {
pt, _ := models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
pt[0].Key()
}
}
func BenchmarkParsePointsTagsUnSorted5(b *testing.B) {
line := `cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c value=1i 1000000000`
for i := 0; i < b.N; i++ {
pt, _ := models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
pt[0].Key()
}
}
func BenchmarkParsePointsTagsUnSorted10(b *testing.B) {
line := `cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c,tag1=value1,tag2=value2,tag3=value3,tag4=value4,tag5=value5 value=1i 1000000000`
for i := 0; i < b.N; i++ {
pt, _ := models.ParsePoints([]byte(line))
b.SetBytes(int64(len(line)))
pt[0].Key()
}
}
func BenchmarkParseKey(b *testing.B) {
line := `cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c,tag1=value1,tag2=value2,tag3=value3,tag4=value4,tag5=value5`
for i := 0; i < b.N; i++ {
models.ParseKey([]byte(line))
}
}
// TestPoint wraps a models.Point but also makes available the raw
// arguments to the Point.
//
// This is useful for ensuring that comparisons between results of
// operations on Points match the expected input data to the Point,
// since models.Point does not expose the raw input data (e.g., tags)
// via its API.
type TestPoint struct {
RawFields models.Fields
RawTags models.Tags
RawTime time.Time
models.Point
}
// NewTestPoint returns a new TestPoint.
//
// NewTestPoint panics if it is not a valid models.Point.
func NewTestPoint(name string, tags models.Tags, fields models.Fields, time time.Time) TestPoint {
return TestPoint{
RawTags: tags,
RawFields: fields,
RawTime: time,
Point: models.MustNewPoint(name, tags, fields, time),
}
}
func test(t *testing.T, line string, point TestPoint) {
pts, err := models.ParsePointsWithPrecision([]byte(line), time.Unix(0, 0), "n")
if err != nil {
t.Fatalf(`ParsePoints("%s") mismatch. got %v, exp nil`, line, err)
}
if exp := 1; len(pts) != exp {
t.Fatalf(`ParsePoints("%s") len mismatch. got %d, exp %d`, line, len(pts), exp)
}
if exp := point.Key(); !bytes.Equal(pts[0].Key(), exp) {
t.Errorf("ParsePoints(\"%s\") key mismatch.\ngot %v\nexp %v", line, string(pts[0].Key()), string(exp))
}
if exp := len(point.Tags()); len(pts[0].Tags()) != exp {
t.Errorf(`ParsePoints("%s") tags mismatch. got %v, exp %v`, line, pts[0].Tags(), exp)
}
for _, tag := range pts[0].Tags() {
if !bytes.Equal(tag.Value, point.RawTags.Get(tag.Key)) {
t.Errorf(`ParsePoints("%s") tags mismatch. got %s, exp %s`, line, tag.Value, point.RawTags.Get(tag.Key))
}
}
for name, value := range point.RawFields {
fields, err := pts[0].Fields()
if err != nil {
t.Fatal(err)
}
val := fields[name]
expfval, ok := val.(float64)
if ok && math.IsNaN(expfval) {
gotfval, ok := value.(float64)
if ok && !math.IsNaN(gotfval) {
t.Errorf(`ParsePoints("%s") field '%s' mismatch. exp NaN`, line, name)
}
}
if !reflect.DeepEqual(val, value) {
t.Errorf(`ParsePoints("%s") field '%s' mismatch. got %[3]v (%[3]T), exp %[4]v (%[4]T)`, line, name, val, value)
}
}
if !pts[0].Time().Equal(point.Time()) {
t.Errorf(`ParsePoints("%s") time mismatch. got %v, exp %v`, line, pts[0].Time(), point.Time())
}
if !strings.HasPrefix(pts[0].String(), line) {
t.Errorf("ParsePoints string mismatch.\ngot: %v\nexp: %v", pts[0].String(), line)
}
}
func TestParsePointNoValue(t *testing.T) {
pts, err := models.ParsePointsString("")
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, "", err)
}
if exp := 0; len(pts) != exp {
t.Errorf(`ParsePoints("%s") len mismatch. got %v, exp %v`, "", len(pts), exp)
}
}
func TestParsePointWhitespaceValue(t *testing.T) {
pts, err := models.ParsePointsString(" ")
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, "", err)
}
if exp := 0; len(pts) != exp {
t.Errorf(`ParsePoints("%s") len mismatch. got %v, exp %v`, "", len(pts), exp)
}
}
func TestParsePointNoFields(t *testing.T) {
expectedSuffix := "missing fields"
examples := []string{
"cpu_load_short,host=server01,region=us-west",
"cpu",
"cpu,host==",
"=",
}
for i, example := range examples {
_, err := models.ParsePointsString(example)
if err == nil {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got nil, exp error`, i, example)
} else if !strings.HasSuffix(err.Error(), expectedSuffix) {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got %q, exp suffix %q`, i, example, err, expectedSuffix)
}
}
}
func TestParsePointNoTimestamp(t *testing.T) {
test(t, "cpu value=1", NewTestPoint("cpu", nil, models.Fields{"value": 1.0}, time.Unix(0, 0)))
}
func TestParsePointMissingQuote(t *testing.T) {
expectedSuffix := "unbalanced quotes"
examples := []string{
`cpu,host=serverA value="test`,
`cpu,host=serverA value="test""`,
}
for i, example := range examples {
_, err := models.ParsePointsString(example)
if err == nil {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got nil, exp error`, i, example)
} else if !strings.HasSuffix(err.Error(), expectedSuffix) {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got %q, exp suffix %q`, i, example, err, expectedSuffix)
}
}
}
func TestParsePointMissingTagKey(t *testing.T) {
expectedSuffix := "missing tag key"
examples := []string{
`cpu, value=1`,
`cpu,`,
`cpu,,,`,
`cpu,host=serverA,=us-east value=1i`,
`cpu,host=serverAa\,,=us-east value=1i`,
`cpu,host=serverA\,,=us-east value=1i`,
`cpu, =serverA value=1i`,
}
for i, example := range examples {
_, err := models.ParsePointsString(example)
if err == nil {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got nil, exp error`, i, example)
} else if !strings.HasSuffix(err.Error(), expectedSuffix) {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got %q, exp suffix %q`, i, example, err, expectedSuffix)
}
}
_, err := models.ParsePointsString(`cpu,host=serverA,\ =us-east value=1i`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,\ =us-east value=1i`, err)
}
}
func TestParsePointMissingTagValue(t *testing.T) {
expectedSuffix := "missing tag value"
examples := []string{
`cpu,host`,
`cpu,host,`,
`cpu,host=`,
`cpu,host value=1i`,
`cpu,host=serverA,region value=1i`,
`cpu,host=serverA,region= value=1i`,
`cpu,host=serverA,region=,zone=us-west value=1i`,
}
for i, example := range examples {
_, err := models.ParsePointsString(example)
if err == nil {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got nil, exp error`, i, example)
} else if !strings.HasSuffix(err.Error(), expectedSuffix) {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got %q, exp suffix %q`, i, example, err, expectedSuffix)
}
}
}
func TestParsePointInvalidTagFormat(t *testing.T) {
expectedSuffix := "invalid tag format"
examples := []string{
`cpu,host=f=o,`,
`cpu,host=f\==o,`,
}
for i, example := range examples {
_, err := models.ParsePointsString(example)
if err == nil {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got nil, exp error`, i, example)
} else if !strings.HasSuffix(err.Error(), expectedSuffix) {
t.Errorf(`[Example %d] ParsePoints("%s") mismatch. got %q, exp suffix %q`, i, example, err, expectedSuffix)
}
}
}
func TestParsePointMissingFieldName(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west =`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west =`)
}
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west =123i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west =123i`)
}
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west a\ =123i`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west a\ =123i`)
}
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value=123i,=456i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=123i,=456i`)
}
}
func TestParsePointMissingFieldValue(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=`)
}
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value= 1000000000i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value= 1000000000i`)
}
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value=,value2=1i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=,value2=1i`)
}
_, err = models.ParsePointsString(`cpu,host=server01,region=us-west 1434055562000000000i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=server01,region=us-west 1434055562000000000i`)
}
_, err = models.ParsePointsString(`cpu,host=server01,region=us-west value=1i,b`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=server01,region=us-west value=1i,b`)
}
}
func TestParsePointBadNumber(t *testing.T) {
for _, tt := range []string{
"cpu v=- ",
"cpu v=-i ",
"cpu v=-. ",
"cpu v=. ",
"cpu v=1.0i ",
"cpu v=1ii ",
"cpu v=1a ",
"cpu v=-e-e-e ",
"cpu v=42+3 ",
"cpu v= ",
} {
_, err := models.ParsePointsString(tt)
if err == nil {
t.Errorf("Point %q should be invalid", tt)
}
}
}
func TestParsePointMaxInt64(t *testing.T) {
// out of range
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=9223372036854775808i`)
exp := `unable to parse 'cpu,host=serverA,region=us-west value=9223372036854775808i': unable to parse integer 9223372036854775808: strconv.ParseInt: parsing "9223372036854775808": value out of range`
if err == nil || (err != nil && err.Error() != exp) {
t.Fatalf("Error mismatch:\nexp: %s\ngot: %v", exp, err)
}
// max int
p, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=9223372036854775807i`)
if err != nil {
t.Fatalf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=9223372036854775807i`, err)
}
fields, err := p[0].Fields()
if err != nil {
t.Fatal(err)
}
if exp, got := int64(9223372036854775807), fields["value"].(int64); exp != got {
t.Fatalf("ParsePoints Value mismatch. \nexp: %v\ngot: %v", exp, got)
}
// leading zeros
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value=0009223372036854775807i`)
if err != nil {
t.Fatalf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=0009223372036854775807i`, err)
}
}
func TestParsePointMinInt64(t *testing.T) {
// out of range
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=-9223372036854775809i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=-9223372036854775809i`)
}
// min int
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value=-9223372036854775808i`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=-9223372036854775808i`, err)
}
// leading zeros
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value=-0009223372036854775808i`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=-0009223372036854775808i`, err)
}
}
func TestParsePointMaxFloat64(t *testing.T) {
// out of range
_, err := models.ParsePointsString(fmt.Sprintf(`cpu,host=serverA,region=us-west value=%s`, "1"+string(maxFloat64)))
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=...`)
}
// max float
_, err = models.ParsePointsString(fmt.Sprintf(`cpu,host=serverA,region=us-west value=%s`, string(maxFloat64)))
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=9223372036854775807`, err)
}
// leading zeros
_, err = models.ParsePointsString(fmt.Sprintf(`cpu,host=serverA,region=us-west value=%s`, "0000"+string(maxFloat64)))
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=0009223372036854775807`, err)
}
}
func TestParsePointMinFloat64(t *testing.T) {
// out of range
_, err := models.ParsePointsString(fmt.Sprintf(`cpu,host=serverA,region=us-west value=%s`, "-1"+string(minFloat64)[1:]))
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=...`)
}
// min float
_, err = models.ParsePointsString(fmt.Sprintf(`cpu,host=serverA,region=us-west value=%s`, string(minFloat64)))
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=...`, err)
}
// leading zeros
_, err = models.ParsePointsString(fmt.Sprintf(`cpu,host=serverA,region=us-west value=%s`, "-0000000"+string(minFloat64)[1:]))
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=...`, err)
}
}
func TestParsePointNumberNonNumeric(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=.1a`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=.1a`)
}
}
func TestParsePointNegativeWrongPlace(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=0.-1`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=0.-1`)
}
}
func TestParsePointOnlyNegativeSign(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=-`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=-`)
}
}
func TestParsePointFloatMultipleDecimals(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1.1.1`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=1.1.1`)
}
}
func TestParsePointInteger(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1i`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1i`, err)
}
}
func TestParsePointNegativeInteger(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=-1i`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=-1i`, err)
}
}
func TestParsePointNegativeFloat(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=-1.0`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=-1.0`, err)
}
}
func TestParsePointFloatNoLeadingDigit(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=.1`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=-1.0`, err)
}
}
func TestParsePointFloatScientific(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1.0e4`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1.0e4`, err)
}
pts, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1e4`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1.0e4`, err)
}
fields, err := pts[0].Fields()
if err != nil {
t.Fatal(err)
}
if fields["value"] != 1e4 {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1e4`, err)
}
}
func TestParsePointFloatScientificUpper(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1.0E4`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1.0E4`, err)
}
pts, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1E4`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1.0E4`, err)
}
fields, err := pts[0].Fields()
if err != nil {
t.Fatal(err)
}
if fields["value"] != 1e4 {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1E4`, err)
}
}
func TestParsePointFloatScientificDecimal(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=1.0e-4`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=1.0e-4`, err)
}
}
func TestParsePointFloatNegativeScientific(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=-1.0e-4`)
if err != nil {
t.Errorf(`ParsePoints("%s") mismatch. got %v, exp nil`, `cpu,host=serverA,region=us-west value=-1.0e-4`, err)
}
}
func TestParsePointBooleanInvalid(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=a`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=a`)
}
}
func TestParsePointScientificIntInvalid(t *testing.T) {
_, err := models.ParsePointsString(`cpu,host=serverA,region=us-west value=9ie10`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=9ie10`)
}
_, err = models.ParsePointsString(`cpu,host=serverA,region=us-west value=9e10i`)
if err == nil {
t.Errorf(`ParsePoints("%s") mismatch. got nil, exp error`, `cpu,host=serverA,region=us-west value=9e10i`)
}
}
func TestParsePointWhitespace(t *testing.T) {
examples := []string{
`cpu value=1.0 1257894000000000000`,
`cpu value=1.0 1257894000000000000`,
`cpu value=1.0 1257894000000000000`,
`cpu value=1.0 1257894000000000000 `,
`cpu value=1.0 1257894000000000000
`,
`cpu value=1.0 1257894000000000000
`,
}
expPoint := NewTestPoint("cpu", models.Tags{}, models.Fields{"value": 1.0}, time.Unix(0, 1257894000000000000))
for i, example := range examples {
pts, err := models.ParsePoints([]byte(example))
if err != nil {
t.Fatalf(`[Example %d] ParsePoints("%s") error. got %v, exp nil`, i, example, err)
}
if got, exp := len(pts), 1; got != exp {
t.Fatalf("[Example %d] got %d points, expected %d", i, got, exp)
}
if got, exp := string(pts[0].Name()), string(expPoint.Name()); got != exp {
t.Fatalf("[Example %d] got %v measurement, expected %v", i, got, exp)
}
fields, err := pts[0].Fields()
if err != nil {
t.Fatal(err)
}
eFields, err := expPoint.Fields()
if err != nil {
t.Fatal(err)
}
if got, exp := len(fields), len(eFields); got != exp {
t.Fatalf("[Example %d] got %d fields, expected %d", i, got, exp)
}
if got, exp := fields["value"], eFields["value"]; got != exp {
t.Fatalf(`[Example %d] got %v for field "value", expected %v`, i, got, exp)
}
if got, exp := pts[0].Time().UnixNano(), expPoint.Time().UnixNano(); got != exp {
t.Fatalf(`[Example %d] got %d time, expected %d`, i, got, exp)
}
}
}
func TestParsePointUnescape(t *testing.T) {
// commas in measurement name
test(t, `foo\,bar value=1i`,
NewTestPoint(
"foo,bar", // comma in the name
models.NewTags(map[string]string{}),
models.Fields{
"value": int64(1),
},
time.Unix(0, 0)))
// comma in measurement name with tags
test(t, `cpu\,main,regions=east value=1.0`,
NewTestPoint(
"cpu,main", // comma in the name
models.NewTags(map[string]string{
"regions": "east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// spaces in measurement name
test(t, `cpu\ load,region=east value=1.0`,
NewTestPoint(
"cpu load", // space in the name
models.NewTags(map[string]string{
"region": "east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// equals in measurement name
test(t, `cpu\=load,region=east value=1.0`,
NewTestPoint(
`cpu\=load`, // backslash is literal
models.NewTags(map[string]string{
"region": "east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// equals in measurement name
test(t, `cpu=load,region=east value=1.0`,
NewTestPoint(
`cpu=load`, // literal equals is fine in measurement name
models.NewTags(map[string]string{
"region": "east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// commas in tag names
test(t, `cpu,region\,zone=east value=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"region,zone": "east", // comma in the tag key
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// spaces in tag name
test(t, `cpu,region\ zone=east value=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"region zone": "east", // space in the tag name
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// backslash with escaped equals in tag name
test(t, `cpu,reg\\=ion=east value=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
`reg\=ion`: "east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// space is tag name
test(t, `cpu,\ =east value=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
" ": "east", // tag name is single space
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// commas in tag values
test(t, `cpu,regions=east\,west value=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"regions": "east,west", // comma in the tag value
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// backslash literal followed by escaped space
test(t, `cpu,regions=\\ east value=1.0`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"regions": `\ east`,
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// backslash literal followed by escaped space
test(t, `cpu,regions=eas\\ t value=1.0`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"regions": `eas\ t`,
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// backslash literal followed by trailing space
test(t, `cpu,regions=east\\ value=1.0`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"regions": `east\ `,
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// spaces in tag values
test(t, `cpu,regions=east\ west value=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"regions": "east west", // comma in the tag value
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// commas in field keys
test(t, `cpu,regions=east value\,ms=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"regions": "east",
}),
models.Fields{
"value,ms": 1.0, // comma in the field keys
},
time.Unix(0, 0)))
// spaces in field keys
test(t, `cpu,regions=east value\ ms=1.0`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"regions": "east",
}),
models.Fields{
"value ms": 1.0, // comma in the field keys
},
time.Unix(0, 0)))
// tag with no value
test(t, `cpu,regions=east value="1"`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"regions": "east",
"foobar": "",
}),
models.Fields{
"value": "1",
},
time.Unix(0, 0)))
// commas in field values
test(t, `cpu,regions=east value="1,0"`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"regions": "east",
}),
models.Fields{
"value": "1,0", // comma in the field value
},
time.Unix(0, 0)))
// random character escaped
test(t, `cpu,regions=eas\t value=1.0`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"regions": "eas\\t",
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// backslash literal followed by escaped characters
test(t, `cpu,regions=\\,\,\=east value=1.0`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"regions": `\,,=east`,
}),
models.Fields{
"value": 1.0,
},
time.Unix(0, 0)))
// field keys using escape char.
test(t, `cpu \a=1i`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"\\a": int64(1), // Left as parsed since it's not a known escape sequence.
},
time.Unix(0, 0)))
// measurement, tag and tag value with equals
test(t, `cpu=load,equals\=foo=tag\=value value=1i`,
NewTestPoint(
"cpu=load", // Not escaped
models.NewTags(map[string]string{
"equals=foo": "tag=value", // Tag and value unescaped
}),
models.Fields{
"value": int64(1),
},
time.Unix(0, 0)))
}
func TestParsePointWithTags(t *testing.T) {
test(t,
"cpu,host=serverA,region=us-east value=1.0 1000000000",
NewTestPoint("cpu",
models.NewTags(map[string]string{"host": "serverA", "region": "us-east"}),
models.Fields{"value": 1.0}, time.Unix(1, 0)))
}
func TestParsePointWithDuplicateTags(t *testing.T) {
for i, tt := range []struct {
line string
err string
}{
{
line: `cpu,host=serverA,host=serverB value=1i 1000000000`,
err: `unable to parse 'cpu,host=serverA,host=serverB value=1i 1000000000': duplicate tags`,
},
{
line: `cpu,b=2,b=1,c=3 value=1i 1000000000`,
err: `unable to parse 'cpu,b=2,b=1,c=3 value=1i 1000000000': duplicate tags`,
},
{
line: `cpu,b=2,c=3,b=1 value=1i 1000000000`,
err: `unable to parse 'cpu,b=2,c=3,b=1 value=1i 1000000000': duplicate tags`,
},
} {
_, err := models.ParsePointsString(tt.line)
if err == nil || tt.err != err.Error() {
t.Errorf("%d. ParsePoint() expected error '%s'. got '%s'", i, tt.err, err)
}
}
}
func TestParsePointWithStringField(t *testing.T) {
test(t, `cpu,host=serverA,region=us-east value=1.0,str="foo",str2="bar" 1000000000`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
"str": "foo",
"str2": "bar",
},
time.Unix(1, 0)),
)
test(t, `cpu,host=serverA,region=us-east str="foo \" bar" 1000000000`,
NewTestPoint("cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"str": `foo " bar`,
},
time.Unix(1, 0)),
)
}
func TestParsePointWithStringWithSpaces(t *testing.T) {
test(t, `cpu,host=serverA,region=us-east value=1.0,str="foo bar" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
"str": "foo bar", // spaces in string value
},
time.Unix(1, 0)),
)
}
func TestParsePointWithStringWithNewline(t *testing.T) {
test(t, "cpu,host=serverA,region=us-east value=1.0,str=\"foo\nbar\" 1000000000",
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
"str": "foo\nbar", // newline in string value
},
time.Unix(1, 0)),
)
}
func TestParsePointWithStringWithCommas(t *testing.T) {
// escaped comma
test(t, `cpu,host=serverA,region=us-east value=1.0,str="foo\,bar" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
"str": `foo\,bar`, // commas in string value
},
time.Unix(1, 0)),
)
// non-escaped comma
test(t, `cpu,host=serverA,region=us-east value=1.0,str="foo,bar" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
"str": "foo,bar", // commas in string value
},
time.Unix(1, 0)),
)
// string w/ trailing escape chars
test(t, `cpu,host=serverA,region=us-east str="foo\\",str2="bar" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"str": "foo\\", // trailing escape char
"str2": "bar",
},
time.Unix(1, 0)),
)
}
func TestParsePointQuotedMeasurement(t *testing.T) {
// non-escaped comma
test(t, `"cpu",host=serverA,region=us-east value=1.0 1000000000`,
NewTestPoint(
`"cpu"`,
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(1, 0)),
)
}
func TestParsePointQuotedTags(t *testing.T) {
test(t, `cpu,"host"="serverA",region=us-east value=1.0 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
`"host"`: `"serverA"`,
"region": "us-east",
}),
models.Fields{
"value": 1.0,
},
time.Unix(1, 0)),
)
}
func TestParsePointsUnbalancedQuotedTags(t *testing.T) {
pts, err := models.ParsePointsString("baz,mytag=\"a x=1 1441103862125\nbaz,mytag=a z=1 1441103862126")
if err != nil {
t.Fatalf("ParsePoints failed: %v", err)
}
if exp := 2; len(pts) != exp {
t.Fatalf("ParsePoints count mismatch. got %v, exp %v", len(pts), exp)
}
// Expected " in the tag value
exp := models.MustNewPoint("baz", models.NewTags(map[string]string{"mytag": `"a`}),
models.Fields{"x": float64(1)}, time.Unix(0, 1441103862125))
if pts[0].String() != exp.String() {
t.Errorf("Point mismatch:\ngot: %v\nexp: %v", pts[0].String(), exp.String())
}
// Expected two points to ensure we did not overscan the line
exp = models.MustNewPoint("baz", models.NewTags(map[string]string{"mytag": `a`}),
models.Fields{"z": float64(1)}, time.Unix(0, 1441103862126))
if pts[1].String() != exp.String() {
t.Errorf("Point mismatch:\ngot: %v\nexp: %v", pts[1].String(), exp.String())
}
}
func TestParsePointEscapedStringsAndCommas(t *testing.T) {
// non-escaped comma and quotes
test(t, `cpu,host=serverA,region=us-east value="{Hello\"{,}\" World}" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": `{Hello"{,}" World}`,
},
time.Unix(1, 0)),
)
// escaped comma and quotes
test(t, `cpu,host=serverA,region=us-east value="{Hello\"{\,}\" World}" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": `{Hello"{\,}" World}`,
},
time.Unix(1, 0)),
)
}
func TestParsePointWithStringWithEquals(t *testing.T) {
test(t, `cpu,host=serverA,region=us-east str="foo=bar",value=1.0 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": 1.0,
"str": "foo=bar", // spaces in string value
},
time.Unix(1, 0)),
)
}
func TestParsePointWithStringWithBackslash(t *testing.T) {
test(t, `cpu value="test\\\"" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": `test\"`,
},
time.Unix(1, 0)),
)
test(t, `cpu value="test\\" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": `test\`,
},
time.Unix(1, 0)),
)
test(t, `cpu value="test\\\"" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": `test\"`,
},
time.Unix(1, 0)),
)
test(t, `cpu value="test\"" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": `test"`,
},
time.Unix(1, 0)),
)
}
func TestParsePointWithBoolField(t *testing.T) {
test(t, `cpu,host=serverA,region=us-east true=true,t=t,T=T,TRUE=TRUE,True=True,false=false,f=f,F=F,FALSE=FALSE,False=False 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"t": true,
"T": true,
"true": true,
"True": true,
"TRUE": true,
"f": false,
"F": false,
"false": false,
"False": false,
"FALSE": false,
},
time.Unix(1, 0)),
)
}
func TestParsePointUnicodeString(t *testing.T) {
test(t, `cpu,host=serverA,region=us-east value="wè" 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{
"host": "serverA",
"region": "us-east",
}),
models.Fields{
"value": "wè",
},
time.Unix(1, 0)),
)
}
func TestParsePointNegativeTimestamp(t *testing.T) {
test(t, `cpu value=1 -1`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": 1.0,
},
time.Unix(0, -1)),
)
}
func TestParsePointMaxTimestamp(t *testing.T) {
test(t, fmt.Sprintf(`cpu value=1 %d`, models.MaxNanoTime),
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": 1.0,
},
time.Unix(0, models.MaxNanoTime)),
)
}
func TestParsePointMinTimestamp(t *testing.T) {
test(t, `cpu value=1 -9223372036854775806`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": 1.0,
},
time.Unix(0, models.MinNanoTime)),
)
}
func TestParsePointInvalidTimestamp(t *testing.T) {
examples := []string{
"cpu value=1 9223372036854775808",
"cpu value=1 -92233720368547758078",
"cpu value=1 -",
"cpu value=1 -/",
"cpu value=1 -1?",
"cpu value=1 1-",
"cpu value=1 9223372036854775807 12",
}
for i, example := range examples {
_, err := models.ParsePointsString(example)
if err == nil {
t.Fatalf("[Example %d] ParsePoints failed: %v", i, err)
}
}
}
func TestNewPointFloatWithoutDecimal(t *testing.T) {
test(t, `cpu value=1 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": 1.0,
},
time.Unix(1, 0)),
)
}
func TestNewPointNegativeFloat(t *testing.T) {
test(t, `cpu value=-0.64 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": -0.64,
},
time.Unix(1, 0)),
)
}
func TestNewPointFloatNoDecimal(t *testing.T) {
test(t, `cpu value=1. 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": 1.0,
},
time.Unix(1, 0)),
)
}
func TestNewPointFloatScientific(t *testing.T) {
test(t, `cpu value=6.632243e+06 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": float64(6632243),
},
time.Unix(1, 0)),
)
}
func TestNewPointLargeInteger(t *testing.T) {
test(t, `cpu value=6632243i 1000000000`,
NewTestPoint(
"cpu",
models.NewTags(map[string]string{}),
models.Fields{
"value": int64(6632243), // if incorrectly encoded as a float, it would show up as 6.632243e+06
},
time.Unix(1, 0)),
)
}
func TestParsePointNaN(t *testing.T) {
_, err := models.ParsePointsString("cpu value=NaN 1000000000")
if err == nil {
t.Fatalf("ParsePoints expected error, got nil")
}
_, err = models.ParsePointsString("cpu value=nAn 1000000000")
if err == nil {
t.Fatalf("ParsePoints expected error, got nil")
}
_, err = models.ParsePointsString("cpu value=NaN")
if err == nil {
t.Fatalf("ParsePoints expected error, got nil")
}
}
func TestNewPointLargeNumberOfTags(t *testing.T) {
tags := ""
for i := 0; i < 255; i++ {
tags += fmt.Sprintf(",tag%d=value%d", i, i)
}
pt, err := models.ParsePointsString(fmt.Sprintf("cpu%s value=1", tags))
if err != nil {
t.Fatalf("ParsePoints() with max tags failed: %v", err)
}
if len(pt[0].Tags()) != 255 {
t.Fatalf("expected %d tags, got %d", 255, len(pt[0].Tags()))
}
}
func TestParsePointIntsFloats(t *testing.T) {
pts, err := models.ParsePoints([]byte(`cpu,host=serverA,region=us-east int=10i,float=11.0,float2=12.1 1000000000`))
if err != nil {
t.Fatalf(`ParsePoints() failed. got %s`, err)
}
if exp := 1; len(pts) != exp {
t.Errorf("ParsePoint() len mismatch: got %v, exp %v", len(pts), exp)
}
pt := pts[0]
fields, err := pt.Fields()
if err != nil {
t.Fatal(err)
}
if _, ok := fields["int"].(int64); !ok {
t.Errorf("ParsePoint() int field mismatch: got %T, exp %T", fields["int"], int64(10))
}
if _, ok := fields["float"].(float64); !ok {
t.Errorf("ParsePoint() float field mismatch: got %T, exp %T", fields["float64"], float64(11.0))
}
if _, ok := fields["float2"].(float64); !ok {
t.Errorf("ParsePoint() float field mismatch: got %T, exp %T", fields["float64"], float64(12.1))
}
}
func TestParsePointKeyUnsorted(t *testing.T) {
pts, err := models.ParsePoints([]byte("cpu,last=1,first=2 value=1i"))
if err != nil {
t.Fatalf(`ParsePoints() failed. got %s`, err)
}
if exp := 1; len(pts) != exp {
t.Errorf("ParsePoint() len mismatch: got %v, exp %v", len(pts), exp)
}
pt := pts[0]
if exp := "cpu,first=2,last=1"; string(pt.Key()) != exp {
t.Errorf("ParsePoint key not sorted. got %v, exp %v", string(pt.Key()), exp)
}
}
func TestParsePointToString(t *testing.T) {
line := `cpu,host=serverA,region=us-east bool=false,float=11,float2=12.123,int=10i,str="string val" 1000000000`
pts, err := models.ParsePoints([]byte(line))
if err != nil {
t.Fatalf(`ParsePoints() failed. got %s`, err)
}
if exp := 1; len(pts) != exp {
t.Errorf("ParsePoint() len mismatch: got %v, exp %v", len(pts), exp)
}
pt := pts[0]
got := pt.String()
if line != got {
t.Errorf("ParsePoint() to string mismatch:\n got %v\n exp %v", got, line)
}
pt = models.MustNewPoint("cpu", models.NewTags(map[string]string{"host": "serverA", "region": "us-east"}),
models.Fields{"int": 10, "float": float64(11.0), "float2": float64(12.123), "bool": false, "str": "string val"},
time.Unix(1, 0))
got = pt.String()
if line != got {
t.Errorf("NewPoint() to string mismatch:\n got %v\n exp %v", got, line)
}
}
func TestParsePointsWithPrecision(t *testing.T) {
tests := []struct {
name string
line string
precision string
exp string
}{
{
name: "nanosecond by default",
line: `cpu,host=serverA,region=us-east value=1.0 946730096789012345`,
precision: "",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
},
{
name: "nanosecond",
line: `cpu,host=serverA,region=us-east value=1.0 946730096789012345`,
precision: "n",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
},
{
name: "microsecond",
line: `cpu,host=serverA,region=us-east value=1.0 946730096789012`,
precision: "u",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012000",
},
{
name: "millisecond",
line: `cpu,host=serverA,region=us-east value=1.0 946730096789`,
precision: "ms",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789000000",
},
{
name: "second",
line: `cpu,host=serverA,region=us-east value=1.0 946730096`,
precision: "s",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096000000000",
},
{
name: "minute",
line: `cpu,host=serverA,region=us-east value=1.0 15778834`,
precision: "m",
exp: "cpu,host=serverA,region=us-east value=1.0 946730040000000000",
},
{
name: "hour",
line: `cpu,host=serverA,region=us-east value=1.0 262980`,
precision: "h",
exp: "cpu,host=serverA,region=us-east value=1.0 946728000000000000",
},
}
for _, test := range tests {
pts, err := models.ParsePointsWithPrecision([]byte(test.line), time.Now().UTC(), test.precision)
if err != nil {
t.Fatalf(`%s: ParsePoints() failed. got %s`, test.name, err)
}
if exp := 1; len(pts) != exp {
t.Errorf("%s: ParsePoint() len mismatch: got %v, exp %v", test.name, len(pts), exp)
}
pt := pts[0]
got := pt.String()
if got != test.exp {
t.Errorf("%s: ParsePoint() to string mismatch:\n got %v\n exp %v", test.name, got, test.exp)
}
}
}
func TestParsePointsWithPrecisionNoTime(t *testing.T) {
line := `cpu,host=serverA,region=us-east value=1.0`
tm, _ := time.Parse(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z")
tests := []struct {
name string
precision string
exp string
}{
{
name: "no precision",
precision: "",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
},
{
name: "nanosecond precision",
precision: "n",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
},
{
name: "microsecond precision",
precision: "u",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012000",
},
{
name: "millisecond precision",
precision: "ms",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789000000",
},
{
name: "second precision",
precision: "s",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096000000000",
},
{
name: "minute precision",
precision: "m",
exp: "cpu,host=serverA,region=us-east value=1.0 946730040000000000",
},
{
name: "hour precision",
precision: "h",
exp: "cpu,host=serverA,region=us-east value=1.0 946728000000000000",
},
}
for _, test := range tests {
pts, err := models.ParsePointsWithPrecision([]byte(line), tm, test.precision)
if err != nil {
t.Fatalf(`%s: ParsePoints() failed. got %s`, test.name, err)
}
if exp := 1; len(pts) != exp {
t.Errorf("%s: ParsePoint() len mismatch: got %v, exp %v", test.name, len(pts), exp)
}
pt := pts[0]
got := pt.String()
if got != test.exp {
t.Errorf("%s: ParsePoint() to string mismatch:\n got %v\n exp %v", test.name, got, test.exp)
}
}
}
func TestParsePointsWithPrecisionComments(t *testing.T) {
tests := []struct {
name string
batch string
exp string
lenPoints int
}{
{
name: "comment only",
batch: `# comment only`,
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
lenPoints: 0,
},
{
name: "point with comment above",
batch: `# a point is below
cpu,host=serverA,region=us-east value=1.0 946730096789012345`,
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
lenPoints: 1,
},
{
name: "point with comment below",
batch: `cpu,host=serverA,region=us-east value=1.0 946730096789012345
# end of points`,
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
lenPoints: 1,
},
{
name: "indented comment",
batch: ` # a point is below
cpu,host=serverA,region=us-east value=1.0 946730096789012345`,
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
lenPoints: 1,
},
}
for _, test := range tests {
pts, err := models.ParsePointsWithPrecision([]byte(test.batch), time.Now().UTC(), "")
if err != nil {
t.Fatalf(`%s: ParsePoints() failed. got %s`, test.name, err)
}
pointsLength := len(pts)
if exp := test.lenPoints; pointsLength != exp {
t.Errorf("%s: ParsePoint() len mismatch: got %v, exp %v", test.name, pointsLength, exp)
}
if pointsLength > 0 {
pt := pts[0]
got := pt.String()
if got != test.exp {
t.Errorf("%s: ParsePoint() to string mismatch:\n got %v\n exp %v", test.name, got, test.exp)
}
}
}
}
func TestNewPointEscaped(t *testing.T) {
// commas
pt := models.MustNewPoint("cpu,main", models.NewTags(map[string]string{"tag,bar": "value"}), models.Fields{"name,bar": 1.0}, time.Unix(0, 0))
if exp := `cpu\,main,tag\,bar=value name\,bar=1 0`; pt.String() != exp {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
}
// spaces
pt = models.MustNewPoint("cpu main", models.NewTags(map[string]string{"tag bar": "value"}), models.Fields{"name bar": 1.0}, time.Unix(0, 0))
if exp := `cpu\ main,tag\ bar=value name\ bar=1 0`; pt.String() != exp {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
}
// equals
pt = models.MustNewPoint("cpu=main", models.NewTags(map[string]string{"tag=bar": "value=foo"}), models.Fields{"name=bar": 1.0}, time.Unix(0, 0))
if exp := `cpu=main,tag\=bar=value\=foo name\=bar=1 0`; pt.String() != exp {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
}
}
func TestNewPointWithoutField(t *testing.T) {
_, err := models.NewPoint("cpu", models.NewTags(map[string]string{"tag": "bar"}), models.Fields{}, time.Unix(0, 0))
if err == nil {
t.Fatalf(`NewPoint() expected error. got nil`)
}
}
func TestNewPointUnhandledType(t *testing.T) {
// nil value
pt := models.MustNewPoint("cpu", nil, models.Fields{"value": nil}, time.Unix(0, 0))
if exp := `cpu value= 0`; pt.String() != exp {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
}
// unsupported type gets stored as string
now := time.Unix(0, 0).UTC()
pt = models.MustNewPoint("cpu", nil, models.Fields{"value": now}, time.Unix(0, 0))
if exp := `cpu value="1970-01-01 00:00:00 +0000 UTC" 0`; pt.String() != exp {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
}
fields, err := pt.Fields()
if err != nil {
t.Fatal(err)
}
if exp := "1970-01-01 00:00:00 +0000 UTC"; fields["value"] != exp {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
}
}
func TestMakeKeyEscaped(t *testing.T) {
if exp, got := `cpu\ load`, models.MakeKey([]byte(`cpu\ load`), models.NewTags(map[string]string{})); string(got) != exp {
t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp)
}
if exp, got := `cpu\ load`, models.MakeKey([]byte(`cpu load`), models.NewTags(map[string]string{})); string(got) != exp {
t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp)
}
if exp, got := `cpu\,load`, models.MakeKey([]byte(`cpu\,load`), models.NewTags(map[string]string{})); string(got) != exp {
t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp)
}
if exp, got := `cpu\,load`, models.MakeKey([]byte(`cpu,load`), models.NewTags(map[string]string{})); string(got) != exp {
t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp)
}
}
func TestPrecisionString(t *testing.T) {
tags := map[string]interface{}{"value": float64(1)}
tm, _ := time.Parse(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z")
tests := []struct {
name string
precision string
exp string
}{
{
name: "no precision",
precision: "",
exp: "cpu value=1 946730096789012345",
},
{
name: "nanosecond precision",
precision: "ns",
exp: "cpu value=1 946730096789012345",
},
{
name: "microsecond precision",
precision: "u",
exp: "cpu value=1 946730096789012",
},
{
name: "millisecond precision",
precision: "ms",
exp: "cpu value=1 946730096789",
},
{
name: "second precision",
precision: "s",
exp: "cpu value=1 946730096",
},
{
name: "minute precision",
precision: "m",
exp: "cpu value=1 15778834",
},
{
name: "hour precision",
precision: "h",
exp: "cpu value=1 262980",
},
}
for _, test := range tests {
pt := models.MustNewPoint("cpu", nil, tags, tm)
act := pt.PrecisionString(test.precision)
if act != test.exp {
t.Errorf("%s: PrecisionString() mismatch:\n actual: %v\n exp: %v",
test.name, act, test.exp)
}
}
}
func TestRoundedString(t *testing.T) {
tags := map[string]interface{}{"value": float64(1)}
tm, _ := time.Parse(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z")
tests := []struct {
name string
precision time.Duration
exp string
}{
{
name: "no precision",
precision: time.Duration(0),
exp: "cpu value=1 946730096789012345",
},
{
name: "nanosecond precision",
precision: time.Nanosecond,
exp: "cpu value=1 946730096789012345",
},
{
name: "microsecond precision",
precision: time.Microsecond,
exp: "cpu value=1 946730096789012000",
},
{
name: "millisecond precision",
precision: time.Millisecond,
exp: "cpu value=1 946730096789000000",
},
{
name: "second precision",
precision: time.Second,
exp: "cpu value=1 946730097000000000",
},
{
name: "minute precision",
precision: time.Minute,
exp: "cpu value=1 946730100000000000",
},
{
name: "hour precision",
precision: time.Hour,
exp: "cpu value=1 946731600000000000",
},
}
for _, test := range tests {
pt := models.MustNewPoint("cpu", nil, tags, tm)
act := pt.RoundedString(test.precision)
if act != test.exp {
t.Errorf("%s: RoundedString() mismatch:\n actual: %v\n exp: %v",
test.name, act, test.exp)
}
}
}
func TestParsePointsStringWithExtraBuffer(t *testing.T) {
b := make([]byte, 70*5000)
buf := bytes.NewBuffer(b)
key := "cpu,host=A,region=uswest"
buf.WriteString(fmt.Sprintf("%s value=%.3f 1\n", key, rand.Float64()))
points, err := models.ParsePointsString(buf.String())
if err != nil {
t.Fatalf("failed to write points: %s", err.Error())
}
pointKey := string(points[0].Key())
if len(key) != len(pointKey) {
t.Fatalf("expected length of both keys are same but got %d and %d", len(key), len(pointKey))
}
if key != pointKey {
t.Fatalf("expected both keys are same but got %s and %s", key, pointKey)
}
}
func TestParsePointsQuotesInFieldKey(t *testing.T) {
buf := `cpu "a=1
cpu value=2 1`
points, err := models.ParsePointsString(buf)
if err != nil {
t.Fatalf("failed to write points: %s", err.Error())
}
fields, err := points[0].Fields()
if err != nil {
t.Fatal(err)
}
value, ok := fields["\"a"]
if !ok {
t.Fatalf("expected to parse field '\"a'")
}
if value != float64(1) {
t.Fatalf("expected field value to be 1, got %v", value)
}
// The following input should not parse
buf = `cpu "\, '= "\ v=1.0`
_, err = models.ParsePointsString(buf)
if err == nil {
t.Fatalf("expected parsing failure but got no error")
}
}
func TestParsePointsQuotesInTags(t *testing.T) {
buf := `t159,label=hey\ "ya a=1i,value=0i
t159,label=another a=2i,value=1i 1`
points, err := models.ParsePointsString(buf)
if err != nil {
t.Fatalf("failed to write points: %s", err.Error())
}
if len(points) != 2 {
t.Fatalf("expected 2 points, got %d", len(points))
}
}
func TestParsePointsBlankLine(t *testing.T) {
buf := `cpu value=1i 1000000000
cpu value=2i 2000000000`
points, err := models.ParsePointsString(buf)
if err != nil {
t.Fatalf("failed to write points: %s", err.Error())
}
if len(points) != 2 {
t.Fatalf("expected 2 points, got %d", len(points))
}
}
func TestNewPointsWithBytesWithCorruptData(t *testing.T) {
corrupted := []byte{0, 0, 0, 3, 102, 111, 111, 0, 0, 0, 4, 61, 34, 65, 34, 1, 0, 0, 0, 14, 206, 86, 119, 24, 32, 72, 233, 168, 2, 148}
p, err := models.NewPointFromBytes(corrupted)
if p != nil || err == nil {
t.Fatalf("NewPointFromBytes: got: (%v, %v), expected: (nil, error)", p, err)
}
}
func TestNewPointsWithShortBuffer(t *testing.T) {
_, err := models.NewPointFromBytes([]byte{0, 0, 0, 3, 4})
if err != io.ErrShortBuffer {
t.Fatalf("NewPointFromBytes: got: (%v, %v), expected: (nil, error)", p, err)
}
}
func TestNewPointsRejectsEmptyFieldNames(t *testing.T) {
if _, err := models.NewPoint("foo", nil, models.Fields{"": 1}, time.Now()); err == nil {
t.Fatalf("new point with empty field name. got: nil, expected: error")
}
}
func TestNewPointsRejectsMaxKey(t *testing.T) {
var key string
for i := 0; i < 65536; i++ {
key += "a"
}
if _, err := models.NewPoint(key, nil, models.Fields{"value": 1}, time.Now()); err == nil {
t.Fatalf("new point with max key. got: nil, expected: error")
}
if _, err := models.ParsePointsString(fmt.Sprintf("%v value=1", key)); err == nil {
t.Fatalf("parse point with max key. got: nil, expected: error")
}
}
func TestParseKeyEmpty(t *testing.T) {
if _, _, err := models.ParseKey(nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestParseKeyMissingValue(t *testing.T) {
if _, _, err := models.ParseKey([]byte("cpu,foo ")); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestPoint_FieldIterator_Simple(t *testing.T) {
p, err := models.ParsePoints([]byte(`m v=42i,f=42 36`))
if err != nil {
t.Fatal(err)
}
if len(p) != 1 {
t.Fatalf("wrong number of points, got %d, exp %d", len(p), 1)
}
fi := p[0].FieldIterator()
if !fi.Next() {
t.Fatal("field iterator terminated before first field")
}
if fi.Type() != models.Integer {
t.Fatalf("'42i' should be an Integer, got %v", fi.Type())
}
iv, err := fi.IntegerValue()
if err != nil {
t.Fatal(err)
}
if exp, got := int64(42), iv; exp != got {
t.Fatalf("'42i' should be %d, got %d", exp, got)
}
if !fi.Next() {
t.Fatalf("field iterator terminated before second field")
}
if fi.Type() != models.Float {
t.Fatalf("'42' should be a Float, got %v", fi.Type())
}
fv, err := fi.FloatValue()
if err != nil {
t.Fatal(err)
}
if exp, got := 42.0, fv; exp != got {
t.Fatalf("'42' should be %f, got %f", exp, got)
}
if fi.Next() {
t.Fatal("field iterator didn't terminate")
}
}
func toFields(fi models.FieldIterator) models.Fields {
m := make(models.Fields)
for fi.Next() {
var v interface{}
var err error
switch fi.Type() {
case models.Float:
v, err = fi.FloatValue()
case models.Integer:
v, err = fi.IntegerValue()
case models.String:
v = fi.StringValue()
case models.Boolean:
v, err = fi.BooleanValue()
case models.Empty:
v = nil
default:
panic("unknown type")
}
if err != nil {
panic(err)
}
m[string(fi.FieldKey())] = v
}
return m
}
func TestPoint_FieldIterator_FieldMap(t *testing.T) {
points, err := models.ParsePointsString(`
m v=42
m v=42i
m v="string"
m v=true
m v="string\"with\"escapes"
m v=42i,f=42,g=42.314
m a=2i,b=3i,c=true,d="stuff",e=-0.23,f=123.456
`)
if err != nil {
t.Fatal("failed to parse test points:", err)
}
for _, p := range points {
exp, err := p.Fields()
if err != nil {
t.Fatal(err)
}
got := toFields(p.FieldIterator())
if !reflect.DeepEqual(got, exp) {
t.Errorf("FieldIterator failed for %#q: got %#v, exp %#v", p.String(), got, exp)
}
}
}
func TestEscapeStringField(t *testing.T) {
cases := []struct {
in string
expOut string
}{
{in: "abcdefg", expOut: "abcdefg"},
{in: `one double quote " .`, expOut: `one double quote \" .`},
{in: `quote " then backslash \ .`, expOut: `quote \" then backslash \\ .`},
{in: `backslash \ then quote " .`, expOut: `backslash \\ then quote \" .`},
}
for _, c := range cases {
// Unescapes as expected.
got := models.EscapeStringField(c.in)
if got != c.expOut {
t.Errorf("unexpected result from EscapeStringField(%s)\ngot [%s]\nexp [%s]\n", c.in, got, c.expOut)
continue
}
pointLine := fmt.Sprintf(`t s="%s"`, got)
test(t, pointLine, NewTestPoint(
"t",
models.NewTags(nil),
models.Fields{"s": c.in},
time.Unix(0, 0),
))
}
}
func BenchmarkEscapeStringField_Plain(b *testing.B) {
s := "nothing special"
for i := 0; i < b.N; i++ {
sink = models.EscapeStringField(s)
}
}
func BenchmarkEscapeString_Quotes(b *testing.B) {
s := `Hello, "world"`
for i := 0; i < b.N; i++ {
sink = models.EscapeStringField(s)
}
}
func BenchmarkEscapeString_Backslashes(b *testing.B) {
s := `C:\windows\system32`
for i := 0; i < b.N; i++ {
sink = models.EscapeStringField(s)
}
}
func BenchmarkEscapeString_QuotesAndBackslashes(b *testing.B) {
s1 := `a quote " then backslash \ .`
s2 := `a backslash \ then quote " .`
for i := 0; i < b.N; i++ {
sink = [...]string{models.EscapeStringField(s1), models.EscapeStringField(s2)}
}
}