Last active
August 29, 2015 14:27
-
-
Save paulmach/74bc0a2df4b71bd8d0da to your computer and use it in GitHub Desktop.
Check uniformity of consistent hash using crc32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"math/rand" | |
"time" | |
"github.com/strava/go.serversets/fixedset" | |
"github.com/strava/go.serversets/mcset" | |
) | |
func init() { | |
rand.Seed(time.Now().UnixNano()) | |
} | |
func main() { | |
watch := fixedset.New([]string{"10.0.16.69:11211", "10.0.22.188:11211", "10.0.26.214:11211"}) | |
cluster := mcset.New(watch) | |
log.Printf("cluster endpoints: %v", cluster.Endpoints()) | |
////////////////////////////////// | |
picks := make(map[string]int) | |
for i := 0; i < 100000; i++ { | |
addr, _ := cluster.PickServer(RandStringBytes(100)) | |
picks[addr.String()]++ | |
} | |
log.Printf("mcset results") | |
log.Printf("\t%d\t%v", picks["10.0.16.69:11211"], "10.0.16.69:11211") | |
log.Printf("\t%d\t%v", picks["10.0.22.188:11211"], "10.0.22.188:11211") | |
log.Printf("\t%d\t%v", picks["10.0.26.214:11211"], "10.0.26.214:11211") | |
} | |
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345/" | |
func RandStringBytes(n int) string { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = letterBytes[rand.Intn(len(letterBytes))] | |
} | |
return string(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code sample from wikipedia: