|
| 1 | +# NATS Client |
| 2 | + |
| 3 | +A Python client for the NATS messaging system. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Support for publish/subscribe |
| 8 | +- Support for request/reply |
| 9 | +- Support for queue groups |
| 10 | +- Support for multi-value message headers |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +pip install nats-client |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```python |
| 21 | +import asyncio |
| 22 | +from nats.client import connect |
| 23 | + |
| 24 | +async def main(): |
| 25 | + client = await connect("nats://localhost:4222") |
| 26 | + |
| 27 | + # Subscribe |
| 28 | + async with await client.subscribe("foo") as subscription: |
| 29 | + # Publish |
| 30 | + await client.publish("foo", "Hello World!") |
| 31 | + |
| 32 | + # Receive message |
| 33 | + message = await subscription.next() |
| 34 | + print(f"Received: {message.data}") |
| 35 | + |
| 36 | + await client.close() |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + asyncio.run(main()) |
| 40 | +``` |
| 41 | + |
| 42 | +## 🚀 Performance |
| 43 | + |
| 44 | +This client implementation delivers significant performance improvements over the nats.aio client, particularly for high-frequency, small message workloads. |
| 45 | + |
| 46 | +Do note tho, it is not as feature complete at this point in time. |
| 47 | + |
| 48 | +| Message Size | nats.py (python3) | nats.py (pypy3) | experimental-nats.py (python3) | experimental-nats (pypy3) | Performance Gain | |
| 49 | +|--------------|-------------------|-----------------|--------------------------------|---------------------------|------------------| |
| 50 | +| 1B | 127,411 | 153,009 | 1,522,673 | **5,376,113** | **35.1x** 🚀 | |
| 51 | +| 2B | 136,485 | 148,981 | 1,544,513 | **5,396,347** | **36.2x** 🚀 | |
| 52 | +| 4B | 131,630 | 149,297 | 1,548,191 | **5,356,600** | **35.9x** 🚀 | |
| 53 | +| 8B | 138,229 | 141,117 | 1,530,825 | **5,307,400** | **37.6x** 🚀 | |
| 54 | +| 16B | 140,874 | 149,826 | 1,539,244 | **5,211,168** | **34.8x** 🚀 | |
| 55 | +| 32B | 141,427 | 146,670 | 1,515,068 | **5,115,238** | **34.9x** 🚀 | |
| 56 | +| 64B | 145,257 | 153,542 | 1,505,724 | **5,339,967** | **34.8x** 🚀 | |
| 57 | +| 128B | 163,181 | 164,723 | 1,479,100 | **4,923,321** | **29.9x** 🔥 | |
| 58 | +| 256B | 145,824 | 161,017 | 1,452,996 | **4,130,165** | **25.7x** 🔥 | |
| 59 | +| 512B | 243,641 | 277,321 | 1,297,250 | **3,430,092** | **12.4x** ⚡ | |
| 60 | +| 1K | 738,895 | 802,283 | 1,253,102 | **2,374,747** | **3.0x** ⚡ | |
| 61 | +| 2K | 696,945 | 736,925 | 1,060,123 | **1,381,177** | **1.9x** ✨ | |
| 62 | +| 4K | 577,335 | 625,935 | 798,797 | **814,393** | **1.3x** ✨ | |
| 63 | +| 8K | 414,077 | 463,383 | 532,429 | 450,211 | 0.97x | |
| 64 | +| 16K | 266,104 | 309,680 | 345,651 | 228,815 | 0.74x | |
| 65 | +| 32K | 102,460 | 128,852 | 166,028 | 125,662 | 0.98x | |
| 66 | +| 64K | 55,208 | 63,563 | 74,359 | 56,804 | 0.89x | |
| 67 | + |
| 68 | +### Key Performance Insights |
| 69 | + |
| 70 | +**🎯 Sweet Spot: Small to Medium Messages** |
| 71 | +- **35-37x faster** for tiny messages (1B-64B) |
| 72 | +- **25-30x faster** for small messages (128B-256B) |
| 73 | +- **12x faster** for medium messages (512B) |
| 74 | + |
| 75 | +### Benchmark Environment |
| 76 | + |
| 77 | +- **CPU**: Apple M3 Max |
| 78 | +- **Memory**: 36 GB |
| 79 | +- **Python**: 3.x |
| 80 | +- **PyPy**: 3.x |
| 81 | + |
| 82 | +> **Note**: Benchmarks may vary based on your specific hardware, network conditions, and NATS server configuration. We recommend running your own benchmarks for production workloads. |
0 commit comments