-
-
Save whyrusleeping/9e79425de9858bdc37d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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