2019-01-29 22:58:30 +00:00
|
|
|
package hrw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2019-01-30 09:32:34 +00:00
|
|
|
"math"
|
2019-05-27 07:45:29 +00:00
|
|
|
"math/rand"
|
2019-01-29 22:58:30 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
2019-07-05 06:49:24 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-29 22:58:30 +00:00
|
|
|
)
|
|
|
|
|
2019-04-12 11:09:49 +00:00
|
|
|
type (
|
|
|
|
hashString string
|
|
|
|
unknown byte
|
|
|
|
slices struct {
|
|
|
|
actual interface{}
|
|
|
|
expect interface{}
|
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
|
|
|
|
Uint32Slice []uint32
|
2019-04-12 11:09:49 +00:00
|
|
|
)
|
2019-01-29 22:58:30 +00:00
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
var testKey = []byte("0xff51afd7ed558ccd")
|
2019-01-29 22:58:30 +00:00
|
|
|
|
2019-04-12 11:19:18 +00:00
|
|
|
func (p Uint32Slice) Len() int { return len(p) }
|
|
|
|
func (p Uint32Slice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
|
func (p Uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
|
2019-01-29 22:58:30 +00:00
|
|
|
func Example() {
|
|
|
|
// given a set of servers
|
|
|
|
servers := []string{
|
|
|
|
"one.example.com",
|
|
|
|
"two.example.com",
|
|
|
|
"three.example.com",
|
|
|
|
"four.example.com",
|
|
|
|
"five.example.com",
|
|
|
|
"six.example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
// HRW can consistently select a uniformly-distributed set of servers for
|
|
|
|
// any given key
|
|
|
|
var (
|
|
|
|
key = []byte("/examples/object-key")
|
2019-01-31 09:54:02 +00:00
|
|
|
h = Hash(key)
|
2019-01-29 22:58:30 +00:00
|
|
|
)
|
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
SortSliceByValue(servers, h)
|
2019-01-29 22:58:30 +00:00
|
|
|
for id := range servers {
|
|
|
|
fmt.Printf("trying GET %s%s\n", servers[id], key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
2019-01-31 09:54:02 +00:00
|
|
|
// trying GET three.example.com/examples/object-key
|
2019-01-29 22:58:30 +00:00
|
|
|
// trying GET two.example.com/examples/object-key
|
2019-01-31 09:54:02 +00:00
|
|
|
// trying GET five.example.com/examples/object-key
|
2019-05-24 09:51:39 +00:00
|
|
|
// trying GET six.example.com/examples/object-key
|
|
|
|
// trying GET one.example.com/examples/object-key
|
|
|
|
// trying GET four.example.com/examples/object-key
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
2019-04-12 11:09:49 +00:00
|
|
|
|
2019-01-29 22:58:30 +00:00
|
|
|
func (h hashString) Hash() uint64 {
|
2019-01-31 09:54:02 +00:00
|
|
|
return Hash([]byte(h))
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortSliceByIndex(t *testing.T) {
|
|
|
|
actual := []string{"a", "b", "c", "d", "e", "f"}
|
2019-01-31 09:54:02 +00:00
|
|
|
expect := []string{"e", "a", "c", "f", "d", "b"}
|
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
SortSliceByIndex(actual, hash)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.Equal(t, expect, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateWeights(t *testing.T) {
|
|
|
|
weights := []float64{10, 10, 10, 2, 2, 2}
|
|
|
|
err := ValidateWeights(weights)
|
|
|
|
require.Error(t, err)
|
2019-08-01 09:16:03 +00:00
|
|
|
weights = []float64{math.NaN(), 1, 1, 0.2, 0.2, 0.2}
|
|
|
|
err = ValidateWeights(weights)
|
|
|
|
require.Error(t, err)
|
2019-07-05 06:49:24 +00:00
|
|
|
weights = []float64{1, 1, 1, 0.2, 0.2, 0.2}
|
|
|
|
err = ValidateWeights(weights)
|
|
|
|
require.NoError(t, err)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func TestSortSliceByWeightIndex(t *testing.T) {
|
|
|
|
actual := []string{"a", "b", "c", "d", "e", "f"}
|
2019-07-05 06:49:24 +00:00
|
|
|
weights := []float64{1, 1, 1, 0.2, 0.2, 0.2}
|
2019-05-27 07:45:29 +00:00
|
|
|
expect := []string{"a", "c", "b", "e", "f", "d"}
|
|
|
|
hash := Hash(testKey)
|
|
|
|
SortSliceByWeightIndex(actual, weights, hash)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.Equal(t, expect, actual)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 22:58:30 +00:00
|
|
|
func TestSortSliceByValue(t *testing.T) {
|
|
|
|
actual := []string{"a", "b", "c", "d", "e", "f"}
|
2019-05-24 09:51:39 +00:00
|
|
|
expect := []string{"d", "f", "c", "b", "a", "e"}
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
|
|
|
SortSliceByValue(actual, hash)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.Equal(t, expect, actual)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
func TestSortSliceByValueFail(t *testing.T) {
|
|
|
|
t.Run("empty slice", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
actual []int
|
|
|
|
hash = Hash(testKey)
|
|
|
|
)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.NotPanics(t, func() { SortSliceByValue(actual, hash) })
|
2019-01-31 09:54:02 +00:00
|
|
|
})
|
2019-01-29 22:58:30 +00:00
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
t.Run("must be slice", func(t *testing.T) {
|
|
|
|
actual := 10
|
|
|
|
hash := Hash(testKey)
|
2021-12-28 11:27:29 +00:00
|
|
|
require.Panics(t, func() { SortSliceByValue(actual, hash) })
|
2019-01-29 22:58:30 +00:00
|
|
|
})
|
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
t.Run("must 'fail' for unknown type", func(t *testing.T) {
|
2019-04-12 11:09:49 +00:00
|
|
|
actual := []unknown{1, 2, 3, 4, 5}
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2021-12-28 11:27:29 +00:00
|
|
|
require.Panics(t, func() { SortSliceByValue(actual, hash) })
|
2019-01-29 22:58:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortSliceByValueHasher(t *testing.T) {
|
|
|
|
actual := []hashString{"a", "b", "c", "d", "e", "f"}
|
2019-05-24 09:51:39 +00:00
|
|
|
expect := []hashString{"d", "f", "c", "b", "a", "e"}
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
|
|
|
SortSliceByValue(actual, hash)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.Equal(t, expect, actual)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSortSliceByValueIntSlice(t *testing.T) {
|
2019-04-12 11:09:49 +00:00
|
|
|
cases := []slices{
|
|
|
|
{
|
|
|
|
actual: []int{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []int{2, 0, 5, 3, 1, 4},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []uint{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []uint{2, 0, 5, 3, 1, 4},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []int8{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []int8{5, 2, 1, 4, 0, 3},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []uint8{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []uint8{5, 2, 1, 4, 0, 3},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []int16{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []int16{1, 0, 3, 2, 4, 5},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []uint16{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []uint16{1, 0, 3, 2, 4, 5},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []int32{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []int32{5, 1, 2, 0, 3, 4},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []uint32{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []uint32{5, 1, 2, 0, 3, 4},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []int64{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []int64{5, 3, 0, 1, 4, 2},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
actual: []uint64{0, 1, 2, 3, 4, 5},
|
2019-05-24 09:51:39 +00:00
|
|
|
expect: []uint64{5, 3, 0, 1, 4, 2},
|
2019-04-12 11:09:49 +00:00
|
|
|
},
|
|
|
|
}
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-04-12 11:09:49 +00:00
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
SortSliceByValue(tc.actual, hash)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.Equal(t, tc.expect, tc.actual)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func TestSort(t *testing.T) {
|
2019-01-29 22:58:30 +00:00
|
|
|
nodes := []uint64{1, 2, 3, 4, 5}
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-05-27 07:45:29 +00:00
|
|
|
actual := Sort(nodes, hash)
|
2019-01-31 09:54:02 +00:00
|
|
|
expected := []uint64{3, 1, 4, 2, 0}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.Equal(t, expected, actual)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func TestDistribution(t *testing.T) {
|
2019-01-31 09:54:02 +00:00
|
|
|
const (
|
|
|
|
size = 10
|
|
|
|
keys = 100000
|
|
|
|
percent = 0.03
|
2019-01-29 22:58:30 +00:00
|
|
|
)
|
2019-01-31 09:54:02 +00:00
|
|
|
// We use χ2 method to determine similarity of distribution with uniform distribution.
|
|
|
|
// χ2 = Σ((n-N)**2/N)
|
|
|
|
// https://www.medcalc.org/manual/chi-square-table.php p=0.1
|
|
|
|
var chiTable = map[int]float64{9: 14.68, 99: 117.407}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
t.Run("sort", func(t *testing.T) {
|
2019-01-31 09:54:02 +00:00
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
nodes [size]uint64
|
|
|
|
counts = make(map[uint64]uint64, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
nodes[i] = i
|
|
|
|
}
|
2019-01-29 22:58:30 +00:00
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
for i = 0; i < keys; i++ {
|
2019-05-24 09:51:39 +00:00
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(key)
|
2019-05-27 07:45:29 +00:00
|
|
|
counts[Sort(nodes[:], hash)[0]]++
|
2019-01-31 09:54:02 +00:00
|
|
|
}
|
2019-01-29 22:58:30 +00:00
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-01-31 09:54:02 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-01-31 09:54:02 +00:00
|
|
|
})
|
2019-01-29 22:58:30 +00:00
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
t.Run("sortByIndex", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]uint64
|
|
|
|
counts = make(map[uint64]int, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
|
2019-05-24 09:51:39 +00:00
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByIndex(b[:], hash)
|
|
|
|
counts[b[0]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-01-31 09:54:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sortByValue", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]int
|
|
|
|
counts = make(map[int]int, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
2019-05-24 09:51:39 +00:00
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByValue(b[:], hash)
|
|
|
|
counts[b[0]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-01-31 09:54:02 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-01-31 09:54:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sortByStringValue", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]string
|
|
|
|
counts = make(map[string]int, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = strconv.FormatUint(i, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
2019-05-24 09:51:39 +00:00
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByValue(b[:], hash)
|
|
|
|
counts[b[0]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-01-31 09:54:02 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-01-31 09:54:02 +00:00
|
|
|
})
|
|
|
|
|
2019-02-01 09:57:05 +00:00
|
|
|
t.Run("sortByInt32Value", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]int32
|
|
|
|
counts = make(map[int32]int, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int32(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
2019-05-24 09:51:39 +00:00
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
2019-02-01 09:57:05 +00:00
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByValue(b[:], hash)
|
|
|
|
counts[b[0]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-02-01 09:57:05 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-02-01 09:57:05 +00:00
|
|
|
})
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
t.Run("sortByWeightValue", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b, result [size]int
|
2019-07-05 06:49:24 +00:00
|
|
|
w [size]float64
|
2019-05-27 07:45:29 +00:00
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int(i)
|
2019-07-05 06:49:24 +00:00
|
|
|
w[i] = float64(size-i) / float64(size)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByWeightValue(b[:], w[:], hash)
|
|
|
|
result[b[0]]++
|
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
for i := 0; i < size-1; i++ {
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, bool(w[i] > w[i+1]) == bool(result[i] > result[i+1]),
|
|
|
|
"result array %v must be corresponded to weights %v", result, w)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
t.Run("sortByWeightValueShuffledWeight", func(t *testing.T) {
|
2019-05-27 07:45:29 +00:00
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b, result [size]int
|
2019-07-05 06:49:24 +00:00
|
|
|
w [size]float64
|
2019-05-27 07:45:29 +00:00
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int(i)
|
2019-07-05 06:49:24 +00:00
|
|
|
w[i] = float64(size-i) / float64(size)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rand.Shuffle(size, func(i, j int) {
|
|
|
|
w[i], w[j] = w[j], w[i]
|
|
|
|
})
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByWeightValue(b[:], w[:], hash)
|
|
|
|
result[b[0]]++
|
|
|
|
}
|
|
|
|
for i := 0; i < size-1; i++ {
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, bool(w[i] > w[i+1]) == bool(result[i] > result[i+1]),
|
|
|
|
"result array %v must be corresponded to weights %v", result, w)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
t.Run("sortByWeightValueEmptyWeight", func(t *testing.T) {
|
2019-05-27 07:45:29 +00:00
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]int
|
2019-07-05 06:49:24 +00:00
|
|
|
w [size]float64
|
2019-05-27 07:45:29 +00:00
|
|
|
counts = make(map[int]int, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByWeightValue(b[:], w[:], hash)
|
|
|
|
counts[b[0]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-05-27 07:45:29 +00:00
|
|
|
})
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
t.Run("sortByWeightValueUniformWeight", func(t *testing.T) {
|
2019-05-27 07:45:29 +00:00
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]int
|
2019-07-05 06:49:24 +00:00
|
|
|
w [size]float64
|
2019-05-27 07:45:29 +00:00
|
|
|
counts = make(map[int]int, size)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int(i)
|
2019-07-05 06:49:24 +00:00
|
|
|
w[i] = 0.5
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByWeightValue(b[:], w[:], hash)
|
|
|
|
counts[b[0]]++
|
|
|
|
}
|
|
|
|
|
|
|
|
var chi2 float64
|
|
|
|
mean := float64(keys) / float64(size)
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range counts {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-05-27 07:45:29 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sortByWeightValueAbsoluteW", func(t *testing.T) {
|
2019-07-05 06:49:24 +00:00
|
|
|
const keys = 1
|
2019-05-27 07:45:29 +00:00
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b [size]int
|
2019-07-05 06:49:24 +00:00
|
|
|
w [size]float64
|
2019-05-27 07:45:29 +00:00
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = int(i)
|
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
w[size-1] = 1
|
2019-05-27 07:45:29 +00:00
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByWeightValue(b[:], w[:], hash)
|
2019-07-05 06:49:24 +00:00
|
|
|
require.True(t, b[0] == a[size-1],
|
|
|
|
"expected last value of %v to be the first with highest distance", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sortByWeightValueNormalizedWeight", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
a, b, result [size]uint64
|
|
|
|
w, normalizedW [size]float64
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
a[i] = i
|
|
|
|
w[int(i)] = 10
|
|
|
|
}
|
|
|
|
w[0] = 100
|
|
|
|
|
|
|
|
// Here let's use logarithm normalization
|
|
|
|
for i = 0; i < size; i++ {
|
|
|
|
normalizedW[i] = math.Log2(w[i]) / math.Log2(w[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
|
|
|
copy(b[:], a[:])
|
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
|
|
|
hash := Hash(key)
|
|
|
|
SortSliceByWeightValue(b[:], normalizedW[:], hash)
|
|
|
|
for j := range b {
|
|
|
|
result[b[j]] += uint64(len(b) - j)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
cutResult := result[1:]
|
|
|
|
var total uint64
|
|
|
|
for i := range cutResult {
|
|
|
|
total += cutResult[i]
|
|
|
|
}
|
2019-05-27 07:45:29 +00:00
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
var chi2 float64
|
|
|
|
mean := float64(total) / float64(len(cutResult))
|
|
|
|
delta := mean * percent
|
|
|
|
for node, count := range cutResult {
|
|
|
|
d := mean - float64(count)
|
|
|
|
chi2 += math.Pow(float64(count)-mean, 2) / mean
|
|
|
|
require.True(t, d < delta && (0-d) < delta,
|
|
|
|
"Node %d received %d keys, expected %.0f (+/- %.2f)", node, count, mean, delta)
|
|
|
|
}
|
|
|
|
require.True(t, chi2 < chiTable[size-1],
|
|
|
|
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)", chi2, chiTable[size-1])
|
2019-05-27 07:45:29 +00:00
|
|
|
})
|
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
t.Run("hash collision", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
i uint64
|
|
|
|
counts = make(map[uint64]uint64)
|
|
|
|
key = make([]byte, 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i = 0; i < keys; i++ {
|
2019-05-24 09:51:39 +00:00
|
|
|
binary.BigEndian.PutUint64(key, i+size)
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(key)
|
|
|
|
counts[hash]++
|
|
|
|
}
|
|
|
|
|
|
|
|
for node, count := range counts {
|
|
|
|
if count > 1 {
|
|
|
|
t.Errorf("Node %d received %d keys", node, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func BenchmarkSort_fnv_10(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-05-27 07:45:29 +00:00
|
|
|
_ = benchmarkSort(b, 10, hash)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func BenchmarkSort_fnv_100(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-05-27 07:45:29 +00:00
|
|
|
_ = benchmarkSort(b, 100, hash)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func BenchmarkSort_fnv_1000(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-05-27 07:45:29 +00:00
|
|
|
_ = benchmarkSort(b, 1000, hash)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByIndex_fnv_10(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
benchmarkSortByIndex(b, 10, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByIndex_fnv_100(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
benchmarkSortByIndex(b, 100, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByIndex_fnv_1000(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
benchmarkSortByIndex(b, 1000, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByValue_fnv_10(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
benchmarkSortByValue(b, 10, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByValue_fnv_100(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
benchmarkSortByValue(b, 100, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByValue_fnv_1000(b *testing.B) {
|
2019-01-31 09:54:02 +00:00
|
|
|
hash := Hash(testKey)
|
2019-01-29 22:58:30 +00:00
|
|
|
benchmarkSortByValue(b, 1000, hash)
|
|
|
|
}
|
|
|
|
|
2019-05-27 07:45:29 +00:00
|
|
|
func BenchmarkSortByWeight_fnv_10(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
_ = benchmarkSortByWeight(b, 10, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeight_fnv_100(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
_ = benchmarkSortByWeight(b, 100, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeight_fnv_1000(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
_ = benchmarkSortByWeight(b, 1000, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightIndex_fnv_10(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
benchmarkSortByWeightIndex(b, 10, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightIndex_fnv_100(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
benchmarkSortByWeightIndex(b, 100, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightIndex_fnv_1000(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
benchmarkSortByWeightIndex(b, 1000, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightValue_fnv_10(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
benchmarkSortByWeightValue(b, 10, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightValue_fnv_100(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
benchmarkSortByWeightValue(b, 100, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSortByWeightValue_fnv_1000(b *testing.B) {
|
|
|
|
hash := Hash(testKey)
|
|
|
|
benchmarkSortByWeightValue(b, 1000, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkSort(b *testing.B, n int, hash uint64) uint64 {
|
2019-01-29 22:58:30 +00:00
|
|
|
servers := make([]uint64, n)
|
|
|
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
|
|
|
servers[i] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
var x uint64
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-05-27 07:45:29 +00:00
|
|
|
x += Sort(servers, hash)[0]
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkSortByIndex(b *testing.B, n int, hash uint64) {
|
|
|
|
servers := make([]uint64, n)
|
|
|
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
|
|
|
servers[i] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
SortSliceByIndex(servers, hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkSortByValue(b *testing.B, n int, hash uint64) {
|
|
|
|
servers := make([]string, n)
|
|
|
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
|
|
|
servers[i] = "localhost:" + strconv.FormatUint(60000-i, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-01-31 09:54:02 +00:00
|
|
|
SortSliceByValue(servers, hash)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 07:45:29 +00:00
|
|
|
|
|
|
|
func benchmarkSortByWeight(b *testing.B, n int, hash uint64) uint64 {
|
|
|
|
servers := make([]uint64, n)
|
2019-07-05 06:49:24 +00:00
|
|
|
weights := make([]float64, n)
|
2019-05-27 07:45:29 +00:00
|
|
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
2019-07-05 06:49:24 +00:00
|
|
|
weights[i] = float64(uint64(n)-i) / float64(n)
|
2019-05-27 07:45:29 +00:00
|
|
|
servers[i] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
var x uint64
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x += SortByWeight(servers, weights, hash)[0]
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkSortByWeightIndex(b *testing.B, n int, hash uint64) {
|
|
|
|
servers := make([]uint64, n)
|
2019-07-05 06:49:24 +00:00
|
|
|
weights := make([]float64, n)
|
2019-05-27 07:45:29 +00:00
|
|
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
2019-07-05 06:49:24 +00:00
|
|
|
weights[i] = float64(uint64(n)-i) / float64(n)
|
2019-05-27 07:45:29 +00:00
|
|
|
servers[i] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
SortSliceByWeightIndex(servers, weights, hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkSortByWeightValue(b *testing.B, n int, hash uint64) {
|
|
|
|
servers := make([]string, n)
|
2019-07-05 06:49:24 +00:00
|
|
|
weights := make([]float64, n)
|
2019-05-27 07:45:29 +00:00
|
|
|
for i := uint64(0); i < uint64(len(servers)); i++ {
|
2019-07-05 06:49:24 +00:00
|
|
|
weights[i] = float64(uint64(n)-i) / float64(n)
|
2019-05-27 07:45:29 +00:00
|
|
|
servers[i] = "localhost:" + strconv.FormatUint(60000-i, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
SortSliceByWeightValue(servers, weights, hash)
|
|
|
|
}
|
|
|
|
}
|