Skip to content

Implement Perf Protocol for py-libp2p #1169

@ItshMoh

Description

@ItshMoh

Summary

Implement the libp2p perf protocol in py-libp2p to enable standardized performance benchmarking between libp2p implementations.

Background

The perf protocol (/perf/1.0.0) is a simple protocol for measuring transfer performance within and across libp2p implementations. It allows measuring:

  • Connection establishment time
  • Upload throughput
  • Download throughput

This protocol is already implemented in:

  • js-libp2p (Note: "Handshakes per second" benchmark from the spec is not implemented)

Proposed API

from libp2p import new_host
from libp2p.perf import Perf, PROTOCOL_ID

# Create hosts
server_host = new_host()
client_host = new_host()

# Server side
perf_server = Perf(server_host)
await perf_server.start()

# Client side - run benchmark
perf_client = Perf(client_host)
result = await perf_client.measure_performance(
    peer_id=server_peer_id,
    multiaddr=server_addr,
    send_bytes=1024 * 1024,    # 1 MB upload
    recv_bytes=1024 * 1024,    # 1 MB download
)

print(f"Time: {result.time_seconds:.3f}s")
print(f"Upload: {result.upload_bytes / result.time_seconds / 1024:.2f} KB/s")
print(f"Download: {result.download_bytes / result.time_seconds / 1024:.2f} KB/s")

Protocol Specification

Per the spec:

  1. Client opens stream with /perf/1.0.0
  2. Client sends 8-byte big-endian uint64: n (bytes to receive back)
  3. Client sends upload data
  4. Client half-closes stream (signals EOF)
  5. Server reads all upload data
  6. Server sends n bytes back
  7. Server closes stream

Related

Are you planning to do it yourself in a pull request?

yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions