Skip to content

Commit

Permalink
Port changes to all go-libp2p versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Oct 19, 2023
1 parent 26820e7 commit 6b05b73
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 9 deletions.
10 changes: 8 additions & 2 deletions perf/impl/go-libp2p/v0.27/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func main() {
}

jsonB, err := json.Marshal(Result{
Latency: time.Since(start).Seconds(),
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 @@ -99,7 +102,10 @@ func main() {
}

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

type simpleReader struct {
Expand Down
57 changes: 56 additions & 1 deletion perf/impl/go-libp2p/v0.27/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"time"

logging "github.com/ipfs/go-log/v2"
pool "github.com/libp2p/go-buffer-pool"
Expand Down Expand Up @@ -89,7 +91,26 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
buf := pool.Get(blockSize)
defer pool.Put(buf)

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

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

lastReportTime = now
lastReportWrite = 0
}

toSend := buf
if bytesToSend < blockSize {
toSend = buf[:bytesToSend]
Expand All @@ -100,15 +121,49 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
return err
}
bytesToSend -= uint64(n)
lastReportWrite += uint64(n)
}
return nil
}

func drainStream(s io.Reader) (uint64, error) {
var recvd int64
recvd, err := io.Copy(io.Discard, s)
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
}

var _ io.Reader = &reportingReader{}

func (r *reportingReader) Read(b []byte) (int, error) {
n, err := r.orig.Read(b)
r.lastReportRead += uint64(n)

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

jsonB, err := json.Marshal(result)
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
}
fmt.Println(string(jsonB))

r.LastReportTime = now
r.lastReportRead = 0
}

return n, err
}
10 changes: 8 additions & 2 deletions perf/impl/go-libp2p/v0.28/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func main() {
}

jsonB, err := json.Marshal(Result{
Latency: time.Since(start).Seconds(),
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 @@ -99,7 +102,10 @@ func main() {
}

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

type simpleReader struct {
Expand Down
57 changes: 56 additions & 1 deletion perf/impl/go-libp2p/v0.28/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"time"

logging "github.com/ipfs/go-log/v2"
pool "github.com/libp2p/go-buffer-pool"
Expand Down Expand Up @@ -89,7 +91,26 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
buf := pool.Get(blockSize)
defer pool.Put(buf)

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

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

lastReportTime = now
lastReportWrite = 0
}

toSend := buf
if bytesToSend < blockSize {
toSend = buf[:bytesToSend]
Expand All @@ -100,15 +121,49 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
return err
}
bytesToSend -= uint64(n)
lastReportWrite += uint64(n)
}
return nil
}

func drainStream(s io.Reader) (uint64, error) {
var recvd int64
recvd, err := io.Copy(io.Discard, s)
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
}

var _ io.Reader = &reportingReader{}

func (r *reportingReader) Read(b []byte) (int, error) {
n, err := r.orig.Read(b)
r.lastReportRead += uint64(n)

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

jsonB, err := json.Marshal(result)
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
}
fmt.Println(string(jsonB))

r.LastReportTime = now
r.lastReportRead = 0
}

return n, err
}
10 changes: 8 additions & 2 deletions perf/impl/go-libp2p/v0.31/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func main() {
}

jsonB, err := json.Marshal(Result{
Latency: time.Since(start).Seconds(),
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 @@ -99,7 +102,10 @@ func main() {
}

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

type simpleReader struct {
Expand Down
57 changes: 56 additions & 1 deletion perf/impl/go-libp2p/v0.31/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"time"

logging "github.com/ipfs/go-log/v2"
pool "github.com/libp2p/go-buffer-pool"
Expand Down Expand Up @@ -89,7 +91,26 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
buf := pool.Get(blockSize)
defer pool.Put(buf)

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

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

lastReportTime = now
lastReportWrite = 0
}

toSend := buf
if bytesToSend < blockSize {
toSend = buf[:bytesToSend]
Expand All @@ -100,15 +121,49 @@ func sendBytes(s io.Writer, bytesToSend uint64) error {
return err
}
bytesToSend -= uint64(n)
lastReportWrite += uint64(n)
}
return nil
}

func drainStream(s io.Reader) (uint64, error) {
var recvd int64
recvd, err := io.Copy(io.Discard, s)
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
}

var _ io.Reader = &reportingReader{}

func (r *reportingReader) Read(b []byte) (int, error) {
n, err := r.orig.Read(b)
r.lastReportRead += uint64(n)

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

jsonB, err := json.Marshal(result)
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
}
fmt.Println(string(jsonB))

r.LastReportTime = now
r.lastReportRead = 0
}

return n, err
}
15 changes: 15 additions & 0 deletions perf/runner/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@ export const versions: Array<Version> = [
implementation: "https",
transportStacks: ["tcp"]
},
{
id: "v0.27",
implementation: "go-libp2p",
transportStacks: ["tcp", "quic-v1"]
},
{
id: "v0.28",
implementation: "go-libp2p",
transportStacks: ["tcp", "quic-v1"]
},
{
id: "v0.29",
implementation: "go-libp2p",
transportStacks: ["tcp", "quic-v1"]
},
{
id: "v0.31",
implementation: "go-libp2p",
transportStacks: ["tcp", "quic-v1"]
},
// {
// id: "v0.46",
// implementation: "js-libp2p",
Expand Down

0 comments on commit 6b05b73

Please sign in to comment.