shuffle sharding: add some benchmarks and tests

Signed-off-by: Bruce Ma <brucema19901024@gmail.com>
This commit is contained in:
Bruce Ma
2019-08-05 21:07:07 +08:00
committed by Mike Spreitzer
parent 1d30ce12bc
commit 3328046a34
2 changed files with 196 additions and 16 deletions

View File

@@ -72,9 +72,9 @@ func DealWithValidation(hashValue uint64, numQueues, handSize int32, pick func(i
return Deal(hashValue, numQueues, handSize, pick)
}
// DealToSlices will use specific pick function to return slices of indices
// DealToSlice will use specific pick function to return slices of indices
// after Deal
func DealToSlices(hashValue uint64, numQueues, handSize int32) ([]int32, error) {
func DealToSlice(hashValue uint64, numQueues, handSize int32) ([]int32, error) {
if !ValidateParameters(numQueues, handSize) {
return nil, errors.New("bad parameters")
}
@@ -90,9 +90,7 @@ func DealToSlices(hashValue uint64, numQueues, handSize int32) ([]int32, error)
return nil
}
if err := Deal(hashValue, numQueues, handSize, pickToSlices); err != nil {
return nil, err
}
_ = Deal(hashValue, numQueues, handSize, pickToSlices)
return candidates, nil
}

View File

@@ -17,18 +17,20 @@ limitations under the License.
package shufflesharding
import (
"errors"
"math/rand"
"testing"
)
func TestValidateParameters(t *testing.T) {
tests := []struct {
name string
queueSize int32
numQueues int32
handSize int32
validated bool
}{
{
"queueSize is < 0",
"numQueues is < 0",
-100,
8,
false,
@@ -40,7 +42,7 @@ func TestValidateParameters(t *testing.T) {
false,
},
{
"queueSize is 0",
"numQueues is 0",
0,
8,
false,
@@ -52,25 +54,25 @@ func TestValidateParameters(t *testing.T) {
false,
},
{
"handSize is greater than queueSize",
"handSize is greater than numQueues",
128,
129,
false,
},
{
"queueSize: 128 handSize: 6",
"numQueues: 128 handSize: 6",
128,
6,
true,
},
{
"queueSize: 1024 handSize: 6",
"numQueues: 1024 handSize: 6",
1024,
6,
true,
},
{
"queueSize: 512 handSize: 8",
"numQueues: 512 handSize: 8",
512,
8,
false,
@@ -78,7 +80,7 @@ func TestValidateParameters(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if ValidateParameters(test.queueSize, test.handSize) != test.validated {
if ValidateParameters(test.numQueues, test.handSize) != test.validated {
t.Errorf("test case %s fails", test.name)
return
}
@@ -87,9 +89,189 @@ func TestValidateParameters(t *testing.T) {
}
func BenchmarkValidateParameters(b *testing.B) {
queueSize, handSize := int32(512), int32(8)
for i := 0; i < b.N; i++ {
//queueSize, handSize := uint32(rand.Intn(513)), uint32(rand.Intn(17))
queueSize, handSize := int32(512), int32(8)
ValidateParameters(queueSize, handSize)
_ = ValidateParameters(queueSize, handSize)
}
}
func TestDealWithValidation(t *testing.T) {
tests := []struct {
name string
numQueues int32
handSize int32
pick func(int32) error
validated bool
}{
{
"numQueues is < 0",
-100,
8,
func(i int32) error {
return nil
},
false,
},
{
"handSize is < 0",
128,
-100,
func(i int32) error {
return nil
},
false,
},
{
"numQueues is 0",
0,
8,
func(i int32) error {
return nil
},
false,
},
{
"handSize is 0",
128,
0,
func(i int32) error {
return nil
},
false,
},
{
"handSize is greater than numQueues",
128,
129,
func(i int32) error {
return nil
},
false,
},
{
"numQueues: 128 handSize: 6",
128,
6,
func(i int32) error {
return nil
},
true,
},
{
"numQueues: 1024 handSize: 6",
1024,
6,
func(i int32) error {
return nil
},
true,
},
{
"numQueues: 128 handSize: 6 with bad pick",
128,
6,
func(i int32) error {
return errors.New("for test")
},
false,
},
{
"numQueues: 512 handSize: 8",
512,
8,
func(i int32) error {
return nil
},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if (DealWithValidation(rand.Uint64(), test.numQueues, test.handSize, test.pick) == nil) != test.validated {
t.Errorf("test case %s fails", test.name)
return
}
})
}
}
func BenchmarkDeal(b *testing.B) {
hashValue := rand.Uint64()
queueSize, handSize := int32(512), int32(8)
pick := func(int32) error {
return nil
}
for i := 0; i < b.N; i++ {
_ = Deal(hashValue, queueSize, handSize, pick)
}
}
func TestDealToSlices(t *testing.T) {
tests := []struct {
name string
numQueues int32
handSize int32
validated bool
}{
{
"validation fails",
-100,
-100,
false,
},
{
"numQueues == handSize == 4",
4,
4,
true,
},
{
"numQueues == handSize == 8",
8,
8,
true,
},
{
"numQueues == handSize == 10",
10,
10,
true,
},
{
"numQueues == handSize == 12",
12,
12,
true,
},
}
for _, test := range tests {
hashValue := rand.Uint64()
t.Run(test.name, func(t *testing.T) {
cards, err := DealToSlice(hashValue, test.numQueues, test.handSize)
if (err == nil) != test.validated {
t.Errorf("test case %s fails in validation check", test.name)
return
}
if test.validated {
// check cards number
if len(cards) != int(test.handSize) {
t.Errorf("test case %s fails in cards number", test.name)
return
}
// check cards duplication
cardMap := make(map[int32]struct{}, test.handSize)
for _, cardIdx := range cards {
cardMap[cardIdx] = struct{}{}
}
for i := int32(0); i < test.handSize; i++ {
if _, ok := cardMap[i]; !ok {
t.Errorf("test case %s fails in duplication check", test.name)
return
}
}
}
})
}
}