Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created September 16, 2015 19:52
Show Gist options
  • Save whyrusleeping/9e79425de9858bdc37d8 to your computer and use it in GitHub Desktop.
Save whyrusleeping/9e79425de9858bdc37d8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
utp "github.com/anacrolix/utp"
"io"
"io/ioutil"
"time"
)
func main() {
s, err := utp.NewSocket("udp", "localhost:5555")
if err != nil {
panic(err)
}
txsize := 4096
nloops := 100000
go func() {
c, err := utp.Dial("localhost:5555")
if err != nil {
panic(err)
}
defer c.Close()
buf := make([]byte, txsize)
for i := 0; i < nloops; i++ {
n, err := c.Write(buf)
if err != nil {
panic(err)
}
if n != txsize {
panic(err)
}
}
}()
oc, err := s.Accept()
if err != nil {
panic(err)
}
before := time.Now()
n, err := io.Copy(ioutil.Discard, oc)
if err != nil {
panic(err)
}
if n != int64(txsize*nloops) {
panic("not enough bytes")
}
took := time.Now().Sub(before)
fmt.Printf("took %s\n", took)
fmt.Printf("bandwidth = %f b/s", float64(txsize*nloops)/took.Seconds())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment