Skip to content

Commit

Permalink
feat: add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
cmackenzie1 committed Jan 3, 2025
1 parent 51a7fe7 commit f3ab6a3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
33 changes: 33 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# go-uuid Benchmarks

This directory contains benchmarks for the go-uuid package, comparing it to `google/uuid` and `gofrs/uuid`.
This package does not claim to be the fastest, or the best, so these benchmarks are provided to give you an idea of how
it compares to other packages.

## Running Benchmarks

To run the benchmarks, execute the following command:

```bash
go test -benchmem -bench=.
```

## Results

The results of the benchmarks are as follows:

```bash
$ go test -bench=. -benchmem
goos: darwin
goarch: arm64
pkg: github.com/cmackenzie1/go-uuid/bench
cpu: Apple M2
BenchmarkNewV4-8 4511821 257.2 ns/op 16 B/op 1 allocs/op
BenchmarkGoogleV4-8 4826209 252.2 ns/op 16 B/op 1 allocs/op
BenchmarkGofrsV4-8 4460016 254.6 ns/op 16 B/op 1 allocs/op
BenchmarkNewV7-8 10082949 122.8 ns/op 16 B/op 1 allocs/op
BenchmarkGoogleV7-8 3549622 298.6 ns/op 16 B/op 1 allocs/op
BenchmarkGofrsV7-8 9184320 136.5 ns/op 16 B/op 1 allocs/op
PASS
ok github.com/cmackenzie1/go-uuid/bench 8.644s
```
29 changes: 24 additions & 5 deletions bench/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,52 @@ import (
"testing"

"github.com/cmackenzie1/go-uuid"
guid "github.com/google/uuid"
gofrs "github.com/gofrs/uuid/v5"
google "github.com/google/uuid"
)

// Version 4 UUID benchmarks

func BenchmarkNewV4(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := uuid.NewV4()
_ = a // prevent compiler optimization
}
}

func BenchmarkGoogleV4(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := google.NewRandom()
_ = a // prevent compiler optimization
}
}

func BenchmarkGofrsV4(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := gofrs.NewV4()
_ = a // prevent compiler optimization
}
}

// Version 7 UUID benchmarks

func BenchmarkNewV7(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := uuid.NewV7()
_ = a // prevent compiler optimization
}
}

func BenchmarkGoogleV4(b *testing.B) {
func BenchmarkGoogleV7(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := guid.NewRandom()
a, _ := google.NewV7()
_ = a // prevent compiler optimization
}
}

func BenchmarkGoogleV7(b *testing.B) {
func BenchmarkGofrsV7(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := guid.NewV7()
a, _ := gofrs.NewV7()
_ = a // prevent compiler optimization
}
}
1 change: 1 addition & 0 deletions bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/cmackenzie1/go-uuid v1.1.3
github.com/gofrs/uuid/v5 v5.3.0
github.com/google/uuid v1.6.0
)

Expand Down
2 changes: 2 additions & 0 deletions bench/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

0 comments on commit f3ab6a3

Please sign in to comment.