Skip to content

Commit

Permalink
Use uint64 and >=
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Sep 26, 2023
1 parent dac602c commit 26820e7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
12 changes: 6 additions & 6 deletions perf/impl/go-libp2p/v0.29/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func main() {
}

jsonB, err := json.Marshal(Result{
TimeSeconds: time.Since(start).Seconds(),
UploadBytes: uint(*uploadBytes),
DownloadBytes: uint(*downloadBytes),
Type: "final",
TimeSeconds: time.Since(start).Seconds(),
UploadBytes: *uploadBytes,
DownloadBytes: *downloadBytes,
Type: "final",
})
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
Expand All @@ -104,8 +104,8 @@ func main() {
type Result struct {
Type string `json:"type"`
TimeSeconds float64 `json:"timeSeconds"`
UploadBytes uint `json:"uploadBytes"`
DownloadBytes uint `json:"downloadBytes"`
UploadBytes uint64 `json:"uploadBytes"`
DownloadBytes uint64 `json:"downloadBytes"`
}

type simpleReader struct {
Expand Down
26 changes: 13 additions & 13 deletions perf/impl/go-libp2p/v0.29/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
defer pool.Put(buf)

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

for bytesToSend > 0 {
now := time.Now()
if now.Sub(lastReportTime) > time.Second {
if now.Sub(lastReportTime) >= time.Second {
jsonB, err := json.Marshal(Result{
TimeSeconds: now.Sub(lastReportTime).Seconds(),
UploadBytes: uint(lastReportWrite),
Type: "intermediary",
UploadBytes: lastReportWrite,
Type: "intermediary",
})
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
Expand All @@ -121,24 +121,24 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
return err
}
bytesToSend -= uint64(n)
lastReportWrite += n
lastReportWrite += uint64(n)
}
return nil
}

func drainStream(s io.Reader) (uint64, error) {
var recvd int64
recvd, err := io.Copy(io.Discard, & reportingReader { orig: s, LastReportTime: time.Now() })
recvd, err := io.Copy(io.Discard, &reportingReader{orig: s, LastReportTime: time.Now()})
if err != nil && err != io.EOF {
return uint64(recvd), err
}
return uint64(recvd), nil
}

type reportingReader struct {
orig io.Reader
LastReportTime time.Time
lastReportRead uint64
orig io.Reader
LastReportTime time.Time
lastReportRead uint64
}

var _ io.Reader = &reportingReader{}
Expand All @@ -148,11 +148,11 @@ func (r *reportingReader) Read(b []byte) (int, error) {
r.lastReportRead += uint64(n)

now := time.Now()
if now.Sub(r.LastReportTime) > time.Second {
if now.Sub(r.LastReportTime) >= time.Second {
result := Result{
TimeSeconds: now.Sub(r.LastReportTime).Seconds(),
Type: "intermediary",
DownloadBytes: uint(r.lastReportRead),
TimeSeconds: now.Sub(r.LastReportTime).Seconds(),
Type: "intermediary",
DownloadBytes: r.lastReportRead,
}

jsonB, err := json.Marshal(result)
Expand Down
36 changes: 18 additions & 18 deletions perf/impl/https/v0.1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func runClient(serverAddr string, uploadBytes, downloadBytes uint64) (time.Durat
fmt.Sprintf("https://%s/", serverAddr),
io.MultiReader(
bytes.NewReader(b),
&reportingReader { orig: &nullReader{ N: uploadBytes }, LastReportTime: time.Now(), isUpload: true },
&reportingReader{orig: &nullReader{N: uploadBytes}, LastReportTime: time.Now(), isUpload: true},
),
)
if err != nil {
Expand Down Expand Up @@ -149,8 +149,8 @@ func generateEphemeralCertificate() (tls.Certificate, error) {
type Result struct {
Type string `json:"type"`
TimeSeconds float64 `json:"timeSeconds"`
UploadBytes uint `json:"uploadBytes"`
DownloadBytes uint `json:"downloadBytes"`
UploadBytes uint64 `json:"uploadBytes"`
DownloadBytes uint64 `json:"downloadBytes"`
}

func main() {
Expand Down Expand Up @@ -203,10 +203,10 @@ func main() {
}

jsonB, err := json.Marshal(Result{
TimeSeconds: latency.Seconds(),
UploadBytes: uint(*uploadBytes),
DownloadBytes: uint(*downloadBytes),
Type: "final",
TimeSeconds: latency.Seconds(),
UploadBytes: *uploadBytes,
DownloadBytes: *downloadBytes,
Type: "final",
})
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
Expand Down Expand Up @@ -235,18 +235,18 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {

func drainStream(s io.Reader) (uint64, error) {
var recvd int64
recvd, err := io.Copy(io.Discard, & reportingReader { orig: s, LastReportTime: time.Now(), isUpload: false })
recvd, err := io.Copy(io.Discard, &reportingReader{orig: s, LastReportTime: time.Now(), isUpload: false})
if err != nil && err != io.EOF {
return uint64(recvd), err
}
return uint64(recvd), nil
}

type reportingReader struct {
orig io.Reader
LastReportTime time.Time
lastReportRead uint64
isUpload bool
orig io.Reader
LastReportTime time.Time
lastReportRead uint64
isUpload bool
}

var _ io.Reader = &reportingReader{}
Expand All @@ -256,16 +256,16 @@ func (r *reportingReader) Read(b []byte) (int, error) {
r.lastReportRead += uint64(n)

now := time.Now()
if now.Sub(r.LastReportTime) > time.Second {
if now.Sub(r.LastReportTime) >= time.Second {
// This section is analogous to your Read implementation
result := Result{
TimeSeconds: now.Sub(r.LastReportTime).Seconds(),
Type: "intermediary",
Type: "intermediary",
}
if r.isUpload {
result.UploadBytes = uint(r.lastReportRead)
result.UploadBytes = r.lastReportRead
} else {
result.DownloadBytes = uint(r.lastReportRead)
result.DownloadBytes = r.lastReportRead
}

jsonB, err := json.Marshal(result)
Expand All @@ -282,8 +282,8 @@ func (r *reportingReader) Read(b []byte) (int, error) {
}

type nullReader struct {
N uint64
read uint64
N uint64
read uint64
LastReportTime time.Time
lastReportRead uint64
}
Expand Down

0 comments on commit 26820e7

Please sign in to comment.