diff --git a/modules/caddyhttp/reverseproxy/hash_bench_test.go b/modules/caddyhttp/reverseproxy/hash_bench_test.go new file mode 100644 index 000000000..7b94dc34e --- /dev/null +++ b/modules/caddyhttp/reverseproxy/hash_bench_test.go @@ -0,0 +1,18 @@ +package reverseproxy + +import ( + "strconv" + "testing" +) + +func BenchmarkHostByHashing(b *testing.B) { + pool := make(UpstreamPool, 0, 8) + for i := 0; i < 8; i++ { + pool = append(pool, &Upstream{Host: new(Host), Dial: "10.0.0." + strconv.Itoa(i) + ":8080"}) + } + const key = "192.168.1.100" + b.ReportAllocs() + for b.Loop() { + _ = hostByHashing(pool, key) + } +} diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go index 751aa0306..f12620cf7 100644 --- a/modules/caddyhttp/reverseproxy/selectionpolicies.go +++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go @@ -868,9 +868,7 @@ func hostByHashing(pool []*Upstream, s string) *Upstream { // hash calculates a fast hash based on s. func hash(s string) uint64 { - h := xxhash.New() - _, _ = h.Write([]byte(s)) - return h.Sum64() + return xxhash.Sum64String(s) } func loadFallbackPolicy(d *caddyfile.Dispenser) (json.RawMessage, error) {