20
20
package main
21
21
22
22
import (
23
+ "encoding/binary"
23
24
"flag"
24
25
"fmt"
25
26
"os"
@@ -31,6 +32,8 @@ import (
31
32
"github.com/netsec-ethz/scion-apps/pkg/appnet"
32
33
)
33
34
35
+ const pattern = "cbrtester" // allow easy identification of packets
36
+
34
37
func main () {
35
38
port := flag .Uint ("port" , 0 , "[Server] Local port to listen on" )
36
39
timeout := flag .Uint ("timeout" , 100 , "[Server] Size of gap between subsequent packets that is considered a freeze (in ms)" )
@@ -92,11 +95,22 @@ func runClient(address string, desiredThroughput int, payloadSize int, interacti
92
95
fmt .Printf ("Sending %v packets per second with a gap of %v\n " , numberOfPacketsPerSecond , interval )
93
96
94
97
buffer := make ([]byte , payloadSize )
95
- copy (buffer , []byte ("cbrtester" )) // allow easy identification of packets
98
+ copy (buffer , []byte (pattern ))
99
+
100
+ var writeFcn func ([]byte , time.Time ) (int , error )
101
+ writeFcn = func (buff []byte , _ time.Time ) (int , error ) {
102
+ return conn .Write (buff )
103
+ }
104
+ if payloadSize >= len (pattern )+ 8 {
105
+ writeFcn = func (buff []byte , now time.Time ) (int , error ) {
106
+ binary .LittleEndian .PutUint64 (buff [len (pattern ):], uint64 (now .UnixNano ()))
107
+ return conn .Write (buff )
108
+ }
109
+ }
96
110
97
111
for {
98
112
before := time .Now ()
99
- _ , err = conn . Write (buffer )
113
+ _ , err = writeFcn (buffer , before )
100
114
if err != nil {
101
115
fmt .Println ("error writing" , err )
102
116
}
@@ -126,6 +140,8 @@ func runServer(port int, timeout int64) error {
126
140
started := false
127
141
totalByteCount := 0
128
142
instaByteCount := 0
143
+ instaPacketCount := 0
144
+ var instaLatency time.Duration
129
145
lastBWReport := time .Now ()
130
146
for {
131
147
count , _ , err := listener .ReadFrom (buffer )
@@ -134,23 +150,38 @@ func runServer(port int, timeout int64) error {
134
150
continue
135
151
}
136
152
153
+ instaPacketCount ++
137
154
totalByteCount += count
138
155
instaByteCount += count
139
156
157
+ if count >= len (pattern )+ 8 { // we are using timestamps
158
+ nanos := binary .LittleEndian .Uint64 (buffer [len (pattern ):])
159
+ sentTime := time .Unix (0 , int64 (nanos ))
160
+ instaLatency += time .Since (sentTime )
161
+ }
162
+
140
163
if ! started {
141
164
start = time .Now ()
142
165
started = true
143
166
}
144
167
145
168
elapsed := time .Since (lastReceived ).Milliseconds ()
146
169
if elapsed > timeout {
147
- fmt .Printf ("Freeze: %.3f s\n " , float64 (elapsed )/ 1000.0 )
170
+ fmt .Printf ("Freeze: %.3f s (at %v) \n " , float64 (elapsed )/ 1000.0 , time . Now (). Format ( time . StampMilli ) )
148
171
}
149
172
150
173
if time .Since (lastBWReport ) > 5 * time .Second {
151
- fmt .Printf ("ave. bandwidth: %.3f kbps, insta. bandwidth: %.3f\n " ,
152
- float64 (totalByteCount )* 8. / 1024. / time .Since (start ).Seconds (),
153
- float64 (instaByteCount )* 8. / 1024. / float64 (time .Since (lastBWReport ).Seconds ()))
174
+ format := "ave. bandwidth: %.3f kbps, insta. bandwidth: %.3f kbps"
175
+ params := []interface {}{float64 (totalByteCount ) * 8. / 1024. / time .Since (start ).Seconds (),
176
+ float64 (instaByteCount ) * 8. / 1024. / float64 (time .Since (lastBWReport ).Seconds ())}
177
+ if count >= len (pattern )+ 8 {
178
+ format += ", # packets: %d, ave. latency: %.3f ms"
179
+ params = append (params ,
180
+ instaPacketCount , float64 (instaLatency .Milliseconds ())/ float64 (instaPacketCount ))
181
+ instaPacketCount = 0
182
+ instaLatency = 0
183
+ }
184
+ fmt .Printf (format + "\n " , params ... )
154
185
155
186
lastBWReport = time .Now ()
156
187
instaByteCount = 0
0 commit comments