mirror of
https://github.com/xtaci/kcp-go.git
synced 2024-04-21 12:32:15 +00:00
commit a8ae293a9ddee042c35e2501f93fa5dfa2425302 Merge:bdaf81e1a84985Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 22:48:13 2020 +0800 Merge branch 'master' into autotune commitbdaf81e88eAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 22:38:31 2020 +0800 add loc commitce66a98f95Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 19:53:42 2020 +0800 lint commitad7383d8a3Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 19:05:35 2020 +0800 fix typo commitdc0fc2364cAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:47:14 2020 +0800 adjust tests commitec45b72c39Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:45:17 2020 +0800 remove obsolete decoder in listener commit827aa70e29Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:42:42 2020 +0800 use hierarchical defensive receiving strategy commitf5ca6b0e3aAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:34:30 2020 +0800 optimize condition commitff34f7683fAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 15:18:00 2020 +0800 fix test commita08b274651Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 15:13:20 2020 +0800 solve compability issue with non FEC sender commitab7596783bAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 15:02:34 2020 +0800 implements autotune in fec decoder commit260f61b3ebAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 14:34:48 2020 +0800 hack 16-bit to identify FEC from packet commit105bfec567Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 2 21:38:47 2020 +0800 add autotune
136 lines
2.9 KiB
Go
136 lines
2.9 KiB
Go
package kcp
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/xtaci/lossyconn"
|
|
)
|
|
|
|
const repeat = 16
|
|
|
|
func TestLossyConn1(t *testing.T) {
|
|
t.Log("testing loss rate 10%, rtt 200ms")
|
|
t.Log("testing link with nodelay parameters:1 10 2 1")
|
|
client, err := lossyconn.NewLossyConn(0.1, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
server, err := lossyconn.NewLossyConn(0.1, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testlink(t, client, server, 1, 10, 2, 1)
|
|
}
|
|
|
|
func TestLossyConn2(t *testing.T) {
|
|
t.Log("testing loss rate 20%, rtt 200ms")
|
|
t.Log("testing link with nodelay parameters:1 10 2 1")
|
|
client, err := lossyconn.NewLossyConn(0.2, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
server, err := lossyconn.NewLossyConn(0.2, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testlink(t, client, server, 1, 10, 2, 1)
|
|
}
|
|
|
|
func TestLossyConn3(t *testing.T) {
|
|
t.Log("testing loss rate 30%, rtt 200ms")
|
|
t.Log("testing link with nodelay parameters:1 10 2 1")
|
|
client, err := lossyconn.NewLossyConn(0.3, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
server, err := lossyconn.NewLossyConn(0.3, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testlink(t, client, server, 1, 10, 2, 1)
|
|
}
|
|
|
|
func TestLossyConn4(t *testing.T) {
|
|
t.Log("testing loss rate 10%, rtt 200ms")
|
|
t.Log("testing link with nodelay parameters:1 10 2 0")
|
|
client, err := lossyconn.NewLossyConn(0.1, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
server, err := lossyconn.NewLossyConn(0.1, 100)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testlink(t, client, server, 1, 10, 2, 0)
|
|
}
|
|
|
|
func testlink(t *testing.T, client *lossyconn.LossyConn, server *lossyconn.LossyConn, nodelay, interval, resend, nc int) {
|
|
t.Log("testing with nodelay parameters:", nodelay, interval, resend, nc)
|
|
sess, _ := NewConn2(server.LocalAddr(), nil, 0, 0, client)
|
|
listener, _ := ServeConn(nil, 0, 0, server)
|
|
echoServer := func(l *Listener) {
|
|
for {
|
|
conn, err := l.AcceptKCP()
|
|
if err != nil {
|
|
return
|
|
}
|
|
go func() {
|
|
conn.SetNoDelay(nodelay, interval, resend, nc)
|
|
buf := make([]byte, 65536)
|
|
for {
|
|
n, err := conn.Read(buf)
|
|
if err != nil {
|
|
return
|
|
}
|
|
conn.Write(buf[:n])
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
echoTester := func(s *UDPSession, raddr net.Addr) {
|
|
s.SetNoDelay(nodelay, interval, resend, nc)
|
|
buf := make([]byte, 64)
|
|
var rtt time.Duration
|
|
for i := 0; i < repeat; i++ {
|
|
start := time.Now()
|
|
s.Write(buf)
|
|
io.ReadFull(s, buf)
|
|
rtt += time.Since(start)
|
|
}
|
|
|
|
t.Log("client:", client)
|
|
t.Log("server:", server)
|
|
t.Log("avg rtt:", rtt/repeat)
|
|
t.Logf("total time: %v for %v round trip:", rtt, repeat)
|
|
}
|
|
|
|
go echoServer(listener)
|
|
echoTester(sess, server.LocalAddr())
|
|
}
|
|
|
|
func BenchmarkFlush(b *testing.B) {
|
|
kcp := NewKCP(1, func(buf []byte, size int) {})
|
|
kcp.snd_buf = make([]segment, 1024)
|
|
for k := range kcp.snd_buf {
|
|
kcp.snd_buf[k].xmit = 1
|
|
kcp.snd_buf[k].resendts = currentMs() + 10000
|
|
}
|
|
b.ResetTimer()
|
|
b.ReportAllocs()
|
|
var mu sync.Mutex
|
|
for i := 0; i < b.N; i++ {
|
|
mu.Lock()
|
|
kcp.flush(false)
|
|
mu.Unlock()
|
|
}
|
|
}
|