Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predicates/traffic: speedup TrafficSegment tests #2509

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions predicates/traffic/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"

"golang.org/x/sync/errgroup"

"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/predicates"
Expand Down Expand Up @@ -56,14 +60,30 @@ func requestWithR(r float64) *http.Request {
// Results use float64 type to simplify fractional comparisons.
func doN(t *testing.T, client *proxytest.TestClient, request func() *http.Request) (map[int]float64, float64) {
const n = 10_000

var mu sync.Mutex
codes := make(map[int]float64)
for i := 0; i < n; i++ {
rsp, err := client.Do(request())
require.NoError(t, err)
rsp.Body.Close()

codes[rsp.StatusCode]++
var g errgroup.Group
g.SetLimit(runtime.NumCPU())

for i := 0; i < n; i++ {
g.Go(func() error {
rsp, err := client.Do(request())
if err != nil {
return err
}
rsp.Body.Close()

mu.Lock()
codes[rsp.StatusCode]++
mu.Unlock()

return nil
})
}
require.NoError(t, g.Wait())

return codes, n
}

Expand Down Expand Up @@ -132,9 +152,9 @@ func TestTrafficSegmentSplit(t *testing.T) {
},
},
Routes: eskip.MustParse(`
r50: Path("/test") && TrafficSegment(0.0, 0.5) -> status(200) -> <shunt>;
r30: Path("/test") && TrafficSegment(0.5, 0.8) -> status(201) -> <shunt>;
r20: Path("/test") && TrafficSegment(0.8, 1.0) -> status(202) -> <shunt>;
r50: Path("/test") && TrafficSegment(0.0, 0.5) -> status(200) -> inlineContent("") -> <shunt>;
r30: Path("/test") && TrafficSegment(0.5, 0.8) -> status(201) -> inlineContent("") -> <shunt>;
r20: Path("/test") && TrafficSegment(0.8, 1.0) -> status(202) -> inlineContent("") -> <shunt>;
`),
}.Create()
defer p.Close()
Expand All @@ -157,9 +177,9 @@ func TestTrafficSegmentRouteWeight(t *testing.T) {
},
},
Routes: eskip.MustParse(`
segment90: Path("/test") && TrafficSegment(0.0, 0.9) -> status(200) -> <shunt>;
segment10: Path("/test") && TrafficSegment(0.9, 1.0) -> status(200) -> <shunt>;
cookie: Path("/test") && Header("X-Foo", "bar") -> status(201) -> <shunt>;
segment90: Path("/test") && TrafficSegment(0.0, 0.9) -> status(200) -> inlineContent("") -> <shunt>;
segment10: Path("/test") && TrafficSegment(0.9, 1.0) -> status(200) -> inlineContent("") -> <shunt>;
cookie: Path("/test") && Header("X-Foo", "bar") -> status(201) -> inlineContent("") -> <shunt>;
`),
}.Create()
defer p.Close()
Expand Down Expand Up @@ -192,8 +212,8 @@ func TestTrafficSegmentTeeLoopback(t *testing.T) {
},
},
Routes: eskip.MustParse(fmt.Sprintf(`
r0: * -> status(200) -> <shunt>;
r1: Path("/test") && TrafficSegment(0.0, 0.5) -> teeLoopback("a-loop") -> status(201) -> <shunt>;
r0: * -> status(200) -> inlineContent("") -> <shunt>;
r1: Path("/test") && TrafficSegment(0.0, 0.5) -> teeLoopback("a-loop") -> status(201) -> inlineContent("") -> <shunt>;
r2: Path("/test") && Tee("a-loop") && True() -> "%s";
`, loopBackend.URL)),
}.Create()
Expand Down Expand Up @@ -224,9 +244,9 @@ func TestTrafficSegmentLoopbackBackend(t *testing.T) {
},
},
Routes: eskip.MustParse(`
r0: * -> status(200) -> <shunt>;
r0: * -> status(200) -> inlineContent("") -> <shunt>;
r1: Path("/test") && TrafficSegment(0.0, 0.5) -> setPath("a-loop") -> <loopback>;
r2: Path("/a-loop") -> status(201) -> <shunt>;
r2: Path("/a-loop") -> status(201) -> inlineContent("") -> <shunt>;
`),
}.Create()
defer p.Close()
Expand Down