Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update udp testcase and add udp throughout test #50

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/nknorg/nkngomobile"
"github.com/nknorg/tuna"
tpb "github.com/nknorg/tuna/pb"
"github.com/nknorg/tuna/udp"
gocache "github.com/patrickmn/go-cache"
"google.golang.org/protobuf/proto"
)
Expand All @@ -48,7 +47,7 @@ type TunaSessionClient struct {
onConnect chan struct{}
onClose chan struct{}
connectedOnce sync.Once
udpConn *udp.EncryptUDPConn
udpConn *tuna.EncryptUDPConn

sync.RWMutex
listeners []net.Listener
Expand Down Expand Up @@ -849,7 +848,7 @@ func (c *TunaSessionClient) startExits() error {
return nil
}

func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*udp.EncryptUDPConn, error) {
func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*tuna.EncryptUDPConn, error) {
acceptAddrs, err := getAcceptAddrs(addrsRe)
if err != nil {
return nil, err
Expand Down Expand Up @@ -887,15 +886,15 @@ func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*udp.En
return nil, err
}

c.udpConn = udp.NewEncryptUDPConn(conn)
c.udpConn = tuna.NewEncryptUDPConn(conn)
return c.udpConn, nil
}

func (c *TunaSessionClient) DialUDP(remoteAddr string) (*udp.EncryptUDPConn, error) {
func (c *TunaSessionClient) DialUDP(remoteAddr string) (*tuna.EncryptUDPConn, error) {
return c.DialUDPWithConfig(remoteAddr, nil)
}

func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.DialConfig) (*udp.EncryptUDPConn, error) {
func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.DialConfig) (*tuna.EncryptUDPConn, error) {
config, err := nkn.MergeDialConfig(c.config.SessionConfig, config)
if err != nil {
return nil, err
Expand All @@ -919,7 +918,7 @@ func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.Dia
return nil, err
}

udpConn := new(udp.EncryptUDPConn)
udpConn := new(tuna.EncryptUDPConn)
for i, addr := range pubAddrs.Addrs {
if len(addr.IP) > 0 && addr.Port > 0 {
udpAddr := net.UDPAddr{IP: net.ParseIP(addr.IP), Port: int(addr.Port)}
Expand All @@ -931,7 +930,7 @@ func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.Dia
log.Printf("dial udp err: %v", err)
continue
}
udpConn = udp.NewEncryptUDPConn(conn)
udpConn = tuna.NewEncryptUDPConn(conn)
break
}
}
Expand Down
159 changes: 124 additions & 35 deletions examples/throughput/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package main

import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"github.com/nknorg/tuna"
"log"
"math"
"net"
"os"
"strings"
"time"

ncp "github.com/nknorg/ncp-go"
nkn "github.com/nknorg/nkn-sdk-go"
"github.com/nknorg/ncp-go"
"github.com/nknorg/nkn-sdk-go"
ts "github.com/nknorg/nkn-tuna-session"
"github.com/nknorg/tuna/geo"
)
Expand All @@ -23,6 +25,9 @@ const (
listenID = "bob"
)

var udpBytesReceived = 0
var udpBytesSend = 0

func read(sess net.Conn) error {
timeStart := time.Now()

Expand Down Expand Up @@ -95,6 +100,64 @@ func write(sess net.Conn, numBytes int) error {
return nil
}

func readUDP(conn *tuna.EncryptUDPConn, numBytes int) error {
defer conn.Close()
buffer := make([]byte, 1024)
var timeStart time.Time
for {
err := conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
return err
}
n, _, err := conn.ReadFromUDP(buffer)
yilunzhang marked this conversation as resolved.
Show resolved Hide resolved
yilunzhang marked this conversation as resolved.
Show resolved Hide resolved
if udpBytesReceived == 0 {
timeStart = time.Now()
}
if err != nil {
return err
}
udpBytesReceived += n
if ((udpBytesReceived - n) * 10 / numBytes) != (udpBytesReceived * 10 / numBytes) {
mbTobytes := math.Pow(2, 20)
sent := float64(udpBytesSend) / mbTobytes
received := float64(udpBytesReceived) / mbTobytes
timeEnd := time.Now()
elapsed := timeEnd.Sub(timeStart)
speed := received / elapsed.Seconds()
log.Printf("UDP: Received %.2f MB bytes, speed: %.2f MB/s, package loss: %.2f%% \n", received, speed, 100*(1-received/sent))
}
}
}

func writeUDP(conn *tuna.EncryptUDPConn, numBytes int) error {
defer conn.Close()
buffer := make([]byte, 1024)
timeStart := time.Now()
for {
rand.Read(buffer)
n, _, err := conn.WriteMsgUDP(buffer, nil, nil)
if err != nil {
return err
}
time.Sleep(100 * time.Nanosecond)
udpBytesSend += n
if udpBytesSend > numBytes {
break
}
if ((udpBytesSend - n) * 10 / numBytes) != (udpBytesSend * 10 / numBytes) {
mbTobytes := math.Pow(2, 20)
sent := float64(udpBytesSend) / mbTobytes
received := float64(udpBytesReceived) / mbTobytes
timeEnd := time.Now()
elapsed := timeEnd.Sub(timeStart)
speed := sent / elapsed.Seconds()
log.Printf("UDP: Sent %.2f MB bytes, speed: %.2f MB/s, package loss: %.2f%% \n", sent, speed, 100*(1-received/sent))
}
}
yilunzhang marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func main() {
numTunaListeners := flag.Int("n", 1, "number of tuna listeners")
numBytes := flag.Int("m", 1, "data to send (MB)")
Expand All @@ -109,6 +172,7 @@ func main() {
tunaMeasureBandwidth := flag.Bool("tmb", false, "tuna measure bandwidth")
tunaMaxPrice := flag.String("price", "0.01", "tuna reverse service max price in unit of NKN/MB")
mtu := flag.Int("mtu", 0, "ncp session mtu")
u := flag.Bool("u", false, "send data through UDP instead TCP")

flag.Parse()

Expand Down Expand Up @@ -162,32 +226,43 @@ func main() {
log.Fatal(err)
}

err = c.Listen(nil)
if err != nil {
log.Fatal(err)
}

<-c.OnConnect()

log.Println("Listening at", c.Addr())

go func() {
for {
s, err := c.Accept()
if *u {
uConn, err := c.ListenUDP(nil)
if err != nil {
log.Fatal(err)
}
log.Println("Listening UDP at", c.Addr())
go func() {
err = readUDP(uConn, *numBytes)
if err != nil {
log.Fatal(err)
}
log.Println(c.Addr(), "accepted a session")

go func(s net.Conn) {
err := read(s)
}()
} else {
err = c.Listen(nil)
if err != nil {
log.Fatal(err)
}
log.Println("Listening at", c.Addr())
go func() {
for {
s, err := c.Accept()
if err != nil {
log.Fatal(err)
}
s.Close()
}(s)
}
}()
log.Println(c.Addr(), "accepted a session")

go func(s net.Conn) {
err := read(s)
if err != nil {
log.Fatal(err)
}
s.Close()
}(s)
}
}()
}
<-c.OnConnect()
}

if *dial {
Expand All @@ -208,24 +283,38 @@ func main() {
*dialAddr = listenID + "." + strings.SplitN(c.Addr().String(), ".", 2)[1]
}

s, err := c.DialWithConfig(*dialAddr, dialConfig)
if err != nil {
log.Fatal(err)
}
log.Println(c.Addr(), "dialed a session")

go func() {
err := write(s, *numBytes)
if *u {
udpConn, err := c.DialUDPWithConfig(*dialAddr, dialConfig)
if err != nil {
log.Fatal(err)
}
for {
if s.IsClosed() {
os.Exit(0)
log.Println(c.Addr(), "dialed a UDP session")
go func() {
err := writeUDP(udpConn, *numBytes)
if err != nil {
log.Fatal(err)
}
time.Sleep(time.Millisecond * 100)
}()
} else {
s, err := c.DialWithConfig(*dialAddr, dialConfig)
if err != nil {
log.Fatal(err)
}
}()
log.Println(c.Addr(), "dialed a session")

go func() {
err := write(s, *numBytes)
if err != nil {
log.Fatal(err)
}
for {
if s.IsClosed() {
os.Exit(0)
}
time.Sleep(time.Millisecond * 100)
}
}()
}
}

select {}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/nknorg/nkn-sdk-go v1.4.3
github.com/nknorg/nkn/v2 v2.1.8
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
github.com/nknorg/tuna v0.0.0-20230224012633-b53aa9c90cd5
github.com/nknorg/tuna v0.0.0-20230307074911-ced36707f273
github.com/patrickmn/go-cache v2.1.0+incompatible
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
google.golang.org/protobuf v1.27.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9 h1:Gr37j7Ttvcn8
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9/go.mod h1:zNY9NCyBcJCCDrXhwOjKarkW5cngPs/Z82xVNy/wvEA=
github.com/nknorg/nnet v0.0.0-20220217113219-4d27780990b1/go.mod h1:4DHEQEMhlRGIKGSyhATdjeusdqaHafDatadtpeHBpvI=
github.com/nknorg/portmapper v0.0.0-20200114081049-1c03cdccc283/go.mod h1:dL4PQJ4670oTO6LqvkjrBQEkD+iMiOYjlKRBBw55Csg=
github.com/nknorg/tuna v0.0.0-20230224012633-b53aa9c90cd5 h1:b6uQxUCJ3Xtq+YO8asJYzGKdP1ZvtCB7qKqt178blC0=
github.com/nknorg/tuna v0.0.0-20230224012633-b53aa9c90cd5/go.mod h1:HtzD/h1ARuyCKoM9d28HWHsel0M4e5UNuk4j9928i9s=
github.com/nknorg/tuna v0.0.0-20230307074911-ced36707f273 h1:pE6Tq5iq/Il5ZEYu/HZ9mNtHYqKLYS2FmyZfP0LkMxs=
github.com/nknorg/tuna v0.0.0-20230307074911-ced36707f273/go.mod h1:HtzD/h1ARuyCKoM9d28HWHsel0M4e5UNuk4j9928i9s=
github.com/nrdcg/auroradns v1.0.1/go.mod h1:y4pc0i9QXYlFCWrhWrUSIETnZgrf4KuwjDIWmmXo3JI=
github.com/nrdcg/dnspod-go v0.4.0/go.mod h1:vZSoFSFeQVm2gWLMkyX61LZ8HI3BaqtHZWgPTGKr6KQ=
github.com/nrdcg/goinwx v0.7.0/go.mod h1:4tKJOCi/1lTxuw9/yB2Ez0aojwtUCSkckjc22eALpqE=
Expand Down
Loading