Skip to content

Commit 2ead49b

Browse files
authored
print the latency. (#194)
* print the latency. Print the difference between the receiver's and the sender's clock. * fix format string * print timestamp on freezes
1 parent e578faf commit 2ead49b

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

cbrtester/cbrtester.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package main
2121

2222
import (
23+
"encoding/binary"
2324
"flag"
2425
"fmt"
2526
"os"
@@ -31,6 +32,8 @@ import (
3132
"github.com/netsec-ethz/scion-apps/pkg/appnet"
3233
)
3334

35+
const pattern = "cbrtester" // allow easy identification of packets
36+
3437
func main() {
3538
port := flag.Uint("port", 0, "[Server] Local port to listen on")
3639
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
9295
fmt.Printf("Sending %v packets per second with a gap of %v\n", numberOfPacketsPerSecond, interval)
9396

9497
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+
}
96110

97111
for {
98112
before := time.Now()
99-
_, err = conn.Write(buffer)
113+
_, err = writeFcn(buffer, before)
100114
if err != nil {
101115
fmt.Println("error writing", err)
102116
}
@@ -126,6 +140,8 @@ func runServer(port int, timeout int64) error {
126140
started := false
127141
totalByteCount := 0
128142
instaByteCount := 0
143+
instaPacketCount := 0
144+
var instaLatency time.Duration
129145
lastBWReport := time.Now()
130146
for {
131147
count, _, err := listener.ReadFrom(buffer)
@@ -134,23 +150,38 @@ func runServer(port int, timeout int64) error {
134150
continue
135151
}
136152

153+
instaPacketCount++
137154
totalByteCount += count
138155
instaByteCount += count
139156

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+
140163
if !started {
141164
start = time.Now()
142165
started = true
143166
}
144167

145168
elapsed := time.Since(lastReceived).Milliseconds()
146169
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))
148171
}
149172

150173
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...)
154185

155186
lastBWReport = time.Now()
156187
instaByteCount = 0

0 commit comments

Comments
 (0)