Linting, Optimizations, Cleanup, More Benchmarks.

This commit is contained in:
Bruce Marriner 2016-06-04 15:50:03 -05:00
parent 0fdfe21511
commit db0acce1d0
2 changed files with 38 additions and 22 deletions

View File

@ -3,24 +3,17 @@ package snowflake
import (
"encoding/base64"
"fmt"
"errors"
"strconv"
"strings"
"sync"
"time"
)
const (
timeBits = 41
nodeBits = 10
stepBits = 12
timeMask int64 = -1 ^ (-1 << timeBits)
nodeMask int64 = -1 ^ (-1 << nodeBits)
stepMask int64 = -1 ^ (-1 << stepBits)
nodeMax = -1 ^ (-1 << nodeBits)
nodeBits = 10
stepBits = 12
nodeMax = -1 ^ (-1 << nodeBits)
stepMask int64 = -1 ^ (-1 << stepBits)
timeShift uint8 = nodeBits + stepBits
nodeShift uint8 = stepBits
)
@ -47,7 +40,7 @@ type ID int64
func NewNode(node int64) (*Node, error) {
if node < 0 || node > nodeMax {
return nil, fmt.Errorf("Node number must be between 0 and 1023")
return nil, errors.New("Node number must be between 0 and 1024")
}
return &Node{
@ -92,7 +85,7 @@ func (f ID) Int64() int64 {
// String returns a string of the snowflake ID
func (f ID) String() string {
return fmt.Sprintf("%d", f)
return strconv.FormatInt(int64(f), 10)
}
// Base2 returns a string base2 of the snowflake ID
@ -130,7 +123,6 @@ func (f ID) Step() int64 {
return int64(f) & 0x0000000000000FFF
}
// MarshalJSON returns a json byte array string of the snowflake ID.
func (f ID) MarshalJSON() ([]byte, error) {
return []byte(`"` + f.String() + `"`), nil
}
@ -138,13 +130,11 @@ func (f ID) MarshalJSON() ([]byte, error) {
// UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.
func (f *ID) UnmarshalJSON(b []byte) error {
s := strings.Replace(string(b), `"`, ``, 2)
i, err := strconv.ParseInt(s, 10, 64)
i, err := strconv.ParseInt(string(b[1:len(b)-1]), 10, 64)
if err != nil {
return err
}
*f = ID(i)
return nil
}

View File

@ -1,16 +1,42 @@
package snowflake
import (
"testing"
)
import "testing"
func BenchmarkGenerate(b *testing.B) {
node, _ := NewNode(1)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = node.Generate()
}
}
func BenchmarkUnmarshal(b *testing.B) {
node, _ := NewNode(1)
id, _ := node.Generate()
var id2 ID
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
id2.UnmarshalJSON(id.Bytes())
}
}
func BenchmarkMarshal(b *testing.B) {
node, _ := NewNode(1)
id, _ := node.Generate()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = id.MarshalJSON()
}
}