-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (65 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"hash/fnv"
"math/rand"
"strconv"
"testing"
"github.com/google/uuid"
)
const (
maxShards = 8
)
func uuidToInt(uuid string) int {
// Hash the UUID
hash := md5.Sum([]byte(uuid))
hashStr := hex.EncodeToString(hash[:])
// fmt.Println(hashStr)
// Convert hexadecimal hash to an integer
seed, _ := strconv.ParseInt(hashStr, 16, 64)
// Initialize the random number generator with the hashed UUID
rand.Seed(seed)
// Generate a random number between 1 and 8
randomNumber := rand.Intn(maxShards) + 1
return randomNumber
}
func uuidToInt2(uuid string) int {
hasher := fnv.New32a()
hasher.Write([]byte(uuid))
hash := uint32(hasher.Sum32())
rand.Seed(int64(hash % uint32(maxShards)))
randomNumber := rand.Intn(maxShards) + 1
return randomNumber
}
func main() {
// // Example UUID
// uuid := "550e8400-e29b-41d4-a716-446655440000"
// // Call the function with the same UUID multiple times
// for i := 0; i < 5; i++ {
// result := uuidToShard(uuid)
// fmt.Println("Mapped number:", result)
// }
result := testing.Benchmark(BenchmarkUUIDToInt)
fmt.Printf("Time taken: %s\n", result.T)
fmt.Printf("Memory used: %d bytes\n", result.MemBytes)
fmt.Printf("Iterations: %d\n", result.N)
fmt.Printf("\n \n \n")
result = testing.Benchmark(BenchmarkUUIDToInt2)
fmt.Printf("Time taken: %s\n", result.T)
fmt.Printf("Memory used: %d bytes\n", result.MemBytes)
fmt.Printf("Iterations: %d\n", result.N)
}
func BenchmarkUUIDToInt(b *testing.B) {
//for i := 0; i < b.N; i++ {
newUUID := uuid.New()
uuidToInt(newUUID.String())
//}
}
func BenchmarkUUIDToInt2(b *testing.B) {
//for i := 0; i < b.N; i++ {
newUUID := uuid.New()
uuidToInt2(newUUID.String())
//}
}