Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion client/cmd/ycsb/ycsb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"math/rand"
"slices"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -135,7 +136,9 @@ func hashKey(keyNum int) string {
h *= prime64
n >>= 8
}
return fmt.Sprintf("user%x", h)
// "user" plus at most 16 hexadecimal digits fits in this stack buffer.
key := [20]byte{'u', 's', 'e', 'r'}
return string(strconv.AppendUint(key[:4], h, 16))
}

// casKey names the CAS-guarded twin of key number keyNum. CAS keys live in
Expand Down
38 changes: 38 additions & 0 deletions client/cmd/ycsb/ycsb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"net"
"strings"
Expand All @@ -23,6 +24,43 @@ import (
"google.golang.org/grpc/status"
)

func TestHashKeyMatchesFormatter(t *testing.T) {
maxInt := int(^uint(0) >> 1)
tests := []struct {
name string
keyNum int
}{
{name: "zero", keyNum: 0},
{name: "positive", keyNum: 42},
{name: "negative", keyNum: -42},
{name: "max int", keyNum: maxInt},
{name: "min int", keyNum: -maxInt - 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, want := hashKey(tt.keyNum), formatterHashKey(tt.keyNum); got != want {
t.Errorf("hashKey(%d) = %q, want %q", tt.keyNum, got, want)
}
})
}
}

func formatterHashKey(keyNum int) string {
const (
offset64 = 14695981039346656037
prime64 = 1099511628211
)
h := uint64(offset64)
n := uint64(keyNum)
for range 8 {
h ^= n & 0xff
h *= prime64
n >>= 8
}
return fmt.Sprintf("user%x", h)
}

const (
ycsbReadWorkers = 4
ycsbReadKeys = 1
Expand Down
Loading