Skip to content

Commit

Permalink
predicates/traffic: speedup TrafficSegment tests (#2509)
Browse files Browse the repository at this point in the history
TrafficSegment tests perform 10k test requests per test case.

Add `inlineContent("")` to test routes to have `Content-Length: 0`
response header that disables chunked encoding and speeds up the tests.

Before:
```
go test ./predicates/traffic/ -count=1 -run=TestTrafficSegment
ok      github.com/zalando/skipper/predicates/traffic   8.928s
```

After:
```
go test ./predicates/traffic/ -count=1 -run=TestTrafficSegment
ok      github.com/zalando/skipper/predicates/traffic   4.285s
```

To further speedup make test requests in parallel:
```
go test ./predicates/traffic/ -count=1 -run=TestTrafficSegment
ok      github.com/zalando/skipper/predicates/traffic   1.554s
```

See related #2459 #1562

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Aug 10, 2023
1 parent 679e064 commit 91851ea
Showing 1 changed file with 35 additions and 15 deletions.
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

0 comments on commit 91851ea

Please sign in to comment.