Skip to content

Commit ce59419

Browse files
committed
Add benchmark for tcp proxy use case
Signed-off-by: Fabian Ruff <[email protected]>
1 parent 394c64c commit ce59419

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

protocol_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,131 @@ func TestCopyFromWrappedConnectionToWrappedConnection(t *testing.T) {
798798
}
799799
}
800800

801+
func benchmarkTCPProxy(size int, b *testing.B) {
802+
//create and start the echo backend
803+
backend, err := net.Listen("tcp", "127.0.0.1:0")
804+
if err != nil {
805+
b.Fatalf("err: %v", err)
806+
}
807+
defer backend.Close()
808+
go func() {
809+
for {
810+
conn, err := backend.Accept()
811+
if err != nil {
812+
break
813+
}
814+
_, err = io.Copy(conn, conn)
815+
conn.Close()
816+
if err != nil {
817+
b.Fatalf("Failed to read entire payload: %v", err)
818+
}
819+
}
820+
}()
821+
822+
//start the proxyprotocol enabled tcp proxy
823+
l, err := net.Listen("tcp", "127.0.0.1:0")
824+
if err != nil {
825+
b.Fatalf("err: %v", err)
826+
}
827+
defer l.Close()
828+
pl := &Listener{Listener: l}
829+
go func() {
830+
for {
831+
conn, err := pl.Accept()
832+
if err != nil {
833+
break
834+
}
835+
bConn, err := net.Dial("tcp", backend.Addr().String())
836+
if err != nil {
837+
b.Fatalf("failed to dial backend: %v", err)
838+
}
839+
go func() {
840+
_, err = io.Copy(bConn, conn)
841+
if err != nil {
842+
b.Fatalf("Failed to proxy incoming data to backend: %v", err)
843+
}
844+
bConn.(*net.TCPConn).CloseWrite()
845+
}()
846+
_, err = io.Copy(conn, bConn)
847+
if err != nil {
848+
b.Fatalf("Failed to proxy data from backend: %v", err)
849+
}
850+
conn.Close()
851+
bConn.Close()
852+
}
853+
}()
854+
855+
data := make([]byte, size)
856+
857+
header := &Header{
858+
Version: 2,
859+
Command: PROXY,
860+
TransportProtocol: TCPv4,
861+
SourceAddr: &net.TCPAddr{
862+
IP: net.ParseIP("10.1.1.1"),
863+
Port: 1000,
864+
},
865+
DestinationAddr: &net.TCPAddr{
866+
IP: net.ParseIP("20.2.2.2"),
867+
Port: 2000,
868+
},
869+
}
870+
871+
//now for the actual benchmark
872+
b.ResetTimer()
873+
for n := 0; n < b.N; n++ {
874+
conn, err := net.Dial("tcp", pl.Addr().String())
875+
if err != nil {
876+
b.Fatalf("err: %v", err)
877+
}
878+
// Write out the header!
879+
header.WriteTo(conn)
880+
//send data
881+
go func() {
882+
_, err = conn.Write(data)
883+
if err != nil {
884+
b.Fatalf("Failed to write data: %v", err)
885+
}
886+
conn.(*net.TCPConn).CloseWrite()
887+
888+
}()
889+
//receive data
890+
n, err := io.Copy(ioutil.Discard, conn)
891+
if n != int64(len(data)) {
892+
b.Fatalf("Expected to receive %d bytes, got %d", len(data), n)
893+
}
894+
if err != nil {
895+
b.Fatalf("Failed to read data: %v", err)
896+
}
897+
conn.Close()
898+
}
899+
}
900+
901+
func BenchmarkTCPProxy16KB(b *testing.B) {
902+
benchmarkTCPProxy(16*1024, b)
903+
}
904+
func BenchmarkTCPProxy32KB(b *testing.B) {
905+
benchmarkTCPProxy(32*1024, b)
906+
}
907+
func BenchmarkTCPProxy64KB(b *testing.B) {
908+
benchmarkTCPProxy(64*1024, b)
909+
}
910+
func BenchmarkTCPProxy128KB(b *testing.B) {
911+
benchmarkTCPProxy(128*1024, b)
912+
}
913+
func BenchmarkTCPProxy256KB(b *testing.B) {
914+
benchmarkTCPProxy(256*1024, b)
915+
}
916+
func BenchmarkTCPProxy512KB(b *testing.B) {
917+
benchmarkTCPProxy(512*1024, b)
918+
}
919+
func BenchmarkTCPProxy1024KB(b *testing.B) {
920+
benchmarkTCPProxy(1024*1024, b)
921+
}
922+
func BenchmarkTCPProxy2048KB(b *testing.B) {
923+
benchmarkTCPProxy(2048*1024, b)
924+
}
925+
801926
// copied from src/net/http/internal/testcert.go
802927

803928
// Copyright 2015 The Go Authors. All rights reserved.

0 commit comments

Comments
 (0)