Skip to content

Commit

Permalink
add benchmark tests for package app
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Dec 8, 2023
1 parent d42c952 commit e148f9e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
82 changes: 82 additions & 0 deletions pkg/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,49 @@ func TestCloseIdleConnections(t *testing.T) {
}
}

func BenchmarkCloseIdleConnections(b *testing.B) {
opt := config.NewOptions([]config.Option{})
opt.Addr = "unix-test-10000"
opt.Network = "unix"
engine := route.NewEngine(opt)

go engine.Run()
defer func() {
engine.Close()
}()
time.Sleep(time.Millisecond * 500)

b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
c, _ := NewClient(WithDialer(newMockDialerWithCustomFunc(opt.Network, opt.Addr, 1*time.Second, nil)))
if _, _, err := c.Get(context.Background(), nil, "http://google.com"); err != nil {
b.Fatal(err)
}
connsLen := func() int {
c.mLock.Lock()
defer c.mLock.Unlock()

if _, ok := c.m["google.com"]; !ok {
return 0
}
return c.m["google.com"].ConnectionCount()
}

if conns := connsLen(); conns > 1 {
b.Errorf("expected 1 conns got %d", conns)
}

c.CloseIdleConnections()

if conns := connsLen(); conns > 0 {
b.Errorf("expected 0 conns got %d", conns)
}
}
})
}

func TestClientInvalidURI(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -190,6 +233,45 @@ func TestClientGetWithBody(t *testing.T) {
}
}

func BenchmarkClientGetWithBody(b *testing.B) {
opt := config.NewOptions([]config.Option{})
opt.Addr = "unix-test-10002"
opt.Network = "unix"
engine := route.NewEngine(opt)
engine.GET("/", func(c context.Context, ctx *app.RequestContext) {
body := ctx.Request.Body()
ctx.Write(body) //nolint:errcheck
})
go engine.Run()
defer func() {
engine.Close()
}()
time.Sleep(time.Millisecond * 500)

c, _ := NewClient(WithDialer(newMockDialerWithCustomFunc(opt.Network, opt.Addr, 1*time.Second, nil)))
req, res := protocol.AcquireRequest(), protocol.AcquireResponse()
defer func() {
protocol.ReleaseRequest(req)
protocol.ReleaseResponse(res)
}()
req.Header.SetMethod(consts.MethodGet)
req.SetRequestURI("http://example.com")
req.SetBodyString("test")

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := c.Do(context.Background(), req, res)
if err != nil {
b.Fatal(err)
}
if len(res.Body()) == 0 {
b.Fatal("missing request body")
}
res.Reset()
}
}

func TestClientPostBodyStream(t *testing.T) {
t.Parallel()

Expand Down
36 changes: 36 additions & 0 deletions pkg/app/middlewares/client/sd/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,39 @@ func TestDiscovery(t *testing.T) {
_ = mw(checkMdw)(context.Background(), req, resp)
}
}

func BenchmarkDiscovery(b *testing.B) {
instances := []discovery.Instance{
discovery.NewInstance("tcp", "127.0.0.1:8888", 10, nil),
discovery.NewInstance("tcp", "127.0.0.1:8889", 10, nil),
}
r := &discovery.SynthesizedResolver{
TargetFunc: func(ctx context.Context, target *discovery.TargetInfo) string {
return target.Host
},
ResolveFunc: func(ctx context.Context, key string) (discovery.Result, error) {
return discovery.Result{CacheKey: "svc1", Instances: instances}, nil
},
NameFunc: func() string { return b.Name() },
}

midware := Discovery(r)
checkMdw := func(ctx context.Context, req *protocol.Request, resp *protocol.Response) (err error) {
assert.Assert(b, string(req.Host()) == "127.0.0.1:8888" || string(req.Host()) == "127.0.0.1:8889")
return nil
}

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
req := protocol.AcquireRequest()
resp := protocol.AcquireResponse()

req.Options().Apply([]config.RequestOption{config.WithSD(true)})
req.SetRequestURI("http://service_name")
_ = midware(checkMdw)(context.Background(), req, resp)

protocol.ReleaseRequest(req)
protocol.ReleaseResponse(resp)
}
}

0 comments on commit e148f9e

Please sign in to comment.