From 155470d4550a777d0f77a06ae9abb8c7ea849a6e Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Mon, 23 May 2022 12:27:34 +0800 Subject: [PATCH] Chore: add benchmark r/w --- test/clash_test.go | 34 ++++++++++++++++++++++++++-------- test/snell_test.go | 2 +- test/ss_test.go | 2 +- test/vmess_test.go | 2 +- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/test/clash_test.go b/test/clash_test.go index 43655246..5e4e0405 100644 --- a/test/clash_test.go +++ b/test/clash_test.go @@ -602,6 +602,10 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { require.NoError(b, err) defer l.Close() + chunkSize := int64(16 * 1024) + chunk := make([]byte, chunkSize) + rand.Read(chunk) + go func() { c, err := l.Accept() if err != nil { @@ -609,12 +613,17 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { } defer c.Close() + go func() { + for { + _, err := c.Write(chunk) + if err != nil { + return + } + } + }() io.Copy(io.Discard, c) }() - chunkSize := int64(16 * 1024) - chunk := make([]byte, chunkSize) - rand.Read(chunk) conn, err := proxy.DialContext(context.Background(), &C.Metadata{ Host: localIP.String(), DstPort: "10001", @@ -625,11 +634,20 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { _, err = conn.Write([]byte("skip protocol handshake")) require.NoError(b, err) - b.SetBytes(chunkSize) - b.ResetTimer() - for i := 0; i < b.N; i++ { - conn.Write(chunk) - } + b.Run("Write", func(b *testing.B) { + b.SetBytes(chunkSize) + for i := 0; i < b.N; i++ { + conn.Write(chunk) + } + }) + + b.Run("Read", func(b *testing.B) { + b.SetBytes(chunkSize) + buf := make([]byte, chunkSize) + for i := 0; i < b.N; i++ { + conn.Read(buf) + } + }) } func TestClash_Basic(t *testing.T) { diff --git a/test/snell_test.go b/test/snell_test.go index a8618812..4c501684 100644 --- a/test/snell_test.go +++ b/test/snell_test.go @@ -151,7 +151,7 @@ func Benchmark_Snell(b *testing.B) { Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell-http.conf"))}, } - id, err := startContainer(cfg, hostCfg, "snell-http") + id, err := startContainer(cfg, hostCfg, "snell-bench") require.NoError(b, err) b.Cleanup(func() { diff --git a/test/ss_test.go b/test/ss_test.go index b2f0b287..13698210 100644 --- a/test/ss_test.go +++ b/test/ss_test.go @@ -168,7 +168,7 @@ func Benchmark_Shadowsocks(b *testing.B) { PortBindings: defaultPortBindings, } - id, err := startContainer(cfg, hostCfg, "ss") + id, err := startContainer(cfg, hostCfg, "ss-bench") require.NoError(b, err) b.Cleanup(func() { diff --git a/test/vmess_test.go b/test/vmess_test.go index b283ce0c..3e8d2759 100644 --- a/test/vmess_test.go +++ b/test/vmess_test.go @@ -372,7 +372,7 @@ func Benchmark_Vmess(b *testing.B) { Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)}, } - id, err := startContainer(cfg, hostCfg, "vmess-aead") + id, err := startContainer(cfg, hostCfg, "vmess-bench") require.NoError(b, err) b.Cleanup(func() {