2015-09-16 20:33:08 +00:00
package models_test
2015-05-22 21:00:51 +00:00
2015-05-28 21:47:52 +00:00
import (
"bytes"
2015-06-25 19:20:20 +00:00
"fmt"
2017-03-01 18:43:13 +00:00
"io"
2015-06-25 19:20:20 +00:00
"math"
2015-10-28 14:12:53 +00:00
"math/rand"
2015-05-28 21:47:52 +00:00
"reflect"
2015-06-25 19:20:20 +00:00
"strconv"
2015-05-29 17:16:59 +00:00
"strings"
2015-05-28 21:47:52 +00:00
"testing"
"time"
2015-07-20 19:59:46 +00:00
2016-02-10 17:26:18 +00:00
"github.com/influxdata/influxdb/models"
2015-05-28 21:47:52 +00:00
)
2015-05-22 21:00:51 +00:00
2015-06-25 19:20:20 +00:00
var (
2016-09-14 01:15:41 +00:00
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 ,
2016-09-14 15:55:31 +00:00
"float64-tiny" : float64 ( math . SmallestNonzeroFloat64 ) ,
"float64-large" : float64 ( math . MaxFloat64 ) ,
2016-09-14 01:15:41 +00:00
}
2015-06-25 19:20:20 +00:00
maxFloat64 = strconv . FormatFloat ( math . MaxFloat64 , 'f' , 1 , 64 )
minFloat64 = strconv . FormatFloat ( - math . MaxFloat64 , 'f' , 1 , 64 )
2016-12-18 07:37:25 +00:00
sink interface { }
2015-06-25 19:20:20 +00:00
)
2015-05-22 21:00:51 +00:00
func TestMarshal ( t * testing . T ) {
2015-07-20 19:59:46 +00:00
got := tags . HashKey ( )
2015-05-28 21:47:52 +00:00
if exp := ",apple=orange,foo=bar,host=serverA,region=uswest" ; string ( got ) != exp {
2015-05-22 21:00:51 +00:00
t . Log ( "got: " , string ( got ) )
t . Log ( "exp: " , exp )
t . Error ( "invalid match" )
}
}
2017-10-04 20:53:45 +00:00
func TestMarshalFields ( t * testing . T ) {
for _ , tt := range [ ] struct {
name string
value interface { }
exp string
} {
{
name : "Float" ,
value : float64 ( 2 ) ,
exp : ` value=2 ` ,
} ,
{
name : "Integer" ,
value : int64 ( 2 ) ,
exp : ` value=2i ` ,
} ,
{
name : "Unsigned" ,
value : uint64 ( 2 ) ,
exp : ` value=2u ` ,
} ,
{
name : "String" ,
value : "foobar" ,
exp : ` value="foobar" ` ,
} ,
{
name : "Boolean" ,
value : true ,
exp : ` value=true ` ,
} ,
} {
t . Run ( tt . name , func ( t * testing . T ) {
fields := map [ string ] interface { } { "value" : tt . value }
if have , want := models . Fields ( fields ) . MarshalBinary ( ) , [ ] byte ( tt . exp ) ; ! bytes . Equal ( have , want ) {
t . Fatalf ( "unexpected field output: %s != %s" , string ( have ) , string ( want ) )
}
} )
}
}
2017-03-25 20:33:36 +00:00
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" )
}
}
2015-05-22 21:00:51 +00:00
func BenchmarkMarshal ( b * testing . B ) {
for i := 0 ; i < b . N ; i ++ {
2015-07-20 19:59:46 +00:00
tags . HashKey ( )
2015-05-28 21:47:52 +00:00
}
}
2016-09-14 15:55:31 +00:00
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...
2017-02-08 18:44:48 +00:00
tagList := [ ] models . Tags { nil , { models . NewTag ( [ ] byte ( "foo" ) , [ ] byte ( "bar" ) ) } , tags }
2016-09-14 15:55:31 +00:00
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 )
}
}
}
}
Fix memory leak of retained HTTP write payloads
This leak seems to have been introduced in 8aa224b22dc007e,
present in 1.1.0 and 1.1.1.
When points were parsed from HTTP payloads, their tags and fields
referred to subslices of the request body; if any tag set introduced a
new series, then those tags then were stored in the in-memory series
index objects, preventing the HTTP body from being garbage collected. If
there were no new series in the payload, then the request body would be
garbage collected as usual.
Now, we clone the tags before we store them in the index. This is an
imperfect fix because the Point still holds references to the original
tags, and the Point's field iterator also refers to the payload buffer.
However, the current write code path does not retain references to the
Point or its fields; and this change will likely be obsoleted when TSI
is introduced.
This change likely fixes #7827, #7810, #7778, and perhaps others.
2017-01-13 00:16:54 +00:00
func TestTag_Clone ( t * testing . T ) {
2017-02-08 18:44:48 +00:00
tag := models . NewTag ( [ ] byte ( "key" ) , [ ] byte ( "value" ) )
Fix memory leak of retained HTTP write payloads
This leak seems to have been introduced in 8aa224b22dc007e,
present in 1.1.0 and 1.1.1.
When points were parsed from HTTP payloads, their tags and fields
referred to subslices of the request body; if any tag set introduced a
new series, then those tags then were stored in the in-memory series
index objects, preventing the HTTP body from being garbage collected. If
there were no new series in the payload, then the request body would be
garbage collected as usual.
Now, we clone the tags before we store them in the index. This is an
imperfect fix because the Point still holds references to the original
tags, and the Point's field iterator also refers to the payload buffer.
However, the current write code path does not retain references to the
Point or its fields; and this change will likely be obsoleted when TSI
is introduced.
This change likely fixes #7827, #7810, #7778, and perhaps others.
2017-01-13 00:16:54 +00:00
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 )
}
}
}
2016-09-14 01:15:41 +00:00
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 )
}
}
2017-04-06 18:46:09 +00:00
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 )
}
}
}
2016-09-20 22:27:19 +00:00
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 ) ) )
}
}
2015-05-28 21:47:52 +00:00
func BenchmarkParsePointNoTags ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-12-07 18:01:46 +00:00
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 ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsSorted2 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,host=serverA,region=us-west value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsSorted5 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,env=prod,host=serverA,region=us-west,target=servers,zone=1c value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsSorted10 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
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 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsUnSorted2 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,region=us-west,host=serverA value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
pt , _ := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
2015-05-29 17:16:59 +00:00
pt [ 0 ] . Key ( )
2015-05-28 21:47:52 +00:00
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsUnSorted5 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
pt , _ := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
2015-05-29 17:16:59 +00:00
pt [ 0 ] . Key ( )
2015-05-28 21:47:52 +00:00
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsUnSorted10 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
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 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
pt , _ := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
2015-05-29 17:16:59 +00:00
pt [ 0 ] . Key ( )
2015-05-28 21:47:52 +00:00
}
}
2016-05-20 20:24:36 +00:00
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 ++ {
2016-06-30 16:49:53 +00:00
models . ParseKey ( [ ] byte ( line ) )
2016-05-20 20:24:36 +00:00
}
}
2016-01-17 19:31:01 +00:00
// 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 ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( line ) , time . Unix ( 0 , 0 ) , "n" )
2015-05-28 21:47:52 +00:00
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 )
}
2016-06-30 16:49:53 +00:00
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 ) )
2015-05-28 21:47:52 +00:00
}
}
2016-01-17 19:31:01 +00:00
for name , value := range point . RawFields {
2017-01-03 17:54:17 +00:00
fields , err := pts [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
val := fields [ name ]
2015-07-06 16:18:07 +00:00
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 )
}
2017-01-03 17:54:17 +00:00
}
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 )
2015-05-28 21:47:52 +00:00
}
}
if ! pts [ 0 ] . Time ( ) . Equal ( point . Time ( ) ) {
t . Errorf ( ` ParsePoints("%s") time mismatch. got %v, exp %v ` , line , pts [ 0 ] . Time ( ) , point . Time ( ) )
}
2015-05-29 17:16:59 +00:00
if ! strings . HasPrefix ( pts [ 0 ] . String ( ) , line ) {
2015-05-28 21:47:52 +00:00
t . Errorf ( "ParsePoints string mismatch.\ngot: %v\nexp: %v" , pts [ 0 ] . String ( ) , line )
}
}
func TestParsePointNoValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( "" )
2015-05-29 17:16:59 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , "" , err )
}
if exp := 0 ; len ( pts ) != exp {
2015-08-05 22:41:59 +00:00
t . Errorf ( ` ParsePoints("%s") len mismatch. got %v, exp %v ` , "" , len ( pts ) , exp )
}
}
func TestParsePointWhitespaceValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( " " )
2015-08-05 22:41:59 +00:00
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 )
}
}
2015-05-28 21:47:52 +00:00
func TestParsePointNoFields ( t * testing . T ) {
2015-11-12 15:25:33 +00:00
expectedSuffix := "missing fields"
examples := [ ] string {
"cpu_load_short,host=server01,region=us-west" ,
"cpu" ,
2015-11-20 01:41:25 +00:00
"cpu,host==" ,
2015-11-12 15:25:33 +00:00
"=" ,
}
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 )
}
2015-06-26 18:34:35 +00:00
}
2015-05-28 21:47:52 +00:00
}
func TestParsePointNoTimestamp ( t * testing . T ) {
2016-01-17 19:31:01 +00:00
test ( t , "cpu value=1" , NewTestPoint ( "cpu" , nil , models . Fields { "value" : 1.0 } , time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
}
func TestParsePointMissingQuote ( t * testing . T ) {
2015-11-12 15:25:33 +00:00
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 )
}
2015-05-28 21:47:52 +00:00
}
}
2015-10-13 23:46:23 +00:00
func TestParsePointMissingTagKey ( t * testing . T ) {
2015-11-12 15:25:33 +00:00
expectedSuffix := "missing tag key"
examples := [ ] string {
` cpu, value=1 ` ,
` cpu, ` ,
2015-11-20 11:01:23 +00:00
` cpu,,, ` ,
2015-11-12 15:25:33 +00:00
` 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 )
}
2015-06-26 18:34:35 +00:00
}
2015-11-12 15:25:33 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,\ =us-east value=1i ` )
2015-06-26 18:34:35 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,\ =us-east value=1i ` , err )
2015-06-26 18:34:35 +00:00
}
}
2015-05-28 21:47:52 +00:00
func TestParsePointMissingTagValue ( t * testing . T ) {
2015-11-12 15:25:33 +00:00
expectedSuffix := "missing tag value"
examples := [ ] string {
2015-11-18 23:18:28 +00:00
` cpu,host ` ,
` cpu,host, ` ,
2016-01-15 22:07:34 +00:00
` cpu,host= ` ,
2015-11-12 15:25:33 +00:00
` 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 )
}
2015-10-13 15:43:13 +00:00
}
2015-05-28 21:47:52 +00:00
}
2015-11-18 23:18:28 +00:00
func TestParsePointInvalidTagFormat ( t * testing . T ) {
expectedSuffix := "invalid tag format"
examples := [ ] string {
` cpu,host=f=o, ` ,
2015-11-20 11:01:23 +00:00
` cpu,host=f\==o, ` ,
2015-11-18 23:18:28 +00:00
}
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 )
}
}
}
2015-06-26 18:02:46 +00:00
func TestParsePointMissingFieldName ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west = ` )
2015-06-26 18:02:46 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west = ` )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west =123i ` )
2015-06-26 18:02:46 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west =123i ` )
2015-06-26 18:02:46 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west a\ =123i ` )
2015-06-26 18:02:46 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west a\ =123i ` )
2015-06-26 18:02:46 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=123i,=456i ` )
2015-06-26 18:02:46 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=123i,=456i ` )
2015-06-26 18:02:46 +00:00
}
}
2015-06-11 17:25:25 +00:00
func TestParsePointMissingFieldValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value= ` )
2015-06-11 17:25:25 +00:00
if err == nil {
2015-06-22 16:23:37 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value= ` )
2015-06-11 17:25:25 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value= 1000000000i ` )
2015-06-11 17:25:25 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value= 1000000000i ` )
2015-06-11 17:25:25 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=,value2=1i ` )
2015-06-11 17:25:25 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=,value2=1i ` )
2015-06-11 17:25:25 +00:00
}
2015-06-22 16:23:37 +00:00
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=server01,region=us-west 1434055562000000000i ` )
2015-06-22 16:23:37 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=server01,region=us-west 1434055562000000000i ` )
2015-06-22 16:23:37 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=server01,region=us-west value=1i,b ` )
2015-06-26 18:02:46 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=server01,region=us-west value=1i,b ` )
2015-06-26 18:02:46 +00:00
}
2015-06-11 19:06:09 +00:00
}
func TestParsePointBadNumber ( t * testing . T ) {
2016-03-14 20:45:15 +00:00
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= " ,
2017-09-14 19:01:07 +00:00
"cpu v=-123u" ,
2016-03-14 20:45:15 +00:00
} {
_ , err := models . ParsePointsString ( tt )
if err == nil {
t . Errorf ( "Point %q should be invalid" , tt )
}
2015-07-31 01:55:08 +00:00
}
2015-06-11 19:06:09 +00:00
}
2015-06-25 19:20:20 +00:00
func TestParsePointMaxInt64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9223372036854775808i ` )
2015-07-31 01:51:18 +00:00
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 )
2015-06-25 19:20:20 +00:00
}
// max int
2015-09-16 20:33:08 +00:00
p , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9223372036854775807i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=9223372036854775807i ` , err )
}
2017-01-03 17:54:17 +00:00
fields , err := p [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp , got := int64 ( 9223372036854775807 ) , fields [ "value" ] . ( int64 ) ; exp != got {
2015-10-17 14:50:45 +00:00
t . Fatalf ( "ParsePoints Value mismatch. \nexp: %v\ngot: %v" , exp , got )
2015-06-25 19:20:20 +00:00
}
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=0009223372036854775807i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=0009223372036854775807i ` , err )
2015-06-25 19:20:20 +00:00
}
}
func TestParsePointMinInt64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-9223372036854775809i ` )
2015-06-25 19:20:20 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=-9223372036854775809i ` )
2015-06-25 19:20:20 +00:00
}
// min int
2017-09-14 19:01:07 +00:00
p , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-9223372036854775808i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-9223372036854775808i ` , err )
2015-06-25 19:20:20 +00:00
}
2017-09-14 19:01:07 +00:00
fields , err := p [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp , got := int64 ( - 9223372036854775808 ) , fields [ "value" ] . ( int64 ) ; exp != got {
t . Fatalf ( "ParsePoints Value mismatch. \nexp: %v\ngot: %v" , exp , got )
}
2015-06-25 19:20:20 +00:00
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-0009223372036854775808i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-0009223372036854775808i ` , err )
2015-06-25 19:20:20 +00:00
}
}
func TestParsePointMaxFloat64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "1" + string ( maxFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=... ` )
}
// max float
2017-09-14 19:01:07 +00:00
p , err := models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , string ( maxFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=9223372036854775807 ` , err )
}
2017-09-14 19:01:07 +00:00
fields , err := p [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp , got := math . MaxFloat64 , fields [ "value" ] . ( float64 ) ; exp != got {
t . Fatalf ( "ParsePoints Value mismatch. \nexp: %v\ngot: %v" , exp , got )
}
2015-06-25 19:20:20 +00:00
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "0000" + string ( maxFloat64 ) ) )
2015-06-25 19:20:20 +00:00
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
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "-1" + string ( minFloat64 ) [ 1 : ] ) )
2015-06-25 19:20:20 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=... ` )
}
// min float
2017-09-14 19:01:07 +00:00
p , err := models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , string ( minFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=... ` , err )
}
2017-09-14 19:01:07 +00:00
fields , err := p [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp , got := - math . MaxFloat64 , fields [ "value" ] . ( float64 ) ; exp != got {
t . Fatalf ( "ParsePoints Value mismatch. \nexp: %v\ngot: %v" , exp , got )
}
2015-06-25 19:20:20 +00:00
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "-0000000" + string ( minFloat64 ) [ 1 : ] ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=... ` , err )
}
}
2017-09-14 19:01:07 +00:00
func TestParsePointMaxUint64 ( t * testing . T ) {
// out of range
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=18446744073709551616u ` )
exp := ` unable to parse 'cpu,host=serverA,region=us-west value=18446744073709551616u': unable to parse unsigned 18446744073709551616: strconv.ParseUint: parsing "18446744073709551616": 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=18446744073709551615u ` )
if err != nil {
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=18446744073709551615u ` , err )
}
fields , err := p [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp , got := uint64 ( 18446744073709551615 ) , fields [ "value" ] . ( uint64 ) ; 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=00018446744073709551615u ` )
if err != nil {
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=00018446744073709551615u ` , err )
}
}
func TestParsePointMinUint64 ( t * testing . T ) {
// out of range
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=--1u ` )
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=-1u ` )
}
// min int
p , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=0u ` )
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=0u ` , err )
}
fields , err := p [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp , got := uint64 ( 0 ) , fields [ "value" ] . ( uint64 ) ; 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=0000u ` )
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=0000u ` , err )
}
}
2015-06-11 19:06:09 +00:00
func TestParsePointNumberNonNumeric ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=.1a ` )
2015-06-11 19:06:09 +00:00
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 ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=0.-1 ` )
2015-06-11 19:06:09 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=0.-1 ` )
}
}
2015-10-02 03:45:10 +00:00
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=- ` )
}
}
2015-06-11 19:06:09 +00:00
func TestParsePointFloatMultipleDecimals ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.1.1 ` )
2015-06-11 19:06:09 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=1.1.1 ` )
}
}
2015-06-11 17:25:25 +00:00
2015-06-11 19:06:09 +00:00
func TestParsePointInteger ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1i ` )
2015-06-11 19:06:09 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1i ` , err )
2015-06-11 19:06:09 +00:00
}
}
func TestParsePointNegativeInteger ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-1i ` )
2015-06-11 19:06:09 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-1i ` , err )
2015-06-11 19:06:09 +00:00
}
}
func TestParsePointNegativeFloat ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-1.0 ` )
2015-06-11 19:06:09 +00:00
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 ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=.1 ` )
2015-06-11 19:06:09 +00:00
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 ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.0e4 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1.0e4 ` , err )
}
2015-07-10 20:18:51 +00:00
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1e4 ` )
2015-07-10 20:18:51 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1.0e4 ` , err )
}
2017-01-03 17:54:17 +00:00
fields , err := pts [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if fields [ "value" ] != 1e4 {
2015-07-10 20:18:51 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1e4 ` , err )
}
2015-06-11 19:06:09 +00:00
}
2015-10-13 20:28:57 +00:00
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 )
}
2017-01-03 17:54:17 +00:00
fields , err := pts [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
if fields [ "value" ] != 1e4 {
2015-10-13 20:28:57 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1E4 ` , err )
}
}
2015-06-11 19:06:09 +00:00
func TestParsePointFloatScientificDecimal ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.0e-4 ` )
2015-06-11 19:06:09 +00:00
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 ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-1.0e-4 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-1.0e-4 ` , err )
}
2015-06-11 17:25:25 +00:00
}
2015-06-11 20:53:53 +00:00
func TestParsePointBooleanInvalid ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=a ` )
2015-06-11 20:53:53 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=a ` )
}
}
2015-08-10 19:46:53 +00:00
func TestParsePointScientificIntInvalid ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9ie10 ` )
2015-08-10 19:46:53 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=9ie10 ` )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9e10i ` )
2015-08-10 19:46:53 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=9e10i ` )
}
}
2016-05-19 15:58:18 +00:00
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 )
}
2017-05-09 05:20:29 +00:00
if got , exp := string ( pts [ 0 ] . Name ( ) ) , string ( expPoint . Name ( ) ) ; got != exp {
2016-05-27 09:31:20 +00:00
t . Fatalf ( "[Example %d] got %v measurement, expected %v" , i , got , exp )
2016-05-19 15:58:18 +00:00
}
2017-01-03 17:54:17 +00:00
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 {
2016-05-19 15:58:18 +00:00
t . Fatalf ( "[Example %d] got %d fields, expected %d" , i , got , exp )
}
2017-01-03 17:54:17 +00:00
if got , exp := fields [ "value" ] , eFields [ "value" ] ; got != exp {
2016-05-19 15:58:18 +00:00
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 )
}
}
}
2015-05-28 21:47:52 +00:00
func TestParsePointUnescape ( t * testing . T ) {
2016-01-17 19:31:01 +00:00
// commas in measurement name
2015-07-31 01:51:18 +00:00
test ( t , ` foo\,bar value=1i ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-29 21:15:50 +00:00
"foo,bar" , // comma in the name
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2016-01-17 19:31:01 +00:00
"value" : int64 ( 1 ) ,
2015-06-29 21:15:50 +00:00
} ,
time . Unix ( 0 , 0 ) ) )
2016-01-17 19:31:01 +00:00
// comma in measurement name with tags
test ( t , ` cpu\,main,regions=east value=1.0 ` ,
NewTestPoint (
2015-05-29 20:34:06 +00:00
"cpu,main" , // comma in the name
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2016-01-17 19:31:01 +00:00
"regions" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-11-20 11:01:23 +00:00
2015-05-29 20:34:06 +00:00
// spaces in measurement name
test ( t , ` cpu\ load,region=east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-05-29 20:34:06 +00:00
"cpu load" , // space in the name
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"region" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-11-20 11:01:23 +00:00
// equals in measurement name
test ( t , ` cpu\=load,region=east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-11-20 11:01:23 +00:00
` cpu\=load ` , // backslash is literal
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-11-20 11:01:23 +00:00
"region" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-11-20 11:01:23 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
// equals in measurement name
test ( t , ` cpu=load,region=east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-11-20 11:01:23 +00:00
` cpu=load ` , // literal equals is fine in measurement name
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-11-20 11:01:23 +00:00
"region" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-11-20 11:01:23 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-29 20:34:06 +00:00
// commas in tag names
test ( t , ` cpu,region\,zone=east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-10-13 23:46:23 +00:00
"region,zone" : "east" , // comma in the tag key
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-11-20 11:01:23 +00:00
// spaces in tag name
2015-05-29 20:34:06 +00:00
test ( t , ` cpu,region\ zone=east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-11-20 11:01:23 +00:00
"region zone" : "east" , // space in the tag name
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2016-01-17 19:31:01 +00:00
// backslash with escaped equals in tag name
test ( t , ` cpu,reg\\=ion=east value=1.0 ` ,
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2016-01-17 19:31:01 +00:00
` reg\=ion ` : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2016-01-17 19:31:01 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-11-12 15:25:33 +00:00
// space is tag name
test ( t , ` cpu,\ =east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-11-20 11:01:23 +00:00
" " : "east" , // tag name is single space
2016-06-30 16:49:53 +00:00
} ) ,
2015-11-12 15:25:33 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
// commas in tag values
test ( t , ` cpu,regions=east\,west value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-28 21:47:52 +00:00
"regions" : "east,west" , // comma in the tag value
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
} ,
2015-05-29 17:16:59 +00:00
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
2016-01-17 19:31:01 +00:00
// backslash literal followed by escaped space
test ( t , ` cpu,regions=\\ east value=1.0 ` ,
NewTestPoint (
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2016-01-17 19:31:01 +00:00
"regions" : ` \ east ` ,
2016-06-30 16:49:53 +00:00
} ) ,
2016-01-17 19:31:01 +00:00
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" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2016-01-17 19:31:01 +00:00
"regions" : ` eas\ t ` ,
2016-06-30 16:49:53 +00:00
} ) ,
2016-01-17 19:31:01 +00:00
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" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2016-01-17 19:31:01 +00:00
"regions" : ` east\ ` ,
2016-06-30 16:49:53 +00:00
} ) ,
2016-01-17 19:31:01 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-29 20:34:06 +00:00
// spaces in tag values
test ( t , ` cpu,regions=east\ west value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"regions" : "east west" , // comma in the tag value
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
} ,
2015-05-29 17:16:59 +00:00
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
2015-10-13 23:53:50 +00:00
// commas in field keys
2015-05-29 20:34:06 +00:00
test ( t , ` cpu,regions=east value\,ms=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"regions" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-10-13 23:53:50 +00:00
"value,ms" : 1.0 , // comma in the field keys
2015-05-29 20:34:06 +00:00
} ,
time . Unix ( 0 , 0 ) ) )
2015-10-13 23:53:50 +00:00
// spaces in field keys
2015-05-29 20:34:06 +00:00
test ( t , ` cpu,regions=east value\ ms=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"regions" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-10-13 23:53:50 +00:00
"value ms" : 1.0 , // comma in the field keys
2015-05-29 20:34:06 +00:00
} ,
time . Unix ( 0 , 0 ) ) )
2015-10-04 18:40:14 +00:00
// tag with no value
test ( t , ` cpu,regions=east value="1" ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-10-04 18:40:14 +00:00
"regions" : "east" ,
"foobar" : "" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-10-04 18:40:14 +00:00
models . Fields {
"value" : "1" ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-29 20:34:06 +00:00
// commas in field values
test ( t , ` cpu,regions=east value="1,0" ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"regions" : "east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : "1,0" , // comma in the field value
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
// random character escaped
test ( t , ` cpu,regions=eas\t value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-05-28 21:47:52 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-28 21:47:52 +00:00
"regions" : "eas\\t" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
} ,
2015-05-29 17:16:59 +00:00
time . Unix ( 0 , 0 ) ) )
2015-07-21 17:12:40 +00:00
2015-11-20 11:01:23 +00:00
// backslash literal followed by escaped characters
test ( t , ` cpu,regions=\\,\,\=east value=1.0 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-11-12 15:25:33 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-11-20 11:01:23 +00:00
"regions" : ` \,,=east ` ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-11-12 15:25:33 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-10-13 23:53:50 +00:00
// field keys using escape char.
2015-07-31 01:51:18 +00:00
test ( t , ` cpu \a=1i ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-07-21 17:12:40 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2016-01-17 19:31:01 +00:00
"\\a" : int64 ( 1 ) , // Left as parsed since it's not a known escape sequence.
2015-07-21 17:12:40 +00:00
} ,
time . Unix ( 0 , 0 ) ) )
2015-08-21 17:56:08 +00:00
// measurement, tag and tag value with equals
test ( t , ` cpu=load,equals\=foo=tag\=value value=1i ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-21 17:56:08 +00:00
"cpu=load" , // Not escaped
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-08-21 17:56:08 +00:00
"equals=foo" : "tag=value" , // Tag and value unescaped
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2016-01-17 19:31:01 +00:00
"value" : int64 ( 1 ) ,
2015-08-21 17:56:08 +00:00
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
}
func TestParsePointWithTags ( t * testing . T ) {
test ( t ,
"cpu,host=serverA,region=us-east value=1.0 1000000000" ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { "host" : "serverA" , "region" : "us-east" } ) ,
2015-09-16 20:33:08 +00:00
models . Fields { "value" : 1.0 } , time . Unix ( 1 , 0 ) ) )
2015-05-28 21:47:52 +00:00
}
2016-06-13 14:45:08 +00:00
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 )
}
2015-05-28 21:47:52 +00:00
}
}
func TestParsePointWithStringField ( t * testing . T ) {
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo",str2="bar" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
"str" : "foo" ,
"str2" : "bar" ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu,host=serverA,region=us-east str="foo \" bar" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint ( "cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"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 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-05-28 21:47:52 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
"str" : "foo bar" , // spaces in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-05-29 20:34:06 +00:00
}
2015-05-28 21:47:52 +00:00
2015-08-05 20:12:19 +00:00
func TestParsePointWithStringWithNewline ( t * testing . T ) {
test ( t , "cpu,host=serverA,region=us-east value=1.0,str=\"foo\nbar\" 1000000000" ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-05 20:12:19 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-08-05 20:12:19 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-05 20:12:19 +00:00
"value" : 1.0 ,
"str" : "foo\nbar" , // newline in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-06-09 21:44:43 +00:00
func TestParsePointWithStringWithCommas ( t * testing . T ) {
// escaped comma
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo\,bar" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-09 21:44:43 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-06-09 21:44:43 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-09 21:44:43 +00:00
"value" : 1.0 ,
2015-06-22 18:22:03 +00:00
"str" : ` foo\,bar ` , // commas in string value
2015-06-09 21:44:43 +00:00
} ,
time . Unix ( 1 , 0 ) ) ,
)
// non-escaped comma
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo,bar" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-09 21:44:43 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-06-09 21:44:43 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-09 21:44:43 +00:00
"value" : 1.0 ,
"str" : "foo,bar" , // commas in string value
} ,
time . Unix ( 1 , 0 ) ) ,
2016-12-01 22:24:11 +00:00
)
// 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 ) ) ,
2015-06-09 21:44:43 +00:00
)
2015-08-18 16:24:03 +00:00
}
2015-06-09 21:44:43 +00:00
2015-08-18 16:24:03 +00:00
func TestParsePointQuotedMeasurement ( t * testing . T ) {
// non-escaped comma
test ( t , ` "cpu",host=serverA,region=us-east value=1.0 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-18 16:24:03 +00:00
` "cpu" ` ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-08-18 16:24:03 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-18 16:24:03 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-06-09 21:44:43 +00:00
}
2015-08-18 16:44:31 +00:00
func TestParsePointQuotedTags ( t * testing . T ) {
test ( t , ` cpu,"host"="serverA",region=us-east value=1.0 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-18 16:44:31 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-08-18 16:44:31 +00:00
` "host" ` : ` "serverA" ` ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-18 16:44:31 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2017-11-09 00:00:25 +00:00
func TestParsePoint_TrailingSlash ( t * testing . T ) {
_ , err := models . ParsePointsString ( ` a v=1 0\ ` )
if err == nil {
t . Fatalf ( "ParsePoints failed: %v" , err )
} else if ! strings . Contains ( err . Error ( ) , "bad timestamp" ) {
t . Fatalf ( "ParsePoints unexpected error: %v" , err )
}
}
2015-09-01 16:56:31 +00:00
func TestParsePointsUnbalancedQuotedTags ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( "baz,mytag=\"a x=1 1441103862125\nbaz,mytag=a z=1 1441103862126" )
2015-09-01 16:56:31 +00:00
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
2016-06-30 16:49:53 +00:00
exp := models . MustNewPoint ( "baz" , models . NewTags ( map [ string ] string { "mytag" : ` "a ` } ) ,
2015-09-16 20:33:08 +00:00
models . Fields { "x" : float64 ( 1 ) } , time . Unix ( 0 , 1441103862125 ) )
2015-09-01 16:56:31 +00:00
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
2016-06-30 16:49:53 +00:00
exp = models . MustNewPoint ( "baz" , models . NewTags ( map [ string ] string { "mytag" : ` a ` } ) ,
2015-09-16 20:33:08 +00:00
models . Fields { "z" : float64 ( 1 ) } , time . Unix ( 0 , 1441103862126 ) )
2015-09-01 16:56:31 +00:00
if pts [ 1 ] . String ( ) != exp . String ( ) {
t . Errorf ( "Point mismatch:\ngot: %v\nexp: %v" , pts [ 1 ] . String ( ) , exp . String ( ) )
}
}
2015-06-22 18:22:03 +00:00
func TestParsePointEscapedStringsAndCommas ( t * testing . T ) {
// non-escaped comma and quotes
test ( t , ` cpu,host=serverA,region=us-east value=" { Hello\" { ,}\" World}" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-22 18:22:03 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-06-22 18:22:03 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-22 18:22:03 +00:00
"value" : ` { Hello" { ,}" World} ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
// escaped comma and quotes
test ( t , ` cpu,host=serverA,region=us-east value=" { Hello\" { \,}\" World}" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-22 18:22:03 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-06-22 18:22:03 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-22 18:22:03 +00:00
"value" : ` { Hello" { \,}" World} ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-05-29 20:34:06 +00:00
func TestParsePointWithStringWithEquals ( t * testing . T ) {
2015-06-26 18:02:46 +00:00
test ( t , ` cpu,host=serverA,region=us-east str="foo=bar",value=1.0 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-05-29 20:34:06 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
"str" : "foo=bar" , // spaces in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-05-28 21:47:52 +00:00
}
2015-08-19 15:27:17 +00:00
func TestParsePointWithStringWithBackslash ( t * testing . T ) {
test ( t , ` cpu value="test\\\"" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test\" ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value="test\\" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test\ ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value="test\\\"" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test\" ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value="test\"" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test" ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-05-28 21:47:52 +00:00
func TestParsePointWithBoolField ( t * testing . T ) {
2015-07-13 17:27:24 +00:00
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 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-05-28 21:47:52 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-11 20:53:53 +00:00
"t" : true ,
"T" : true ,
"true" : true ,
2015-07-13 17:27:24 +00:00
"True" : true ,
2015-06-11 20:53:53 +00:00
"TRUE" : true ,
"f" : false ,
"F" : false ,
"false" : false ,
2015-07-13 17:27:24 +00:00
"False" : false ,
2015-06-11 20:53:53 +00:00
"FALSE" : false ,
2015-05-28 21:47:52 +00:00
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-05-29 20:34:06 +00:00
func TestParsePointUnicodeString ( t * testing . T ) {
test ( t , ` cpu,host=serverA,region=us-east value="wè" 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-05-29 20:34:06 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string {
2015-05-29 20:34:06 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
2016-06-30 16:49:53 +00:00
} ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : "wè" ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-10-19 17:40:18 +00:00
func TestParsePointNegativeTimestamp ( t * testing . T ) {
test ( t , ` cpu value=1 -1 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-10-19 17:40:18 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-10-19 17:40:18 +00:00
models . Fields {
"value" : 1.0 ,
} ,
time . Unix ( 0 , - 1 ) ) ,
)
}
func TestParsePointMaxTimestamp ( t * testing . T ) {
2016-06-16 17:15:41 +00:00
test ( t , fmt . Sprintf ( ` cpu value=1 %d ` , models . MaxNanoTime ) ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-10-19 17:40:18 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-10-19 17:40:18 +00:00
models . Fields {
"value" : 1.0 ,
} ,
2016-05-19 15:37:15 +00:00
time . Unix ( 0 , models . MaxNanoTime ) ) ,
2015-10-19 17:40:18 +00:00
)
}
func TestParsePointMinTimestamp ( t * testing . T ) {
2016-08-25 17:52:39 +00:00
test ( t , ` cpu value=1 -9223372036854775806 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-10-19 17:40:18 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-10-19 17:40:18 +00:00
models . Fields {
"value" : 1.0 ,
} ,
2016-05-19 15:37:15 +00:00
time . Unix ( 0 , models . MinNanoTime ) ) ,
2015-10-19 17:40:18 +00:00
)
}
func TestParsePointInvalidTimestamp ( t * testing . T ) {
2016-05-19 15:58:18 +00:00
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" ,
2015-10-19 17:40:18 +00:00
}
2016-05-19 15:58:18 +00:00
for i , example := range examples {
_ , err := models . ParsePointsString ( example )
if err == nil {
t . Fatalf ( "[Example %d] ParsePoints failed: %v" , i , err )
}
2015-10-19 20:06:13 +00:00
}
2015-10-19 17:40:18 +00:00
}
2015-07-31 01:51:18 +00:00
func TestNewPointFloatWithoutDecimal ( t * testing . T ) {
test ( t , ` cpu value=1 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-07-31 01:51:18 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-07-31 01:51:18 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-06-12 06:06:54 +00:00
func TestNewPointNegativeFloat ( t * testing . T ) {
test ( t , ` cpu value=-0.64 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-12 06:06:54 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-12 06:06:54 +00:00
"value" : - 0.64 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
func TestNewPointFloatNoDecimal ( t * testing . T ) {
test ( t , ` cpu value=1. 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-12 06:06:54 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-12 06:06:54 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-06-12 17:08:11 +00:00
func TestNewPointFloatScientific ( t * testing . T ) {
test ( t , ` cpu value=6.632243e+06 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-12 17:08:11 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-12 17:08:11 +00:00
"value" : float64 ( 6632243 ) ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
func TestNewPointLargeInteger ( t * testing . T ) {
2015-07-31 01:51:18 +00:00
test ( t , ` cpu value=6632243i 1000000000 ` ,
2016-01-17 19:31:01 +00:00
NewTestPoint (
2015-06-12 17:08:11 +00:00
"cpu" ,
2016-06-30 16:49:53 +00:00
models . NewTags ( map [ string ] string { } ) ,
2015-09-16 20:33:08 +00:00
models . Fields {
2016-01-17 19:31:01 +00:00
"value" : int64 ( 6632243 ) , // if incorrectly encoded as a float, it would show up as 6.632243e+06
2015-06-12 17:08:11 +00:00
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-10-26 22:05:27 +00:00
func TestParsePointNaN ( t * testing . T ) {
_ , err := models . ParsePointsString ( "cpu value=NaN 1000000000" )
if err == nil {
t . Fatalf ( "ParsePoints expected error, got nil" )
}
2015-07-06 16:18:07 +00:00
2015-10-26 22:05:27 +00:00
_ , err = models . ParsePointsString ( "cpu value=nAn 1000000000" )
if err == nil {
t . Fatalf ( "ParsePoints expected error, got nil" )
}
2015-08-21 18:12:48 +00:00
2015-10-26 22:05:27 +00:00
_ , err = models . ParsePointsString ( "cpu value=NaN" )
if err == nil {
t . Fatalf ( "ParsePoints expected error, got nil" )
}
2015-08-05 18:24:42 +00:00
}
func TestNewPointLargeNumberOfTags ( t * testing . T ) {
tags := ""
for i := 0 ; i < 255 ; i ++ {
tags += fmt . Sprintf ( ",tag%d=value%d" , i , i )
}
2015-07-06 16:18:07 +00:00
2015-09-16 20:33:08 +00:00
pt , err := models . ParsePointsString ( fmt . Sprintf ( "cpu%s value=1" , tags ) )
2015-08-05 18:24:42 +00:00
if err != nil {
t . Fatalf ( "ParsePoints() with max tags failed: %v" , err )
}
if len ( pt [ 0 ] . Tags ( ) ) != 255 {
2015-11-18 23:18:28 +00:00
t . Fatalf ( "expected %d tags, got %d" , 255 , len ( pt [ 0 ] . Tags ( ) ) )
2015-08-05 18:24:42 +00:00
}
2015-07-06 16:18:07 +00:00
}
2015-05-28 21:47:52 +00:00
func TestParsePointIntsFloats ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePoints ( [ ] byte ( ` cpu,host=serverA,region=us-east int=10i,float=11.0,float2=12.1 1000000000 ` ) )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints() failed. got %s ` , err )
}
2015-05-29 17:16:59 +00:00
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "ParsePoint() len mismatch: got %v, exp %v" , len ( pts ) , exp )
}
pt := pts [ 0 ]
2017-01-03 17:54:17 +00:00
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 ) )
2015-05-28 21:47:52 +00:00
}
2017-01-03 17:54:17 +00:00
if _ , ok := fields [ "float" ] . ( float64 ) ; ! ok {
t . Errorf ( "ParsePoint() float field mismatch: got %T, exp %T" , fields [ "float64" ] , float64 ( 11.0 ) )
2015-05-28 21:47:52 +00:00
}
2017-01-03 17:54:17 +00:00
if _ , ok := fields [ "float2" ] . ( float64 ) ; ! ok {
t . Errorf ( "ParsePoint() float field mismatch: got %T, exp %T" , fields [ "float64" ] , float64 ( 12.1 ) )
2015-05-28 21:47:52 +00:00
}
}
func TestParsePointKeyUnsorted ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePoints ( [ ] byte ( "cpu,last=1,first=2 value=1i" ) )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints() failed. got %s ` , err )
}
2015-05-29 17:16:59 +00:00
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "ParsePoint() len mismatch: got %v, exp %v" , len ( pts ) , exp )
}
pt := pts [ 0 ]
2015-05-28 21:47:52 +00:00
if exp := "cpu,first=2,last=1" ; string ( pt . Key ( ) ) != exp {
2015-11-18 23:18:28 +00:00
t . Errorf ( "ParsePoint key not sorted. got %v, exp %v" , string ( pt . Key ( ) ) , exp )
2015-05-28 21:47:52 +00:00
}
}
func TestParsePointToString ( t * testing . T ) {
2015-08-25 13:49:49 +00:00
line := ` cpu,host=serverA,region=us-east bool=false,float=11,float2=12.123,int=10i,str="string val" 1000000000 `
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints() failed. got %s ` , err )
}
2015-05-29 17:16:59 +00:00
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "ParsePoint() len mismatch: got %v, exp %v" , len ( pts ) , exp )
}
pt := pts [ 0 ]
2015-05-28 21:47:52 +00:00
got := pt . String ( )
if line != got {
t . Errorf ( "ParsePoint() to string mismatch:\n got %v\n exp %v" , got , line )
}
2016-06-30 16:49:53 +00:00
pt = models . MustNewPoint ( "cpu" , models . NewTags ( map [ string ] string { "host" : "serverA" , "region" : "us-east" } ) ,
2015-09-16 20:33:08 +00:00
models . Fields { "int" : 10 , "float" : float64 ( 11.0 ) , "float2" : float64 ( 12.123 ) , "bool" : false , "str" : "string val" } ,
2015-05-28 21:47:52 +00:00
time . Unix ( 1 , 0 ) )
got = pt . String ( )
if line != got {
t . Errorf ( "NewPoint() to string mismatch:\n got %v\n exp %v" , got , line )
2015-05-22 21:00:51 +00:00
}
}
2015-05-29 17:16:59 +00:00
func TestParsePointsWithPrecision ( t * testing . T ) {
2015-06-04 17:22:53 +00:00
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 {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( test . line ) , time . Now ( ) . UTC ( ) , test . precision )
2015-06-04 17:22:53 +00:00
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 )
}
2015-05-29 17:16:59 +00:00
}
2015-06-04 17:22:53 +00:00
}
2015-05-29 17:16:59 +00:00
2015-06-04 17:22:53 +00:00
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 {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( line ) , tm , test . precision )
2015-06-04 17:22:53 +00:00
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 )
}
2015-05-29 17:16:59 +00:00
}
}
2015-05-29 20:34:06 +00:00
2015-06-18 18:59:20 +00:00
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 {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( test . batch ) , time . Now ( ) . UTC ( ) , "" )
2015-06-18 18:59:20 +00:00
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 )
}
}
}
}
2015-05-29 20:34:06 +00:00
func TestNewPointEscaped ( t * testing . T ) {
// commas
2016-06-30 16:49:53 +00:00
pt := models . MustNewPoint ( "cpu,main" , models . NewTags ( map [ string ] string { "tag,bar" : "value" } ) , models . Fields { "name,bar" : 1.0 } , time . Unix ( 0 , 0 ) )
2015-08-25 13:49:49 +00:00
if exp := ` cpu\,main,tag\,bar=value name\,bar=1 0 ` ; pt . String ( ) != exp {
2015-05-29 20:34:06 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
// spaces
2016-06-30 16:49:53 +00:00
pt = models . MustNewPoint ( "cpu main" , models . NewTags ( map [ string ] string { "tag bar" : "value" } ) , models . Fields { "name bar" : 1.0 } , time . Unix ( 0 , 0 ) )
2015-08-25 13:49:49 +00:00
if exp := ` cpu\ main,tag\ bar=value name\ bar=1 0 ` ; pt . String ( ) != exp {
2015-05-29 20:34:06 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
// equals
2016-06-30 16:49:53 +00:00
pt = models . MustNewPoint ( "cpu=main" , models . NewTags ( map [ string ] string { "tag=bar" : "value=foo" } ) , models . Fields { "name=bar" : 1.0 } , time . Unix ( 0 , 0 ) )
2015-08-25 13:49:49 +00:00
if exp := ` cpu=main,tag\=bar=value\=foo name\=bar=1 0 ` ; pt . String ( ) != exp {
2015-05-29 20:34:06 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
}
2015-07-06 22:03:03 +00:00
2015-11-17 05:21:52 +00:00
func TestNewPointWithoutField ( t * testing . T ) {
2016-06-30 16:49:53 +00:00
_ , err := models . NewPoint ( "cpu" , models . NewTags ( map [ string ] string { "tag" : "bar" } ) , models . Fields { } , time . Unix ( 0 , 0 ) )
2015-11-17 05:21:52 +00:00
if err == nil {
t . Fatalf ( ` NewPoint() expected error. got nil ` )
}
}
2015-07-06 22:03:03 +00:00
func TestNewPointUnhandledType ( t * testing . T ) {
// nil value
2015-10-27 16:21:54 +00:00
pt := models . MustNewPoint ( "cpu" , nil , models . Fields { "value" : nil } , time . Unix ( 0 , 0 ) )
2015-07-06 22:03:03 +00:00
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
2015-07-07 03:11:09 +00:00
now := time . Unix ( 0 , 0 ) . UTC ( )
2015-10-27 16:21:54 +00:00
pt = models . MustNewPoint ( "cpu" , nil , models . Fields { "value" : now } , time . Unix ( 0 , 0 ) )
2015-07-07 03:11:09 +00:00
if exp := ` cpu value="1970-01-01 00:00:00 +0000 UTC" 0 ` ; pt . String ( ) != exp {
2015-07-06 22:03:03 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
2017-01-03 17:54:17 +00:00
fields , err := pt . Fields ( )
if err != nil {
t . Fatal ( err )
}
if exp := "1970-01-01 00:00:00 +0000 UTC" ; fields [ "value" ] != exp {
2015-07-06 22:03:03 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
}
2015-08-18 16:02:48 +00:00
func TestMakeKeyEscaped ( t * testing . T ) {
2016-06-30 16:49:53 +00:00
if exp , got := ` cpu\ load ` , models . MakeKey ( [ ] byte ( ` cpu\ load ` ) , models . NewTags ( map [ string ] string { } ) ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
2016-06-30 16:49:53 +00:00
if exp , got := ` cpu\ load ` , models . MakeKey ( [ ] byte ( ` cpu load ` ) , models . NewTags ( map [ string ] string { } ) ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
2016-06-30 16:49:53 +00:00
if exp , got := ` cpu\,load ` , models . MakeKey ( [ ] byte ( ` cpu\,load ` ) , models . NewTags ( map [ string ] string { } ) ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
2016-06-30 16:49:53 +00:00
if exp , got := ` cpu\,load ` , models . MakeKey ( [ ] byte ( ` cpu,load ` ) , models . NewTags ( map [ string ] string { } ) ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
}
2015-09-15 17:59:52 +00:00
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 {
2015-10-27 16:21:54 +00:00
pt := models . MustNewPoint ( "cpu" , nil , tags , tm )
2015-09-15 17:59:52 +00:00
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 )
}
}
}
2015-10-28 14:12:53 +00:00
2015-11-03 20:20:52 +00:00
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 )
}
}
}
2015-10-28 14:12:53 +00:00
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 )
}
}
2015-12-17 20:38:27 +00:00
func TestParsePointsQuotesInFieldKey ( t * testing . T ) {
2015-12-29 19:35:00 +00:00
buf := ` cpu " a = 1
cpu value = 2 1 `
2015-12-17 20:38:27 +00:00
points , err := models . ParsePointsString ( buf )
if err != nil {
t . Fatalf ( "failed to write points: %s" , err . Error ( ) )
}
2017-01-03 17:54:17 +00:00
fields , err := points [ 0 ] . Fields ( )
if err != nil {
t . Fatal ( err )
}
value , ok := fields [ "\"a" ]
2015-12-17 20:38:27 +00:00
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" )
}
}
2015-12-29 19:35:00 +00:00
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 ) )
}
}
2016-02-17 15:47:29 +00:00
2016-08-30 14:25:53 +00:00
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 ) )
}
}
2016-02-17 15:47:29 +00:00
func TestNewPointsWithBytesWithCorruptData ( t * testing . T ) {
2016-02-20 11:22:26 +00:00
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 )
2016-02-17 15:47:29 +00:00
}
}
2017-03-01 18:43:13 +00:00
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 )
}
}
2016-02-17 15:47:29 +00:00
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" )
}
}
2016-03-31 05:57:41 +00:00
func TestNewPointsRejectsMaxKey ( t * testing . T ) {
var key string
2017-05-24 20:35:34 +00:00
// tsm field key is point key, separator (4 bytes) and field
for i := 0 ; i < models . MaxKeyLength - len ( "value" ) - 4 ; i ++ {
2016-03-31 05:57:41 +00:00
key += "a"
}
2017-05-24 20:35:34 +00:00
// Test max key len
if _ , err := models . NewPoint ( key , nil , models . Fields { "value" : 1 , "ok" : 2.0 } , time . Now ( ) ) ; err != nil {
t . Fatalf ( "new point with max key. got: %v, expected: nil" , err )
}
if _ , err := models . ParsePointsString ( fmt . Sprintf ( "%v value=1,ok=2.0" , key ) ) ; err != nil {
t . Fatalf ( "parse point with max key. got: %v, expected: nil" , err )
}
// Test 1 byte over max key len
key += "a"
if _ , err := models . NewPoint ( key , nil , models . Fields { "value" : 1 , "ok" : 2.0 } , time . Now ( ) ) ; err == nil {
2016-03-31 05:57:41 +00:00
t . Fatalf ( "new point with max key. got: nil, expected: error" )
}
2017-05-24 20:35:34 +00:00
if _ , err := models . ParsePointsString ( fmt . Sprintf ( "%v value=1,ok=2.0" , key ) ) ; err == nil {
2016-03-31 05:57:41 +00:00
t . Fatalf ( "parse point with max key. got: nil, expected: error" )
}
2017-05-24 20:35:34 +00:00
2016-03-31 05:57:41 +00:00
}
2016-07-28 00:01:29 +00:00
2016-09-21 17:12:09 +00:00
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 ( ) )
}
2017-01-03 17:54:17 +00:00
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 )
2016-09-21 17:12:09 +00:00
}
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 ( ) )
}
2017-01-03 17:54:17 +00:00
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 )
2016-09-21 17:12:09 +00:00
}
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 { }
2017-01-03 17:54:17 +00:00
var err error
2016-09-21 17:12:09 +00:00
switch fi . Type ( ) {
case models . Float :
2017-01-03 17:54:17 +00:00
v , err = fi . FloatValue ( )
2016-09-21 17:12:09 +00:00
case models . Integer :
2017-01-03 17:54:17 +00:00
v , err = fi . IntegerValue ( )
2017-09-14 19:01:07 +00:00
case models . Unsigned :
v , err = fi . UnsignedValue ( )
2016-09-21 17:12:09 +00:00
case models . String :
v = fi . StringValue ( )
case models . Boolean :
2017-01-03 17:54:17 +00:00
v , err = fi . BooleanValue ( )
2016-09-21 17:12:09 +00:00
case models . Empty :
v = nil
default :
panic ( "unknown type" )
}
2017-01-03 17:54:17 +00:00
if err != nil {
panic ( err )
}
2016-09-21 17:12:09 +00:00
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"
2017-09-14 19:01:07 +00:00
m v = 42i , f = 42 , g = 42.314 , u = 123 u
2016-09-21 17:12:09 +00:00
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 {
2017-01-03 17:54:17 +00:00
exp , err := p . Fields ( )
if err != nil {
t . Fatal ( err )
}
2016-09-21 17:12:09 +00:00
got := toFields ( p . FieldIterator ( ) )
if ! reflect . DeepEqual ( got , exp ) {
t . Errorf ( "FieldIterator failed for %#q: got %#v, exp %#v" , p . String ( ) , got , exp )
}
}
}
2016-12-18 07:37:25 +00:00
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 ) }
}
}
2017-09-14 19:01:07 +00:00
2017-11-27 21:13:02 +00:00
func BenchmarkParseTags ( b * testing . B ) {
tags := [ ] byte ( "cpu,tag0=value0,tag1=value1,tag2=value2,tag3=value3,tag4=value4,tag5=value5" )
for i := 0 ; i < b . N ; i ++ {
models . ParseTags ( tags )
}
}
2017-09-14 19:01:07 +00:00
func init ( ) {
// Force uint support to be enabled for testing.
models . EnableUintSupport ( )
}