This commit is contained in:
Fangliding
2026-02-05 20:25:32 +08:00
parent d14767d4f3
commit 0a1b5bfb51
2 changed files with 71 additions and 1 deletions

70
common/mux/bench_test.go Normal file
View File

@@ -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)
}
}
}

View File

@@ -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)