zmq.rs Documentation

repository·master·Indexed 23 days ago

https://github.com/zeromq/zmq.rs

A native Rust implementation of the ZeroMQ messaging library (version 0.6.0) that implements the ZMTP protocol. It provides high-performance asynchronous messaging patterns supporting TCP and IPC transports, and socket patterns including Request/Response (REQ, REP, DEALER, ROUTER), Publish/Subscribe (PUB, SUB, XPUB, XSUB), and Pipeline (PUSH, PULL). The library integrates with the Rust async ecosystem and supports tokio, async-std, and async-dispatcher runtimes via feature flags.

Tokens
5K
Snippets
6
Records
43
Agent score
81%

What's inside zmq.rs

  1. Overview of zmq.rs capabilities

    master

    zmq.rs is a native Rust implementation of ZeroMQ designed to leverage Rust's async ecosystem. It provides a high-performance asynchronous messaging library that implements the ZMTP protocol.

    Note: This implementation does not currently cover the full ZeroMQ feature set.

    Supported Transport Types

    • TCP
    • IPC (Unix only)

    Supported Socket Patterns

    • Request/Response: REQ, REP, DEALER, ROUTER
    • Publish/Subscribe: PUB, SUB, XPUB, XSUB
    • Pipeline: PUSH, PULL
  2. Understand the available benchmark targets

    master

    The benchmark suite includes several distinct targets, each answering different performance questions:

    • codec: Microbenchmarks for ZMTP codec encode/decode. These are pure CPU tasks and do not perform network I/O.
    • compare_libzmq: Side-by-side latency comparisons (PUB/SUB, REQ/REP, PUSH/PULL, DEALER/ROUTER) between zmq.rs and libzmq over TCP and IPC.
    • throughput: Measures sustained batch throughput for various patterns (e.g., PUB/SUB fanout). Note that pub_fanout/send_pressure numbers are send-pressure oriented and may not represent strict delivered throughput.
    • hotpath: Internal experiments focusing on message_construct, runtime, backend_primitives, and async_send_overhead to help calibrate send-only performance gaps.

    Note: The suite excludes inproc transport, security builders, and engine internals. libzmq peers run on OS threads, while zeromq peers run on a fixed 2-worker Tokio runtime.

  3. Format stable Run IDs for benchmarks

    master

    When running benchmarks, use a stable and searchable Run ID format to allow for quick human scanning. The recommended pattern is:

    <host>-<profile>-<candidate-sha>-<YYYYMMDDTHHMMSSZ>

    Examples:

    • macbook-standard-16ae7d1-20260514T190000Z
    • c7g-4xlarge-standard-16ae7d1-20260514T190000Z

    Note that the manifest.json file within the run directory already captures detailed metadata including git SHAs, tool versions, OS, CPU metadata, profile, transports, implementations, runtimes, and OMQ revision.

  4. Interpret benchmark results and throughput calculations

    master

    When reading results, it is critical to distinguish between different measurement types:

    • Sender-side hot-path: Measures local send admission only. A successful send().await does not guarantee a transport flush or delivery acknowledgement.
    • Delivered-latency: Requires a receiver to observe the message. Includes runtime scheduling, blocking peer threads, and transport behavior.
    • Throughput: Measures sustained batches. Use this to evaluate batching, writer queue design, and receive-path changes.

    Throughput Calculation Logic

    Criterion throughput is computed based on the bytes declared by the benchmark:

    • One-way send/receive: Uses msg_size.
    • Fanout delivered: Uses msg_size * subscriber_count.
    • Roundtrip DEALER/ROUTER delivered: Uses 2 * msg_size.
    • Historical one-message groups: May use msg_size even for replies; compare these by latency rather than MiB/s.
  5. Understand `async_helpers` for runtime compatibility

    master

    The zeromq crate supports both tokio and async-std runtimes. To facilitate compatibility, the crate provides async_helpers.

    While async_helpers is available to help bridge the two runtimes, most users do not need to use it. Instead, you should use the standard entry point macros for your chosen runtime:

    • For tokio: Use #[tokio::main]
    • For async-std: Use #[async_std::main]
  6. Run the zmq.rs performance suite

    master

    The performance suite is orchestrated by a Python script that runs Criterion benchmarks, normalizes results, and can compare zmq.rs against libzmq or OMQ implementations.

    Basic Smoke Profile

    Run a smoke profile comparing zmqrs and libzmq over TCP:

    python3 scripts/run_perf_suite.py --profile smoke --impl zmqrs,libzmq --transport tcp

    Comparing with OMQ

    To include OMQ in the comparison, the script will clone the pinned revision from perf-suite.json into target/perf-deps/omq.rs. You can specify a custom revision using --omq-rev <sha> or a specific toolchain using --toolchain <version>:

    python3 scripts/run_perf_suite.py --profile smoke --impl omq --transport tcp --toolchain 1.94.1

    Comprehensive Local Run

    For a full decision-making run covering multiple implementations and transports:

    python3 scripts/run_perf_suite.py --profile standard --impl zmqrs,libzmq,omq --transport tcp,ipc

    Viewing Results

    After a run completes, results (including manifest.json, results.jsonl, summary.md, and summary.html) are stored in target/perf-runs/<run-id>/. You can generate a report using:

    python3 scripts/report_perf_suite.py target/perf-runs/<run-id>
    python3 scripts/run_perf_suite.py --profile smoke --impl zmqrs,libzmq --transport tcp
  7. Post benchmark results to GitHub

    master

    When reporting performance results in GitHub discussions or Pull Request comments, include the following information to ensure reproducibility and clarity:

    1. The summary.md table.
    2. The candidate git SHA (found in manifest.json).
    3. The host CPU/OS (found in manifest.json).
    4. The archive location and its SHA256 checksum.
    5. Any non-default flags used during the run (e.g., --toolchain).

    For distributed agent runs, it is recommended to store archives outside the repository (e.g., GitHub Actions artifacts or an S3 bucket).

  8. Run ZeroMQ examples

    master

    You can run any example provided in the repository using the standard cargo run command. By default, examples use the tokio asynchronous runtime.

    To run an example with the default tokio runtime:

    cargo run --example <example_name>

    To run an example using the async-std runtime instead, you must disable the default features and explicitly enable the async-std-runtime feature:

    cargo run --example <example_name> --no-default-features --features async-std-runtime
  9. Configure the async runtime via feature flags

    master

    The library supports three different async runtimes, which are selected using Cargo feature flags. tokio is the default runtime.

    To use a different runtime, you must disable the default features and explicitly select the desired runtime feature.

    • tokio: Default runtime (enabled via tokio-runtime).
    • async-std: Enabled via async-std-runtime.
    • async-dispatcher: Enabled via async-dispatcher-runtime.
  10. Install dependencies for running benchmarks locally

    master

    To run benchmarks on your local machine, you must first install the libzmq development headers for your operating system.

    • Linux: Use apt-get to install libzmq3-dev.
    • macOS: Use brew to install zeromq.

    After installation, you can run benchmarks using cargo bench.

    # Linux
    sudo apt-get install libzmq3-dev
    
    # macOS
    brew install zeromq
    # Linux
    sudo apt-get install libzmq3-dev
    # macOS
    brew install zeromq
  11. Package and share performance benchmark runs

    master

    Performance results are stored in target/perf-runs/<run-id>/. To share these results across machines, pull requests, or issue comments, use the package_perf_run.py script to create a compressed archive.

    By default, the archive includes manifest.json, results.jsonl, summary.md, and summary.html. If you need raw Criterion directories or external benchmark outputs for debugging, use the --include-artifacts flag, but note that these archives will be significantly larger.

  12. Supported `Endpoint` variants and formats

    master

    The Endpoint enum currently supports the following transport types:

    • TCP: Requires a Host and a Port (u16). Formatted as tcp://<host>:<port>. IPv6 addresses are wrapped in brackets in the string representation, e.g., tcp://[::1]:34567.
    • IPC: Requires an optional PathBuf. Formatted as ipc://<path>.