test: compare native concurrency and server memory
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
package native_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
omni "git.yuntuo.hk/k2safe/go-omni"
|
||||
"git.yuntuo.hk/k2safe/go-omni/transport/native"
|
||||
netrouter "git.yuntuo.hk/k2safe/go-omni/transport/nethttp"
|
||||
)
|
||||
|
||||
// BenchmarkNativeApplicationRequest measures the real TCP path with a reused
|
||||
// client connection. It is intentionally separate from the in-process router
|
||||
// benchmarks in transport/benchmark_test.go.
|
||||
func BenchmarkNativeApplicationRequest(b *testing.B) {
|
||||
application := omni.NewWithRouter(netrouter.New())
|
||||
application.GET("/health", func(ctx *omni.Context) error {
|
||||
return ctx.OK(map[string]string{"status": "ok"})
|
||||
})
|
||||
server := native.New(application)
|
||||
server.MaxConnections = 1024
|
||||
server.MaxConcurrentRequests = 1024
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Serve(context.Background(), listener) }()
|
||||
waitForBenchmarkServer(b, listener.Addr().String())
|
||||
|
||||
client := &http.Client{Transport: &http.Transport{
|
||||
MaxIdleConns: 1024,
|
||||
MaxIdleConnsPerHost: 1024,
|
||||
IdleConnTimeout: time.Minute,
|
||||
}}
|
||||
endpoint := "http://" + listener.Addr().String() + "/health"
|
||||
response, err := client.Get(endpoint)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, response.Body)
|
||||
response.Body.Close()
|
||||
|
||||
var failed atomic.Bool
|
||||
var failure atomic.Value
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
response, err := client.Get(endpoint)
|
||||
if err != nil {
|
||||
if failed.CompareAndSwap(false, true) {
|
||||
failure.Store(err.Error())
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, response.Body)
|
||||
response.Body.Close()
|
||||
}
|
||||
})
|
||||
b.StopTimer()
|
||||
client.CloseIdleConnections()
|
||||
shutdownContext, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(shutdownContext); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if failed.Load() {
|
||||
b.Fatalf("native request failed: %s", failure.Load().(string))
|
||||
}
|
||||
}
|
||||
|
||||
func waitForBenchmarkServer(b *testing.B, address string) {
|
||||
b.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
conn, err := net.DialTimeout("tcp", address, 50*time.Millisecond)
|
||||
if err == nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
b.Fatalf("server did not start: %v", err)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user