github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/bench/microbenchmarks/hrw/helpers.go (about) 1 // Package hrw provides a way to benchmark different HRW variants. 2 // See /bench/hrw/README.md for more info. 3 /* 4 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 5 */ 6 package hrw 7 8 import ( 9 "fmt" 10 "math/rand" 11 "strconv" 12 13 "github.com/NVIDIA/aistore/cmn/cos" 14 "github.com/OneOfOne/xxhash" 15 ) 16 17 type hashFuncs struct { 18 name string 19 hashF func(string, []node) int 20 countObjs []int 21 } 22 23 const ( 24 objNameLen = 50 25 fqnMaxLen = 128 26 ) 27 28 // Duplicated on purpose to avoid dependency on any AIStore code. 29 func randFileName(src *rand.Rand, nameLen int) string { 30 const ( 31 letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 32 letterIdxBits = 6 // 6 bits to represent a letter index 33 letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits 34 letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits 35 ) 36 37 b := make([]byte, nameLen) 38 // A src.Int63() generates 63 random bits, enough for letterIdxMax characters! 39 for i, cache, remain := nameLen-1, src.Int63(), letterIdxMax; i >= 0; { 40 if remain == 0 { 41 cache, remain = src.Int63(), letterIdxMax 42 } 43 if idx := int(cache & letterIdxMask); idx < len(letterBytes) { 44 b[i] = letterBytes[idx] 45 i-- 46 } 47 cache >>= letterIdxBits 48 remain-- 49 } 50 51 return string(b) 52 } 53 54 func similarFileName(bucketName string, objNum int) string { 55 paddedLen := objNameLen - len(strconv.Itoa(objNum)) - len("set-") 56 objectName := fmt.Sprintf("set-%0*d", paddedLen, objNum) 57 return bucketName + "/" + objectName 58 } 59 60 // Duplicated on purpose to avoid dependency on any AIStore code. 61 func randNodeID(randGen *rand.Rand) string { 62 randIP := "" 63 for range 3 { 64 randIP += strconv.Itoa(randGen.Intn(255)) + "." 65 } 66 randIP += strconv.Itoa(randGen.Intn(255)) 67 cksum := xxhash.Checksum32S(cos.UnsafeB(randIP), xxHashSeed) 68 nodeID := strconv.Itoa(int(cksum & 0xfffff)) 69 randPort := strconv.Itoa(randGen.Intn(65535)) 70 return nodeID + "_" + randPort 71 } 72 73 func randNodeIDs(numNodes int, randGen *rand.Rand) []node { 74 nodes := make([]node, numNodes) 75 for i := range numNodes { 76 id := randNodeID(randGen) 77 xhash := xxhash.NewS64(xxHashSeed) 78 xhash.WriteString(id) 79 seed := xxhash.Checksum64S(cos.UnsafeB(id), xxHashSeed) 80 nodes[i] = node{ 81 id: id, 82 idDigestInt: xorshift64(seed), 83 idDigestXX: xhash, 84 } 85 } 86 return nodes 87 } 88 89 func get3DSlice(numRoutines, numFuncs, numNodes int) [][][]int { 90 perRoutine := make([][][]int, numRoutines) 91 for w := range numRoutines { 92 perFunc := make([][]int, numFuncs) 93 for p := range perFunc { 94 perFunc[p] = make([]int, numNodes) 95 } 96 perRoutine[w] = perFunc 97 } 98 return perRoutine 99 } 100 101 func xorshift64(x uint64) uint64 { 102 x ^= x >> 12 // a 103 x ^= x << 25 // b 104 x ^= x >> 27 // c 105 return x * 2685821657736338717 106 }