mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-02 16:30:17 +02:00
New integration test fixtures (#5233)
* Initial test environment implementation * linter pass * wip: update request latency test * bugfixes * Fix logic race in envoy process monitor when canceling context * skip tests using test environment on non-linux
This commit is contained in:
parent
3d958ff9c5
commit
526e2a58d6
29 changed files with 2972 additions and 101 deletions
87
internal/benchmarks/latency_bench_test.go
Normal file
87
internal/benchmarks/latency_bench_test.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package benchmarks_test
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/testenv"
|
||||
"github.com/pomerium/pomerium/internal/testenv/scenarios"
|
||||
"github.com/pomerium/pomerium/internal/testenv/snippets"
|
||||
"github.com/pomerium/pomerium/internal/testenv/upstreams"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var (
|
||||
numRoutes int
|
||||
dumpErrLogs bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.IntVar(&numRoutes, "routes", 100, "number of routes")
|
||||
flag.BoolVar(&dumpErrLogs, "dump-err-logs", false, "if the test fails, write all captured logs to a file (testdata/<test-name>)")
|
||||
}
|
||||
|
||||
func TestRequestLatency(t *testing.T) {
|
||||
env := testenv.New(t, testenv.Silent())
|
||||
users := []*scenarios.User{}
|
||||
for i := range numRoutes {
|
||||
users = append(users, &scenarios.User{
|
||||
Email: fmt.Sprintf("user%d@example.com", i),
|
||||
FirstName: fmt.Sprintf("Firstname%d", i),
|
||||
LastName: fmt.Sprintf("Lastname%d", i),
|
||||
})
|
||||
}
|
||||
env.Add(scenarios.NewIDP(users))
|
||||
|
||||
up := upstreams.HTTP(nil)
|
||||
up.Handle("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
})
|
||||
routes := make([]testenv.Route, numRoutes)
|
||||
for i := range numRoutes {
|
||||
routes[i] = up.Route().
|
||||
From(env.SubdomainURL(fmt.Sprintf("from-%d", i))).
|
||||
PPL(fmt.Sprintf(`{"allow":{"and":["email":{"is":"user%d@example.com"}]}}`, i))
|
||||
}
|
||||
env.AddUpstream(up)
|
||||
|
||||
env.Start()
|
||||
snippets.WaitStartupComplete(env)
|
||||
|
||||
out := testing.Benchmark(func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
var rec *testenv.LogRecorder
|
||||
if dumpErrLogs {
|
||||
rec = env.NewLogRecorder(testenv.WithSkipCloseDelay())
|
||||
}
|
||||
for pb.Next() {
|
||||
idx := rand.IntN(numRoutes)
|
||||
resp, err := up.Get(routes[idx], upstreams.AuthenticateAs(fmt.Sprintf("user%d@example.com", idx)))
|
||||
if !assert.NoError(b, err) {
|
||||
filename := "TestRequestLatency_err.log"
|
||||
if dumpErrLogs {
|
||||
rec.DumpToFile(filename)
|
||||
b.Logf("test logs written to %s", filename)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(b, resp.StatusCode, 200)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
assert.NoError(b, err)
|
||||
assert.Equal(b, "OK", string(body))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Log(out)
|
||||
t.Logf("req/s: %f", float64(out.N)/out.T.Seconds())
|
||||
|
||||
env.Stop()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue