github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/kcp-go/kcp_test.go (about) 1 package kcp 2 3 import ( 4 "io" 5 "net" 6 "sync" 7 "testing" 8 "time" 9 10 "github.com/xtaci/lossyconn" 11 ) 12 13 const repeat = 16 14 15 func TestLossyConn1(t *testing.T) { 16 t.Log("testing loss rate 10%, rtt 200ms") 17 t.Log("testing link with nodelay parameters:1 10 2 1") 18 client, err := lossyconn.NewLossyConn(0.1, 100) 19 if err != nil { 20 t.Fatal(err) 21 } 22 23 server, err := lossyconn.NewLossyConn(0.1, 100) 24 if err != nil { 25 t.Fatal(err) 26 } 27 testlink(t, client, server, 1, 10, 2, 1) 28 } 29 30 func TestLossyConn2(t *testing.T) { 31 t.Log("testing loss rate 20%, rtt 200ms") 32 t.Log("testing link with nodelay parameters:1 10 2 1") 33 client, err := lossyconn.NewLossyConn(0.2, 100) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 server, err := lossyconn.NewLossyConn(0.2, 100) 39 if err != nil { 40 t.Fatal(err) 41 } 42 testlink(t, client, server, 1, 10, 2, 1) 43 } 44 45 func TestLossyConn3(t *testing.T) { 46 t.Log("testing loss rate 30%, rtt 200ms") 47 t.Log("testing link with nodelay parameters:1 10 2 1") 48 client, err := lossyconn.NewLossyConn(0.3, 100) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 server, err := lossyconn.NewLossyConn(0.3, 100) 54 if err != nil { 55 t.Fatal(err) 56 } 57 testlink(t, client, server, 1, 10, 2, 1) 58 } 59 60 func TestLossyConn4(t *testing.T) { 61 t.Log("testing loss rate 10%, rtt 200ms") 62 t.Log("testing link with nodelay parameters:1 10 2 0") 63 client, err := lossyconn.NewLossyConn(0.1, 100) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 server, err := lossyconn.NewLossyConn(0.1, 100) 69 if err != nil { 70 t.Fatal(err) 71 } 72 testlink(t, client, server, 1, 10, 2, 0) 73 } 74 75 func testlink(t *testing.T, client *lossyconn.LossyConn, server *lossyconn.LossyConn, nodelay, interval, resend, nc int) { 76 t.Log("testing with nodelay parameters:", nodelay, interval, resend, nc) 77 sess, _ := NewConn2(server.LocalAddr(), nil, 0, 0, client) 78 listener, _ := ServeConn(nil, 0, 0, server) 79 echoServer := func(l *Listener) { 80 for { 81 conn, err := l.AcceptKCP() 82 if err != nil { 83 return 84 } 85 go func() { 86 conn.SetNoDelay(nodelay, interval, resend, nc) 87 buf := make([]byte, 65536) 88 for { 89 n, err := conn.Read(buf) 90 if err != nil { 91 return 92 } 93 conn.Write(buf[:n]) 94 } 95 }() 96 } 97 } 98 99 echoTester := func(s *UDPSession, raddr net.Addr) { 100 s.SetNoDelay(nodelay, interval, resend, nc) 101 buf := make([]byte, 64) 102 var rtt time.Duration 103 for i := 0; i < repeat; i++ { 104 start := time.Now() 105 s.Write(buf) 106 io.ReadFull(s, buf) 107 rtt += time.Now().Sub(start) 108 } 109 110 t.Log("client:", client) 111 t.Log("server:", server) 112 t.Log("avg rtt:", rtt/repeat) 113 t.Logf("total time: %v for %v round trip:", rtt, repeat) 114 } 115 116 go echoServer(listener) 117 echoTester(sess, server.LocalAddr()) 118 } 119 120 func BenchmarkFlush(b *testing.B) { 121 kcp := NewKCP(1, func(buf []byte, size int) {}) 122 kcp.snd_buf = make([]segment, 1024) 123 for k := range kcp.snd_buf { 124 kcp.snd_buf[k].xmit = 1 125 kcp.snd_buf[k].resendts = currentMs() + 10000 126 } 127 b.ResetTimer() 128 b.ReportAllocs() 129 var mu sync.Mutex 130 for i := 0; i < b.N; i++ { 131 mu.Lock() 132 kcp.flush(false) 133 mu.Unlock() 134 } 135 }