yggdrasil-go/misc/tests/bandwidth.go

54 lines
953 B
Go
Raw Normal View History

2017-12-29 04:16:20 +00:00
package main
import "fmt"
import "net"
import "time"
2018-01-04 22:37:51 +00:00
func main() {
addr, err := net.ResolveTCPAddr("tcp", "[::1]:9001")
if err != nil {
panic(err)
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
defer listener.Close()
2017-12-29 04:16:20 +00:00
2018-01-04 22:37:51 +00:00
packetSize := 65535
numPackets := 65535
2017-12-29 04:16:20 +00:00
2018-01-04 22:37:51 +00:00
go func() {
send, err := net.DialTCP("tcp", nil, addr)
if err != nil {
panic(err)
}
defer send.Close()
msg := make([]byte, packetSize)
for idx := 0; idx < numPackets; idx++ {
send.Write(msg)
}
}()
2017-12-29 04:16:20 +00:00
2018-01-04 22:37:51 +00:00
start := time.Now()
//msg := make([]byte, 1280)
sock, err := listener.AcceptTCP()
if err != nil {
panic(err)
}
defer sock.Close()
read := 0
buf := make([]byte, packetSize)
for {
n, err := sock.Read(buf)
read += n
if err != nil {
break
}
}
timed := time.Since(start)
2017-12-29 04:16:20 +00:00
2018-01-04 22:37:51 +00:00
fmt.Printf("%f packets per second\n", float64(numPackets)/timed.Seconds())
fmt.Printf("%f bits/sec\n", 8*float64(read)/timed.Seconds())
2017-12-29 04:16:20 +00:00
}