Skip to content

Commit

Permalink
Add BenchmarkSwissTableQueue.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rj1k committed Mar 19, 2024
1 parent d935bc3 commit dcf51f4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 29 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ goos: linux
goarch: amd64
pkg: github.com/s3rj1k/go-randset
cpu: 12th Gen Intel(R) Core(TM) i5-1240P
BenchmarkRandomSet-16 15442075 83.31 ns/op 0 B/op 0 allocs/op
BenchmarkSliceQueue-16 231713943 5.094 ns/op 39 B/op 0 allocs/op
BenchmarkChannelQueue-16 40679883 29.27 ns/op 0 B/op 0 allocs/op
BenchmarkSyncMapQueue-16 3498108 338.0 ns/op 24 B/op 2 allocs/op
PASS
ok github.com/s3rj1k/go-randset 6.887s
BenchmarkSliceQueue-16 226308338 4.814 ns/op 39 B/op 0 allocs/op
BenchmarkChannelQueue-16 40899385 29.27 ns/op 0 B/op 0 allocs/op
BenchmarkSwissTableQueue-16 20348682 381.9 ns/op 3 B/op 0 allocs/op
BenchmarkRandomSet-16 12879373 88.29 ns/op 0 B/op 0 allocs/op
BenchmarkSyncMapQueue-16 3520684 335.1 ns/op 24 B/op 2 allocs/op
```

## Getting Started
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/s3rj1k/go-randset

go 1.22.0

require github.com/dolthub/swiss v0.2.1

require github.com/dolthub/maphash v0.1.0 // indirect
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
github.com/dolthub/swiss v0.2.1 h1:gs2osYs5SJkAaH5/ggVJqXQxRXtWshF6uE0lgR/Y3Gw=
github.com/dolthub/swiss v0.2.1/go.mod h1:8AhKZZ1HK7g18j7v7k6c5cYIGEZJcPn0ARsai8cUrh0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
82 changes: 59 additions & 23 deletions randset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

"github.com/dolthub/swiss"
"github.com/s3rj1k/go-randset"
)

Expand Down Expand Up @@ -162,29 +163,6 @@ func TestLoadAndDelete(t *testing.T) {
}
}

func BenchmarkRandomSet(b *testing.B) {
set := randset.NewWithInitialSize[uint64](setSize)

for set.Size() < setSize {
set.Add(rand.Uint64()) //nolint:gosec // G404: Use of weak random number generator
}

if set.Size() != setSize {
b.Fatalf("Initial setup failed, set size = %d, expected %d", set.Size(), setSize)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
val, found := set.LoadAndDelete()
if !found {
b.Fatalf("%s() failed to return a value, set might be empty", fnLoadAndDelete)
}

set.Add(val)
}
}

func BenchmarkSliceQueue(b *testing.B) {
s := make([]uint64, setSize)

Expand Down Expand Up @@ -217,6 +195,64 @@ func BenchmarkChannelQueue(b *testing.B) {
}
}

func BenchmarkSwissTableQueue(b *testing.B) {
sm := swiss.NewMap[uint64, struct{}](setSize)

for range setSize {
for {
key := rand.Uint64() //nolint:gosec // G404: Use of weak random number generator

if found := sm.Has(key); found {
continue
}

sm.Put(key, struct{}{})

break
}
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
var key uint64

sm.Iter(func(k uint64, _ struct{}) (stop bool) {
key = k
if ok := sm.Delete(k); !ok {

Check failure on line 222 in randset_test.go

View workflow job for this annotation

GitHub Actions / Go Lint

if statements should only be cuddled with assignments used in the if statement itself (wsl)
b.Fatal("Failed to delete data from SwissTable")
}

return true
})

sm.Put(key, struct{}{})
}
}

func BenchmarkRandomSet(b *testing.B) {
set := randset.NewWithInitialSize[uint64](setSize)

for set.Size() < setSize {
set.Add(rand.Uint64()) //nolint:gosec // G404: Use of weak random number generator
}

if set.Size() != setSize {
b.Fatalf("Initial setup failed, set size = %d, expected %d", set.Size(), setSize)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
val, found := set.LoadAndDelete()
if !found {
b.Fatalf("%s() failed to return a value, set might be empty", fnLoadAndDelete)
}

set.Add(val)
}
}

func BenchmarkSyncMapQueue(b *testing.B) {
var sm sync.Map

Expand Down

0 comments on commit dcf51f4

Please sign in to comment.