Giter Club home page Giter Club logo

Comments (12)

wallyqs avatar wallyqs commented on May 26, 2024 2

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.

umax avatar umax commented on May 26, 2024 1

@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.

umax avatar umax commented on May 26, 2024 1

Also, it make sense to use __slots__ for Msg, Subscription and other classes for memory save and faster access.

from nats.py.

Gr1N avatar Gr1N commented on May 26, 2024 1

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.

wallyqs avatar wallyqs commented on May 26, 2024

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.

Gr1N avatar Gr1N commented on May 26, 2024

@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.

wallyqs avatar wallyqs commented on May 26, 2024

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.

Gr1N avatar Gr1N commented on May 26, 2024

Nice! Can't wait for updates!

from nats.py.

Gr1N avatar Gr1N commented on May 26, 2024

@wallyqs, PR with improvement: #55

from nats.py.

wallyqs avatar wallyqs commented on May 26, 2024

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.

rudyryk avatar rudyryk commented on May 26, 2024

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.

rudyryk avatar rudyryk commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.