add benchmarks for ids (#816)

... I was curious
This commit is contained in:
Reed Allman
2018-03-06 15:13:00 -08:00
committed by GitHub
parent 54d6a59909
commit 84df77a757
2 changed files with 31 additions and 1 deletions

View File

@@ -47,7 +47,7 @@ func SetMachineIdHost(addr net.IP, port uint16) {
func New() Id { func New() Id {
var id Id var id Id
t := time.Now() t := time.Now()
// TODO optimize out division by constant (check assembly for compiler optimization) // NOTE compiler optimizes out division by constant for us
ms := uint64(t.Unix())*1000 + uint64(t.Nanosecond()/int(time.Millisecond)) ms := uint64(t.Unix())*1000 + uint64(t.Nanosecond()/int(time.Millisecond))
count := atomic.AddUint64(&counter, 1) count := atomic.AddUint64(&counter, 1)

30
api/id/id_test.go Normal file
View File

@@ -0,0 +1,30 @@
package id
import (
"testing"
)
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
}
}