package consistenthash

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

func BenchmarkConsistentHash(b *testing.B) {
	nodes := []string{
		"node1",
		"node2",
		"node3",
		"node4",
	}
	rh := NewConsistentHash(nodes, 0)
	b.ReportAllocs()
	b.SetBytes(int64(len(benchKeys)))
	b.RunParallel(func(pb *testing.PB) {
		sum := 0
		for pb.Next() {
			for _, k := range benchKeys {
				idx := rh.GetNodeIdx(k, nil)
				sum += idx
			}
		}
		BenchSink.Add(uint64(sum))
	})
}

var benchKeys = func() []uint64 {
	r := rand.New(rand.NewSource(1))
	keys := make([]uint64, 10000)
	for i := range keys {
		keys[i] = r.Uint64()
	}
	return keys
}()

var BenchSink atomic.Uint64
