Skip to content

Commit

Permalink
add benchmarks for TimestampFrom and Timestamp.Time (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmcgarvey-modopayments authored and kohenkatz committed Feb 10, 2025
1 parent cbd1ae5 commit 619065c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
11 changes: 11 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ func TestDefaultHWAddrFunc(t *testing.T) {
}

func BenchmarkGenerator(b *testing.B) {
b.ReportAllocs()
b.Run("NewV1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV1()
Expand All @@ -1121,6 +1122,16 @@ func BenchmarkGenerator(b *testing.B) {
NewV5(NamespaceDNS, "www.example.com")
}
})
b.Run("NewV6", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV6()
}
})
b.Run("NewV7", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV7()
}
})
}

type faultyReader struct {
Expand Down
91 changes: 91 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,94 @@ func BenchmarkFormat(b *testing.B) {
})
}
}

var uuidBenchmarkSink UUID
var timestampBenchmarkSink Timestamp
var timeBenchmarkSink time.Time

func BenchmarkTimestampFrom(b *testing.B) {
b.ReportAllocs()

var err error
numbUUIDs := 1000
if testing.Short() {
numbUUIDs = 10
}

funcs := []struct {
name string
create func() (UUID, error)
timestamp func(UUID) (Timestamp, error)
}{
{"v1", NewV1, TimestampFromV1},
{"v6", NewV6, TimestampFromV6},
{"v7", NewV7, TimestampFromV7},
}

for _, fns := range funcs {
b.Run(fns.name, func(b *testing.B) {
// Make sure we don't just encode the same string over and over again as that will hit memory caches unrealistically
uuids := make([]UUID, numbUUIDs)
for i := 0; i < numbUUIDs; i++ {
uuids[i] = Must(fns.create())
if !testing.Short() {
time.Sleep(1 * time.Millisecond)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
timestampBenchmarkSink, err = fns.timestamp(uuids[i%numbUUIDs])

if err != nil {
b.Fatal(err)
}
}
})
}
}

func BenchmarkTimestampTime(b *testing.B) {
b.ReportAllocs()

var err error
numbUUIDs := 1000
if testing.Short() {
numbUUIDs = 10
}

funcs := []struct {
name string
create func() (UUID, error)
timestamp func(UUID) (Timestamp, error)
}{
{"v1", NewV1, TimestampFromV1},
{"v6", NewV6, TimestampFromV6},
{"v7", NewV7, TimestampFromV7},
}

for _, fns := range funcs {
b.Run(fns.name, func(b *testing.B) {
// Make sure we don't just encode the same string over and over again as that will hit memory caches unrealistically
uuids := make([]UUID, numbUUIDs)
timestamps := make([]Timestamp, numbUUIDs)
for i := 0; i < numbUUIDs; i++ {
uuids[i] = Must(fns.create())
timestamps[i], err = fns.timestamp(uuids[i])
if err != nil {
b.Fatal(err)
}
if !testing.Short() {
time.Sleep(1 * time.Millisecond)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
timeBenchmarkSink, err = timestamps[i%numbUUIDs].Time()
if err != nil {
b.Fatal(err)
}
}
})
}

}

0 comments on commit 619065c

Please sign in to comment.