Created
October 10, 2017 15:37
-
-
Save Dieterbe/e95a9ffedcf7da9e2cefd96bab0ab726 to your computer and use it in GitHub Desktop.
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
go test -run=. -bench=. | |
goos: linux | |
goarch: amd64 | |
BenchmarkUintsParse-8 10000000 162 ns/op | |
BenchmarkUintsPrint-8 5000000 337 ns/op | |
BenchmarkArrParse-8 10000000 151 ns/op | |
BenchmarkArrPrint-8 3000000 403 ns/op | |
PASS | |
ok _/home/dieter/tmp/string-to-byte-array 7.126s |
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
package main | |
import ( | |
"encoding/hex" | |
"errors" | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
} | |
var errInvalidIdString = errors.New("invalid ID string") | |
type MetricIDUints struct { | |
org int | |
part1 uint64 | |
part2 uint64 | |
} | |
func NewMetricIDUints(s string) (MetricIDUints, error) { | |
id := MetricIDUints{} | |
splits := strings.Split(s, ".") | |
if len(splits) != 2 || len(splits[1]) != 32 { | |
return id, errInvalidIdString | |
} | |
var err error | |
id.org, err = strconv.Atoi(splits[0]) | |
if err != nil { | |
return id, err | |
} | |
id.part1, err = strconv.ParseUint(splits[1][:16], 16, 64) | |
if err != nil { | |
return id, err | |
} | |
id.part2, err = strconv.ParseUint(splits[1][16:], 16, 64) | |
if err != nil { | |
return id, err | |
} | |
return id, err | |
} | |
func (id *MetricIDUints) String() string { | |
return fmt.Sprintf("%d.%016x%016x", id.org, id.part1, id.part2) | |
} | |
type MetricIDArr struct { | |
org int | |
key [16]byte | |
} | |
func NewMetricIDArr(s string) (MetricIDArr, error) { | |
id := MetricIDArr{} | |
splits := strings.Split(s, ".") | |
if len(splits) != 2 || len(splits[1]) != 32 { | |
return id, errInvalidIdString | |
} | |
var err error | |
id.org, err = strconv.Atoi(splits[0]) | |
if err != nil { | |
return id, err | |
} | |
dst := make([]byte, 16) | |
n, err := hex.Decode(dst, []byte(splits[1])) | |
if n != 16 { | |
return id, errInvalidIdString | |
} | |
copy(id.key[:], dst) | |
return id, nil | |
} | |
func (id *MetricIDArr) String() string { | |
return fmt.Sprintf("%d.%x", id.org, id.key) | |
} |
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
package main | |
import "testing" | |
func TestMetricIDStringConversionUints(t *testing.T) { | |
testIDs := []string{ | |
"1.abcdefabcdef12340123456789abcdef", | |
"12345.00000000000000000000000000000000", | |
"12345.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
} | |
for _, idStr := range testIDs { | |
id, err := NewMetricIDUints(idStr) | |
if err != nil { | |
t.Fatalf("Error parsing id %s: %s", idStr, err) | |
} | |
idStr2 := id.String() | |
if idStr != idStr2 { | |
t.Fatalf("Converted id string has changed: %s %s", idStr, idStr2) | |
} | |
} | |
} | |
func TestMetricIDStringConversionErrorsUints(t *testing.T) { | |
testIDs := []string{ | |
"abc.00000000000000000000000000000000", | |
"1.1111111111111111111111111111111", | |
"1.111111111111111111111111111111111", | |
"1.g1111111111111111111111111111111", | |
} | |
for _, idStr := range testIDs { | |
_, err := NewMetricIDUints(idStr) | |
if err == nil { | |
t.Fatalf("Expected error, but didn't get one with %s", idStr) | |
} | |
} | |
} | |
func TestMetricIDStringConversionArr(t *testing.T) { | |
testIDs := []string{ | |
"1.abcdefabcdef12340123456789abcdef", | |
"12345.00000000000000000000000000000000", | |
"12345.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
} | |
for _, idStr := range testIDs { | |
id, err := NewMetricIDArr(idStr) | |
if err != nil { | |
t.Fatalf("Error parsing id %s: %s", idStr, err) | |
} | |
idStr2 := id.String() | |
if idStr != idStr2 { | |
t.Fatalf("Converted id string has changed: %s %s", idStr, idStr2) | |
} | |
} | |
} | |
func TestMetricIDStringConversionErrorsArr(t *testing.T) { | |
testIDs := []string{ | |
"abc.00000000000000000000000000000000", | |
"1.1111111111111111111111111111111", | |
"1.111111111111111111111111111111111", | |
"1.g1111111111111111111111111111111", | |
} | |
for _, idStr := range testIDs { | |
_, err := NewMetricIDArr(idStr) | |
if err == nil { | |
t.Fatalf("Expected error, but didn't get one with %s", idStr) | |
} | |
} | |
} | |
func BenchmarkUintsParse(b *testing.B) { | |
str := "1.abcdefabcdef12340123456789abcdef" | |
for i := 0; i < b.N; i++ { | |
NewMetricIDUints(str) | |
} | |
} | |
func BenchmarkUintsPrint(b *testing.B) { | |
str := "1.abcdefabcdef12340123456789abcdef" | |
m, _ := NewMetricIDUints(str) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
m.String() | |
} | |
} | |
func BenchmarkArrParse(b *testing.B) { | |
str := "1.abcdefabcdef12340123456789abcdef" | |
for i := 0; i < b.N; i++ { | |
NewMetricIDArr(str) | |
} | |
} | |
func BenchmarkArrPrint(b *testing.B) { | |
str := "1.abcdefabcdef12340123456789abcdef" | |
m, _ := NewMetricIDArr(str) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
m.String() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment