Skip to content

Commit

Permalink
Move benchmarks to submodule.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rj1k committed Mar 19, 2024
1 parent dcf51f4 commit 8b2ba9f
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 141 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- **Zero Dependencies**: Uses only Go's standard library, ensuring easy integration and compatibility.

## Benchmark results.
`$ go test -run=^$ -bench=. -benchmem`
`$ (cd benchmarks && go test -run=^$ -bench=. -benchmem)`

```
goos: linux
Expand Down
149 changes: 149 additions & 0 deletions benchmarks/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package benchmarks_test

import (
"math/rand"
"sync"
"testing"

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

const (
setSize = 500000
)

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

for i := 0; i < setSize; i++ {
s[i] = rand.Uint64() //nolint:gosec // G404: Use of weak random number generator
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
val := s[0]

s = s[1:]
s = append(s, val)
}
}

func BenchmarkChannelQueue(b *testing.B) {
ch := make(chan uint64, setSize)

for range setSize {
ch <- rand.Uint64() //nolint:gosec // G404: Use of weak random number generator
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
val := <-ch
ch <- val
}
}

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 {
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.Fatal("Failed to return a value, set might be empty")
}

set.Add(val)
}
}

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

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

_, found := sm.Load(key)
if found {
continue
}

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

break
}
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
var (
key uint64
ok bool
)

sm.Range(func(k, _ any) bool {
key, ok = k.(uint64)
if !ok {
b.Fatalf("Unexpected data type: %T", key)
}

return false
})

_, ok = sm.LoadAndDelete(key)
if !ok {
b.Fatal("Failed to load data from sync.Map")
}

sm.Store(key, struct{}{})
}
}
12 changes: 12 additions & 0 deletions benchmarks/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/s3rj1k/go-randset/benchmarks

go 1.22.0

replace github.com/s3rj1k/go-randset => ../.

require (
github.com/dolthub/swiss v0.2.1
github.com/s3rj1k/go-randset v0.0.0-00010101000000-000000000000
)

require github.com/dolthub/maphash v0.1.0 // indirect
File renamed without changes.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
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
136 changes: 0 additions & 136 deletions randset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"math/rand"
"reflect"
"slices"
"sync"
"testing"

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

Expand Down Expand Up @@ -162,137 +160,3 @@ func TestLoadAndDelete(t *testing.T) {
t.Errorf("Expected the set to be empty after %d removals, but it was not", setSize)
}
}

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

for i := 0; i < setSize; i++ {
s[i] = rand.Uint64() //nolint:gosec // G404: Use of weak random number generator
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
val := s[0]

s = s[1:]
s = append(s, val)
}
}

func BenchmarkChannelQueue(b *testing.B) {
ch := make(chan uint64, setSize)

for range setSize {
ch <- rand.Uint64() //nolint:gosec // G404: Use of weak random number generator
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
val := <-ch
ch <- val
}
}

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 {
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

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

_, found := sm.Load(key)
if found {
continue
}

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

break
}
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
var (
key uint64
ok bool
)

sm.Range(func(k, _ any) bool {
key, ok = k.(uint64)
if !ok {
b.Fatalf("Unexpected data type: %T", key)
}

return false
})

_, ok = sm.LoadAndDelete(key)
if !ok {
b.Fatal("Failed to load data from sync.Map")
}

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

0 comments on commit 8b2ba9f

Please sign in to comment.