mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
perf: reuse reverse proxy candidate slices
This commit is contained in:
parent
71a344a3de
commit
017bb13295
3 changed files with 86 additions and 8 deletions
|
|
@ -110,10 +110,10 @@ func BenchmarkReverseProxyCopyResponse(b *testing.B) {
|
|||
func BenchmarkReverseProxyAvailableUpstreams(b *testing.B) {
|
||||
proxy := &reverseProxyHandler{
|
||||
upstreams: []*reverseProxyUpstream{
|
||||
{key: "a"},
|
||||
{key: "b"},
|
||||
{key: "c"},
|
||||
{key: "d"},
|
||||
{key: "a", index: 0},
|
||||
{key: "b", index: 1},
|
||||
{key: "c", index: 2},
|
||||
{key: "d", index: 3},
|
||||
},
|
||||
config: ReverseProxyConfig{
|
||||
PassiveHealth: ReverseProxyPassiveHealthConfig{
|
||||
|
|
@ -135,6 +135,38 @@ func BenchmarkReverseProxyAvailableUpstreams(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkReverseProxySelectUpstream(b *testing.B) {
|
||||
proxy := &reverseProxyHandler{
|
||||
upstreams: []*reverseProxyUpstream{
|
||||
{key: "a", index: 0},
|
||||
{key: "b", index: 1},
|
||||
{key: "c", index: 2},
|
||||
{key: "d", index: 3},
|
||||
},
|
||||
config: ReverseProxyConfig{
|
||||
LoadBalancing: ReverseProxyLoadBalancingConfig{Policy: LBRoundRobin()},
|
||||
PassiveHealth: ReverseProxyPassiveHealthConfig{
|
||||
FailDuration: time.Minute,
|
||||
MaxFails: 3,
|
||||
},
|
||||
},
|
||||
}
|
||||
proxy.upstreams[0].failures = []time.Time{time.Now().Add(-30 * time.Second)}
|
||||
|
||||
c, _ := CreateTestContext(nil)
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
selected, err := proxy.selectUpstream(c, nil)
|
||||
if err != nil {
|
||||
b.Fatalf("selectUpstream failed: %v", err)
|
||||
}
|
||||
benchmarkReverseProxySink = selected.index
|
||||
}
|
||||
}
|
||||
|
||||
func TestReverseProxyCopyResponseWithoutBufferPool(t *testing.T) {
|
||||
proxy := newReverseProxyHandler(ReverseProxyConfig{})
|
||||
dst := newBenchmarkResponseWriter()
|
||||
|
|
@ -203,4 +235,29 @@ func TestReverseProxyCopyResponseRespectsCustomBufferLength(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestReverseProxyAvailableUpstreamsFiltersExcludedAndUnhealthy(t *testing.T) {
|
||||
now := time.Now()
|
||||
proxy := &reverseProxyHandler{
|
||||
upstreams: []*reverseProxyUpstream{
|
||||
{key: "a"},
|
||||
{key: "b", failures: []time.Time{now.Add(-20 * time.Second), now.Add(-10 * time.Second)}},
|
||||
{key: "c"},
|
||||
},
|
||||
config: ReverseProxyConfig{
|
||||
PassiveHealth: ReverseProxyPassiveHealthConfig{
|
||||
FailDuration: time.Minute,
|
||||
MaxFails: 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
available := proxy.availableUpstreams(now, map[string]struct{}{"c": {}})
|
||||
if len(available) != 1 {
|
||||
t.Fatalf("expected only one available upstream, got %d", len(available))
|
||||
}
|
||||
if available[0].key != "a" {
|
||||
t.Fatalf("expected upstream 'a', got %q", available[0].key)
|
||||
}
|
||||
}
|
||||
|
||||
var _ io.Writer = (*benchmarkResponseWriter)(nil)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue