Skip to content

Commit f3ab6a3

Browse files
committed
feat: add benchmarks
1 parent 51a7fe7 commit f3ab6a3

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

bench/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# go-uuid Benchmarks
2+
3+
This directory contains benchmarks for the go-uuid package, comparing it to `google/uuid` and `gofrs/uuid`.
4+
This package does not claim to be the fastest, or the best, so these benchmarks are provided to give you an idea of how
5+
it compares to other packages.
6+
7+
## Running Benchmarks
8+
9+
To run the benchmarks, execute the following command:
10+
11+
```bash
12+
go test -benchmem -bench=.
13+
```
14+
15+
## Results
16+
17+
The results of the benchmarks are as follows:
18+
19+
```bash
20+
$ go test -bench=. -benchmem
21+
goos: darwin
22+
goarch: arm64
23+
pkg: github.com/cmackenzie1/go-uuid/bench
24+
cpu: Apple M2
25+
BenchmarkNewV4-8 4511821 257.2 ns/op 16 B/op 1 allocs/op
26+
BenchmarkGoogleV4-8 4826209 252.2 ns/op 16 B/op 1 allocs/op
27+
BenchmarkGofrsV4-8 4460016 254.6 ns/op 16 B/op 1 allocs/op
28+
BenchmarkNewV7-8 10082949 122.8 ns/op 16 B/op 1 allocs/op
29+
BenchmarkGoogleV7-8 3549622 298.6 ns/op 16 B/op 1 allocs/op
30+
BenchmarkGofrsV7-8 9184320 136.5 ns/op 16 B/op 1 allocs/op
31+
PASS
32+
ok github.com/cmackenzie1/go-uuid/bench 8.644s
33+
```

bench/benchmark_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,52 @@ import (
44
"testing"
55

66
"github.com/cmackenzie1/go-uuid"
7-
guid "github.com/google/uuid"
7+
gofrs "github.com/gofrs/uuid/v5"
8+
google "github.com/google/uuid"
89
)
910

11+
// Version 4 UUID benchmarks
12+
1013
func BenchmarkNewV4(b *testing.B) {
1114
for i := 0; i < b.N; i++ {
1215
a, _ := uuid.NewV4()
1316
_ = a // prevent compiler optimization
1417
}
1518
}
1619

20+
func BenchmarkGoogleV4(b *testing.B) {
21+
for i := 0; i < b.N; i++ {
22+
a, _ := google.NewRandom()
23+
_ = a // prevent compiler optimization
24+
}
25+
}
26+
27+
func BenchmarkGofrsV4(b *testing.B) {
28+
for i := 0; i < b.N; i++ {
29+
a, _ := gofrs.NewV4()
30+
_ = a // prevent compiler optimization
31+
}
32+
}
33+
34+
// Version 7 UUID benchmarks
35+
1736
func BenchmarkNewV7(b *testing.B) {
1837
for i := 0; i < b.N; i++ {
1938
a, _ := uuid.NewV7()
2039
_ = a // prevent compiler optimization
2140
}
2241
}
2342

24-
func BenchmarkGoogleV4(b *testing.B) {
43+
func BenchmarkGoogleV7(b *testing.B) {
2544
for i := 0; i < b.N; i++ {
26-
a, _ := guid.NewRandom()
45+
a, _ := google.NewV7()
2746
_ = a // prevent compiler optimization
2847
}
2948
}
3049

31-
func BenchmarkGoogleV7(b *testing.B) {
50+
func BenchmarkGofrsV7(b *testing.B) {
3251
for i := 0; i < b.N; i++ {
33-
a, _ := guid.NewV7()
52+
a, _ := gofrs.NewV7()
3453
_ = a // prevent compiler optimization
3554
}
3655
}

bench/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
github.com/cmackenzie1/go-uuid v1.1.3
7+
github.com/gofrs/uuid/v5 v5.3.0
78
github.com/google/uuid v1.6.0
89
)
910

bench/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
github.com/gofrs/uuid/v5 v5.3.0 h1:m0mUMr+oVYUdxpMLgSYCZiXe7PuVPnI94+OMeVBNedk=
2+
github.com/gofrs/uuid/v5 v5.3.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
13
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
24
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

0 commit comments

Comments
 (0)