Skip to content

Commit

Permalink
feat(perf): support iperf-style intermittent results
Browse files Browse the repository at this point in the history
Required for libp2p/test-plans#261.
  • Loading branch information
mxinden committed Aug 28, 2023
1 parent a5cd126 commit 97d621d
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
)

type Result struct {
Latency float64 `json:"latency"`
Type string `json:"type"`
TimeSeconds float64 `json:"timeSeconds"`
UploadBytes uint `json:"uploadBytes"`
DownloadBytes uint `json:"downloadBytes"`
}

func RunClient(addr string, uploadBytes, downloadBytes uint64) error {
Expand Down Expand Up @@ -44,7 +47,10 @@ func RunClient(addr string, uploadBytes, downloadBytes uint64) error {
log.Printf("uploaded %s: %.2fs (%s/s)", formatBytes(uploadBytes), uploadTook.Seconds(), formatBytes(bandwidth(uploadBytes, uploadTook)))
log.Printf("downloaded %s: %.2fs (%s/s)", formatBytes(downloadBytes), downloadTook.Seconds(), formatBytes(bandwidth(downloadBytes, downloadTook)))
json, err := json.Marshal(Result{
Latency: time.Since(start).Seconds(),
TimeSeconds: time.Since(start).Seconds(),
Type: "final",
UploadBytes: uint(uploadBytes),
DownloadBytes: uint(downloadBytes),
})
if err != nil {
return err
Expand All @@ -63,7 +69,28 @@ func handleClientStream(str io.ReadWriteCloser, uploadBytes, downloadBytes uint6
// upload data
b = make([]byte, 16*1024)
uploadStart := time.Now()

lastReportTime := time.Now()
lastReportWrite := 0

for uploadBytes > 0 {
// TODO: Is this expensive in go? Is there a cheaper API with less resolution?

This comment has been minimized.

Copy link
@marten-seemann

marten-seemann Sep 4, 2023

This is fine. quic-go does ~1 time.Now() call per packet (or per GSO packet batch, when using GSO), and it's not showing up as a large factor in benchmarks.

This comment has been minimized.

Copy link
@mxinden

mxinden Sep 6, 2023

Author Owner

Cool. Thank you for checking.

now := time.Now()
if now.Sub(lastReportTime) > time.Second {
jsonB, err := json.Marshal(Result{
TimeSeconds: now.Sub(lastReportTime).Seconds(),
UploadBytes: uint(lastReportWrite),
Type: "intermediary",
})
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
}
fmt.Println(string(jsonB))

lastReportTime = now
lastReportWrite = 0
}

if uploadBytes < uint64(len(b)) {
b = b[:uploadBytes]
}
Expand All @@ -72,7 +99,9 @@ func handleClientStream(str io.ReadWriteCloser, uploadBytes, downloadBytes uint6
return 0, 0, err
}
uploadBytes -= uint64(n)
lastReportWrite += n
}

if err := str.Close(); err != nil {
return 0, 0, err
}
Expand Down

0 comments on commit 97d621d

Please sign in to comment.