Files
fn-serverless/api/id/id_test.go
Reed Allman b9ff601098 fix machine id to 48 bits, add test (#877)
another bone head fail here. hopefully can leave this alone meow...

newID does not get inlined, but doesn't allocate to trigger any stack
expansion either. net perf hit on my laptop is 5ns, and we get a test out of
it. it will push a new stack, so it's not negligible overhead and we could
avoid it. we could keep the logic in both places just to have a test for it
separate instead of re-using the function in the hot path. up to us.
2018-03-21 16:01:18 -07:00

64 lines
1.1 KiB
Go

package id
import (
"encoding/binary"
"fmt"
"math"
"net"
"testing"
"time"
)
func BenchmarkGen(b *testing.B) {
for i := 0; i < b.N; i++ {
id := New()
_ = id
}
}
func BenchmarkMarshalText(b *testing.B) {
id := New()
for i := 0; i < b.N; i++ {
byts, _ := id.MarshalText()
_ = byts
}
}
func BenchmarkUnmarshalText(b *testing.B) {
id := New()
byts, _ := id.MarshalText()
for i := 0; i < b.N; i++ {
var id Id
id.UnmarshalText(byts)
_ = id
}
}
func TestIdRaw(t *testing.T) {
SetMachineIdHost(net.IP{127, 0, 0, 1}, 8080)
ts := time.Now()
ms := uint64(ts.Unix())*1000 + uint64(ts.Nanosecond()/int(time.Millisecond))
count := uint32(math.MaxUint32)
id := newID(ms, machineID, count)
fmt.Println(len(id), id)
var buf [8]byte
copy(buf[2:], id[:6])
idTime := binary.BigEndian.Uint64(buf[:])
if ms != idTime {
t.Fatal("id time doesn't not match time given", ms, idTime)
}
copy(buf[2:], id[6:12])
idMid := binary.BigEndian.Uint64(buf[:])
if idMid != machineID {
t.Fatal("machine id mismatch", idMid, machineID)
}
idCount := binary.BigEndian.Uint32(id[12:16])
if idCount != count {
t.Fatal("count mismatch", idCount, count)
}
}