From 0a1b5bfb51c895f547088b0f3110ee722b038dd6 Mon Sep 17 00:00:00 2001 From: Fangliding Date: Thu, 5 Feb 2026 20:25:32 +0800 Subject: [PATCH] Bench --- common/mux/bench_test.go | 70 +++++++++++++++++++++++++++++++++++++++ common/mux/server_test.go | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 common/mux/bench_test.go diff --git a/common/mux/bench_test.go b/common/mux/bench_test.go new file mode 100644 index 00000000..6eb6dc2a --- /dev/null +++ b/common/mux/bench_test.go @@ -0,0 +1,70 @@ +package mux_test + +import ( + "context" + "testing" + + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/mux" + "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/common/session" + "github.com/xtls/xray-core/transport" + "github.com/xtls/xray-core/transport/pipe" +) + +func BenchmarkMuxThroughput(b *testing.B) { + serverCtx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{}}) + muxServerUplink, muxServerDownlink := newLinkPair() + dispatcher := TestDispatcher{ + OnDispatch: func(ctx context.Context, dest net.Destination) (*transport.Link, error) { + inputReader, inputWriter := pipe.New(pipe.WithSizeLimit(512 * 1024)) + outputReader, outputWriter := pipe.New(pipe.WithSizeLimit(512 * 1024)) + go func() { + defer outputWriter.Close() + for { + mb, err := inputReader.ReadMultiBuffer() + if err != nil { + break + } + buf.ReleaseMulti(mb) + } + }() + return &transport.Link{ + Reader: outputReader, + Writer: inputWriter, + }, nil + }, + } + _, err := mux.NewServerWorker(serverCtx, &dispatcher, muxServerUplink) + common.Must(err) + client, err := mux.NewClientWorker(*muxServerDownlink, mux.ClientStrategy{}) + common.Must(err) + clientCtx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{ + Target: net.TCPDestination(net.DomainAddress("www.example.com"), 80), + }}) + muxClientUplink, muxClientDownlink := newLinkPair() + go func() { + for { + mb, err := muxClientDownlink.Reader.ReadMultiBuffer() + if err != nil { + break + } + buf.ReleaseMulti(mb) + } + }() + ok := client.Dispatch(clientCtx, muxClientUplink) + if !ok { + b.Fatal("failed to dispatch") + } + data := buf.FromBytes(make([]byte, 8192)) + b.SetBytes(int64(8192)) + b.ResetTimer() + + for i := 0; i < b.N; i++ { + err := muxClientUplink.Writer.WriteMultiBuffer(buf.MultiBuffer{data}) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/common/mux/server_test.go b/common/mux/server_test.go index 4158bf46..65cbc946 100644 --- a/common/mux/server_test.go +++ b/common/mux/server_test.go @@ -15,7 +15,7 @@ import ( ) func newLinkPair() (*transport.Link, *transport.Link) { - opt := pipe.WithoutSizeLimit() + opt := pipe.WithSizeLimit(512 * 1024) uplinkReader, uplinkWriter := pipe.New(opt) downlinkReader, downlinkWriter := pipe.New(opt)