Comments (12)
Thanks everyone for the feedback in this issue. In the next release v0.7.0 the inbox generation will be around twice as fast (although think implementation at https://github.com/nats-io/asyncio-nats/blob/master/nats/aio/nuid.py could be further optimized).
print('NUID based inboxes x 100,000 times:\n%ss' % timeit.timeit(
stmt='inbox = INBOX_PREFIX[:]; inbox.extend(nuid.next())',
setup='from nats.aio.nuid import NUID; INBOX_PREFIX = bytearray(b"_INBOX."); nuid = NUID();',
number=100000,
))
print('Old style inboxes x 100,000 times:\n%ss' % timeit.timeit(
stmt='new_inbox()',
setup='from nats.aio.utils import new_inbox',
number=100000,
))
Results:
NUID based inboxes x 100,000 times:
0.738977312023053s
Old style inboxes x 100,000 times:
1.3899898040108383s
```
Also with the internal changes to the request/response implementation (https://github.com/nats-io/asyncio-nats/pull/59) and using uvloop getting much results in the latency test:
```
# Before:
$ python3.6 benchmark/latency_perf.py
Sending 10000 request/responses on [test]
#######################################
Test completed : 0.340 ms avg request/response latency
# After:
$ python3.6 benchmark/latency_perf.py
Sending 10000 request/responses on [test]
#######################################
Test completed : 0.284 ms avg request/response latency
```
from nats.py.
@wallyqs does this diff may have a chance?
+++ b/nats/aio/utils.py
@@ -9,7 +9,7 @@ def hex_rand(n):
"""
Generates a hexadecimal string with `n` random bits.
"""
- return "%x" % random.SystemRandom().getrandbits(n)
+ return "%x" % random.getrandbits(n)
Before this diff:
python benchmark/latency_perf.py
Sending 10000 request/responses on [test]
#########
Test completed : 1.405 ms avg request/response latency
After this diff:
python benchmark/latency_perf.py
Sending 10000 request/responses on [test]
#########
Test completed : 0.430 ms avg request/response latency
Br,
Max
from nats.py.
Also, it make sense to use __slots__
for Msg, Subscription and other classes for memory save and faster access.
from nats.py.
About random, better to use SystemRandom
, but move part of code to the top of module:
diff --git a/nats/aio/utils.py b/nats/aio/utils.py
index b289971..7aed392 100644
--- a/nats/aio/utils.py
+++ b/nats/aio/utils.py
@@ -2,6 +2,16 @@
import random
+
+# Use the system PRNG if possible
+try:
+ random = random.SystemRandom()
+except NotImplementedError:
+ import warnings
+ warnings.warn('A secure pseudo-random number generator is not available '
+ 'on your system. Falling back to Mersenne Twister.')
+
+
INBOX_PREFIX = "_INBOX."
@@ -9,7 +19,7 @@ def hex_rand(n):
"""
Generates a hexadecimal string with `n` random bits.
"""
- return "%x" % random.SystemRandom().getrandbits(n)
+ return "%x" % random.getrandbits(n)
def new_inbox():
from nats.py.
You can take a look at the latency_perf
that is in the repo to confirm the request/response roundtrip performance from the client: https://github.com/nats-io/asyncio-nats/blob/master/benchmark/latency_perf.py
You are right that there is room for improvements in the current roundtrip time, for example locally I'm getting the following:
python3 benchmark/latency_perf.py
Sending 10000 request/responses on [test]
#########
Test completed : 1.628 ms avg request/response latency
$ nats-top
...
In: Msgs: 188.3K Bytes: 308.2K Msgs/Sec: 1356.1 Bytes/Sec: 0
Out: Msgs: 188.3K Bytes: 308.2K Msgs/Sec: 1356.1 Bytes/Sec: 0
While a similar test but for the Ruby library, the numbers are a bit higher:
ruby benchmark/latency_perf.rb
Sending 10000 request/responses
Test completed : 0.35 ms avg request/response latency
$ nats-top
...
In: Msgs: 131.4K Bytes: 308.2K Msgs/Sec: 6232.8 Bytes/Sec: 0
Out: Msgs: 131.4K Bytes: 308.2K Msgs/Sec: 6232.8 Bytes/Sec: 0
I have some pending updates to do around the request/response handling from the library to follow the new style of request/response done like in the Go library, so will take a look at revising performance again soon.
from nats.py.
@wallyqs nice to hear that you are working on performance improvements. In our company we are interesting in nats and python client.
Maybe you can share any estimates you have with us? Thanks!
from nats.py.
Right now I'm planning a couple of releases, one minor release at the end of this month v0.6.6
and then a major version bump v0.7.0
middle of next month which would include updates in the request/response handling.
from nats.py.
Nice! Can't wait for updates!
from nats.py.
@wallyqs, PR with improvement: #55
from nats.py.
Thanks for the tips, the SystemRandom
change improves the latency test quite a bit. Sounds good on using __slots__
for Msg
class as well, seems like would be more efficient... http://book.pythontips.com/en/latest/__slots__magic.html
from nats.py.
We can also use os.urandom()
here, as it produces "real" random numbers. Here's benchmarking results:
random.SystemRandom() x 10000 times:
2.0309601490153s
random.getrandbits() x 10000 times:
0.008057761995587498s
os.urandom() x 10000 times:
0.04249835800146684s
Code for benchmarks:
import timeit
if __name__ == '__main__':
print('random.SystemRandom() x 10000 times:\n%ss' % timeit.timeit(
stmt='"%x" % random.SystemRandom().getrandbits(256)',
setup='import random',
number=10000,
))
print('random.getrandbits() x 10000 times:\n%ss' % timeit.timeit(
stmt='"%x" % random.getrandbits(256)',
setup='import random',
number=10000,
))
print('os.urandom() x 10000 times:\n%ss' % timeit.timeit(
stmt='binascii.hexlify(os.urandom(32)).decode()',
setup='import os, binascii',
number=10000,
))
from nats.py.
Ah, OK - with pre-created SystemRandom()
generator it runs at almost the same speed:
random.SystemRandom() x 100000 times:
0.5566856279619969s
random.getrandbits() x 100000 times:
0.09096840699203312s
os.urandom() x 100000 times:
0.48127816000487655s
Updated code:
print('random.SystemRandom() x 100000 times:\n%ss' % timeit.timeit(
stmt='"%x" % sysrandom.getrandbits(256)',
setup='import random; sysrandom = random.SystemRandom()',
number=100000,
))
from nats.py.
Related Issues (20)
- Can't install version 2.2.0 HOT 9
- Unable to connect using Bcrypted password HOT 1
- Add compression field for stream config
- ping/pong is not working HOT 1
- msg.ack_sync(timeout=15.0) but acknowledgment floor stays at 8 HOT 2
- CERTIFICATE_VERIFY_FAILED HOT 2
- Message publish failure after reconnection
- Jetstream no available responders HOT 1
- published wheels would be really helpful
- [JetStream] Publishing to a subject without stream will fail in nats.errors.TimeoutError
- Object Store replication from LeafNode to Cluster nats: error: nats: no stream matches subject HOT 2
- Placement setting has no effect when creating a new key-value bucket HOT 1
- Using the async iterator is significantly slower compared to using callbacks in a subscription
- Implement Consumer Pause
- Upon reconnect in k8s, python nats client does not receive messages
- Client does not handle connect URL containing multiple servers HOT 4
- nats-py broken while running opentelemetry instrumentation 0.44b0
- Add Service API HOT 3
- KV should raise an error if the key contains unacceptable characters
- Bug in URL Normalization from List in client.py class
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nats.py.