| 1 | package mainscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/ |
| 2 | |
| 3 | // This implements a echo server over a SOCK_FGRAM connection. The |
| 4 | // client sends random data and random amount of data to a server |
| 5 | // which echos it back. Unlike streamEcho no checks are performed on |
| 6 | // the data and we stop receiving in the client a short while after it |
| 7 | // sent all the data. This means that there may still be some data in |
| 8 | // flight. |
| 9 | |
| 10 | import ( |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/" |
| 12 | "math/randscip-go gomod github.com/golang/go/src go1.17 `math/rand`/" |
| 13 | "timescip-go gomod github.com/golang/go/src go1.17 time/" |
| 14 | ) |
| 15 | |
| 16 | type dgramEchoscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho# struct{} |
| 17 | |
| 18 | func newDgramEchoTestscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/newDgramEchoTest().() dgramEchoscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho# { |
| 19 | return dgramEchoscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho#{} |
| 20 | } |
| 21 | |
| 22 | func (t dgramEchoscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho#) Serverscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho#Server().(s Sockscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/Sock#) { |
| 23 | pc := s.ListenPacketscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/Sock#ListenPacket.() |
| 24 | for { |
| 25 | buf := make([]byte, 4096) |
| 26 | n, addr, err := pc.ReadFromscip-go gomod github.com/golang/go/src go1.17 net/PacketConn#ReadFrom().(buf) |
| 27 | if err != nil { |
| 28 | prErrorscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prError().("ReadFrom: %s\n", err) |
| 29 | break |
| 30 | } |
| 31 | go pc.WriteToscip-go gomod github.com/golang/go/src go1.17 net/PacketConn#WriteTo().(buf[:n], addr) |
| 32 | } |
| 33 | prDebugscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prDebug().("Closing\n") |
| 34 | err := pc.Closescip-go gomod github.com/golang/go/src go1.17 net/PacketConn#Close().() |
| 35 | if err != nil { |
| 36 | prErrorscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prError().("Close(): %s\n", err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (t dgramEchoscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho#) Clientscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/dgramEcho#Client().(s Sockscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/Sock#, conid int) { |
| 41 | c, err := s.Dialscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/Sock#Dial.(conid) |
| 42 | if err != nil { |
| 43 | prErrorscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prError().("[%05d] Failed to Dial: %s %s\n", conid, s, err) |
| 44 | return |
| 45 | } |
| 46 | defer c.Closescip-go gomod github.com/golang/go/src go1.17 net/Conn#Close().() |
| 47 | |
| 48 | // Hardcode the range of what we send with a single Write |
| 49 | minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. = 1 |
| 50 | maxBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxBufLen. = 8192 |
| 51 | |
| 52 | // Create buffer with random data and random length. |
| 53 | // Make sure the buffer is not zero-length |
| 54 | buflen := minDataLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minDataLen. |
| 55 | if maxDataLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxDataLen. > minDataLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minDataLen. { |
| 56 | buflen += randscip-go gomod github.com/golang/go/src go1.17 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.17 `math/rand`/Intn().(maxDataLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxDataLen. - minDataLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minDataLen. + 1) |
| 57 | } |
| 58 | |
| 59 | start := timescip-go gomod github.com/golang/go/src go1.17 time/.Nowscip-go gomod github.com/golang/go/src go1.17 time/Now().() |
| 60 | |
| 61 | // The receiver just slurps data |
| 62 | w := make(chan int) |
| 63 | go func() { |
| 64 | total := 0 |
| 65 | Loop: |
| 66 | for { |
| 67 | batch := 0 |
| 68 | bufsize := minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. |
| 69 | if maxBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxBufLen. > minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. { |
| 70 | bufsize += randscip-go gomod github.com/golang/go/src go1.17 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.17 `math/rand`/Intn().(maxBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxBufLen. - minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. + 1) |
| 71 | } |
| 72 | batch = bufsize |
| 73 | rxbuf := make([]byte, batch) |
| 74 | |
| 75 | e := make(chan error, 0) |
| 76 | go func() { |
| 77 | l, err := c.Readscip-go gomod github.com/golang/go/src go1.17 net/Conn#Read().(rxbuf) |
| 78 | if err != nil { |
| 79 | e <- err |
| 80 | } else { |
| 81 | total += l |
| 82 | e <- nil |
| 83 | } |
| 84 | }() |
| 85 | select { |
| 86 | case err := <-e: |
| 87 | if err != nil { |
| 88 | prDebugscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prDebug().("[%05d] Failed to receive: %s\n", conid, err) |
| 89 | break Loop |
| 90 | } |
| 91 | case <-timescip-go gomod github.com/golang/go/src go1.17 time/.Afterscip-go gomod github.com/golang/go/src go1.17 time/After().(ioTimeoutscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/ioTimeout.): |
| 92 | prErrorscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prError().("[%05d] Receive timed out\n", conid) |
| 93 | break Loop |
| 94 | } |
| 95 | } |
| 96 | w <- total |
| 97 | }() |
| 98 | |
| 99 | remaining := buflen |
| 100 | totalSent := 0 |
| 101 | for remaining > 0 { |
| 102 | batch := 0 |
| 103 | bufsize := minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. |
| 104 | if maxBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxBufLen. > minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. { |
| 105 | bufsize += randscip-go gomod github.com/golang/go/src go1.17 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.17 `math/rand`/Intn().(maxBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/maxBufLen. - minBufLenscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/minBufLen. + 1) |
| 106 | } |
| 107 | if remaining > bufsize { |
| 108 | batch = bufsize |
| 109 | } else { |
| 110 | batch = remaining |
| 111 | } |
| 112 | |
| 113 | txbuf := randBufscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/randBuf().(batch) |
| 114 | |
| 115 | e := make(chan error, 0) |
| 116 | go func() { |
| 117 | l, err := c.Writescip-go gomod github.com/golang/go/src go1.17 net/Conn#Write().(txbuf) |
| 118 | if err != nil { |
| 119 | e <- err |
| 120 | } else if l != batch { |
| 121 | e <- fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.17 fmt/Errorf().("Sent incorrect length: expected %d, got %d", batch, l) |
| 122 | } else { |
| 123 | e <- nil |
| 124 | } |
| 125 | }() |
| 126 | select { |
| 127 | case err := <-e: |
| 128 | if err != nil { |
| 129 | prErrorscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prError().("[%05d] Failed to send: %s\n", conid, err) |
| 130 | break |
| 131 | } |
| 132 | case <-timescip-go gomod github.com/golang/go/src go1.17 time/.Afterscip-go gomod github.com/golang/go/src go1.17 time/After().(ioTimeoutscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/ioTimeout.): |
| 133 | prErrorscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prError().("[%05d] Send timed out\n", conid) |
| 134 | break |
| 135 | } |
| 136 | |
| 137 | totalSent += batch |
| 138 | remaining -= batch |
| 139 | } |
| 140 | // wait for a little while to drain some of the receive |
| 141 | timescip-go gomod github.com/golang/go/src go1.17 time/.Sleepscip-go gomod github.com/golang/go/src go1.17 time/Sleep().(timescip-go gomod github.com/golang/go/src go1.17 time/.Secondscip-go gomod github.com/golang/go/src go1.17 time/Second. / 10) |
| 142 | c.Closescip-go gomod github.com/golang/go/src go1.17 net/Conn#Close().() |
| 143 | totalReceived := <-w |
| 144 | txTime := timescip-go gomod github.com/golang/go/src go1.17 time/.Sincescip-go gomod github.com/golang/go/src go1.17 time/Since().(start) |
| 145 | prInfoscip-go gomod github.com/linuxkit/virtsock . `github.com/linuxkit/virtsock/cmd/sock_stress`/prInfo().("[%05d] TX=%10d RX=%10d bytes in %10.4f ms\n", conid, totalSent, totalReceived, txTime.Secondsscip-go gomod github.com/golang/go/src go1.17 time/Duration#Seconds().()*1000) |
| 146 | } |