forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in lee/ints-for-cpu-only (pull request timescale#70)
Change devops CPU-only use case to use ints instead of high-precision floats Approved-by: RobAtticus NA <rob.kiefer@gmail.com>
- Loading branch information
Showing
5 changed files
with
126 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package serialize | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// Utility function for appending various data types to a byte string | ||
func fastFormatAppend(v interface{}, buf []byte) []byte { | ||
switch v.(type) { | ||
case int: | ||
return strconv.AppendInt(buf, int64(v.(int)), 10) | ||
case int64: | ||
return strconv.AppendInt(buf, v.(int64), 10) | ||
case float64: | ||
// Why -1 ? | ||
// From Golang source on genericFtoa (called by AppendFloat): 'Negative precision means "only as much as needed to be exact."' | ||
// Using this instead of an exact number for precision ensures we preserve the precision passed in to the function, allowing us | ||
// to use different precision for different use cases. | ||
return strconv.AppendFloat(buf, v.(float64), 'f', -1, 64) | ||
case float32: | ||
return strconv.AppendFloat(buf, float64(v.(float32)), 'f', -1, 32) | ||
case bool: | ||
return strconv.AppendBool(buf, v.(bool)) | ||
case []byte: | ||
buf = append(buf, v.([]byte)...) | ||
return buf | ||
case string: | ||
buf = append(buf, v.(string)...) | ||
return buf | ||
default: | ||
panic(fmt.Sprintf("unknown field type for %#v", v)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package serialize | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFastFormatAppend(t *testing.T) { | ||
cases := []struct { | ||
desc string | ||
inputString []byte | ||
input interface{} | ||
output []byte | ||
shouldPanic bool | ||
}{ | ||
{ | ||
desc: "fastFormatAppend should properly append a float64 to a given byte string", | ||
inputString: []byte("values,"), | ||
input: float64(29.37), | ||
output: []byte("values,29.37"), | ||
shouldPanic: false, | ||
}, | ||
{ | ||
desc: "fastFormatAppend should properly append a float32 to a given byte string", | ||
inputString: []byte("values,"), | ||
input: float32(29.37), | ||
output: []byte("values,29.37"), | ||
shouldPanic: false, | ||
}, | ||
{ | ||
desc: "fastFormatAppend should properly append an int to a given byte string", | ||
inputString: []byte("values,"), | ||
input: int(29), | ||
output: []byte("values,29"), | ||
shouldPanic: false, | ||
}, | ||
{ | ||
desc: "fastFormatAppend should properly append a byte string to a given byte string", | ||
inputString: []byte("values,"), | ||
input: []byte("bytestring"), | ||
output: []byte("values,bytestring"), | ||
shouldPanic: false, | ||
}, | ||
{ | ||
desc: "fastFormatAppend should properly append a string to a given byte string", | ||
inputString: []byte("values,"), | ||
input: "string", | ||
output: []byte("values,string"), | ||
shouldPanic: false, | ||
}, | ||
{ | ||
desc: "fastFormatAppend should properly append a boolean to a given byte string", | ||
inputString: []byte("values,"), | ||
input: true, | ||
output: []byte("values,true"), | ||
shouldPanic: false, | ||
}, | ||
{ | ||
desc: "fastFormatAppend should panic when given an unsupported type", | ||
inputString: []byte("values,"), | ||
input: []int{}, | ||
output: []byte("values,true"), | ||
shouldPanic: true, | ||
}, | ||
} | ||
|
||
testPanic := func(input interface{}, inputString []byte, desc string) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Errorf("%s: did not panic when should", desc) | ||
} | ||
}() | ||
fastFormatAppend(input, inputString) | ||
} | ||
|
||
for _, c := range cases { | ||
if c.shouldPanic == true { | ||
testPanic(c.input, c.inputString, c.desc) | ||
} else { | ||
got := fastFormatAppend(c.input, c.inputString) | ||
if string(got) != string(c.output) { | ||
t.Errorf("%s \nOutput incorrect: Want: %s Got: %s", c.desc, c.output, got) | ||
} | ||
} | ||
} | ||
} |