ikalchev / HAP-python

A python implementation of the HomeKit Accessory Protocol (HAP)
Other
629 stars 119 forks source link

Chacha20_Poly1305 performance can result in unresponsive homekit #228

Closed bdraco closed 4 years ago

bdraco commented 4 years ago
Screen Shot 2020-03-08 at 6 22 05 PM

If the bridge has ~80 devices the startup can timeout on slow systems because the chacha overhead in python.

I wonder if this can be switched to be done in C

bdraco commented 4 years ago

https://github.com/Legrandin/pycryptodome/blob/master/lib/Crypto/Cipher/ChaCha20_Poly1305.py

bdraco commented 4 years ago

Looks like its most of my startup time for home assistant

 14.00%  14.00%    1.79s     1.79s   do_execute (sqlalchemy/engine/default.py:588)
  8.00%   8.00%    1.11s     1.11s   acquire (logging/__init__.py:843)
 14.00%  14.00%   0.770s    0.770s   _loop (senseme/lib/background_monitor.py:34)
 15.00%  15.00%   0.560s    0.560s   recv (pyhap/hap_server.py:725)
 14.00%  14.00%   0.550s    0.550s   run (zeroconf/__init__.py:1226)
  8.00%   8.00%   0.520s    0.520s   double_round (tlslite/utils/chacha.py:87)
  1.00%   1.00%   0.440s    0.440s   double_round (tlslite/utils/chacha.py:79)
  5.00%   5.00%   0.420s    0.420s   double_round (tlslite/utils/chacha.py:91)
  0.00%   0.00%   0.360s    0.360s   _fetchall_impl (sqlalchemy/engine/result.py:1161)
  6.00%   6.00%   0.350s    0.350s   double_round (tlslite/utils/chacha.py:83)
 10.00%  10.00%   0.330s    0.330s   sendall (pyhap/hap_server.py:786)
  0.00%   0.00%   0.320s    0.320s   handle_read (zeroconf/__init__.py:1265)
  0.00%   0.00%   0.300s    0.300s   double_round (tlslite/utils/chacha.py:85)
  3.00%   3.00%   0.280s    0.280s   <genexpr> (tlslite/utils/chacha.py:150)
  5.00%   5.00%   0.270s    0.270s   emit (logging/__init__.py:1028)
  5.00%   5.00%   0.260s    0.260s   double_round (tlslite/utils/chacha.py:89)
  0.00%   0.00%   0.250s    0.250s   checkcache (linecache.py:74)
  2.00%   2.00%   0.230s    0.230s   double_round (tlslite/utils/chacha.py:81)
  0.00%   0.00%   0.220s    0.220s   double_round (tlslite/utils/chacha.py:77)
  2.00%   2.00%   0.220s    0.220s   uuid4 (uuid.py:761)
Jc2k commented 4 years ago

I recently ported aiohomekit to use cryptography for nearly all its crypto, including ChaCha20_Poly1305. That uses openssl under the hood, so wonder if it would help here?

bdraco commented 4 years ago

I recently ported aiohomekit to use cryptography for nearly all its crypto, including ChaCha20_Poly1305. That uses openssl under the hood, so wonder if it would help here?

That should make a big difference and likely solve this

bdraco commented 4 years ago

@Jc2k Converted to using cryptography and updates are now almost instant. I used to watch the following scroll down the screen, and now it just appears!

2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
2020-03-27 19:35:52 INFO (Thread-32) [pyhap.hap_server] 192.168.210.42 - "PUT /characteristics HTTP/1.1" 204 -
bdraco commented 4 years ago

Crypto time is now imperceptible with the profiler

Screen Shot 2020-03-27 at 2 41 24 PM
bdraco commented 4 years ago

Haven't had siri tell me that some of your devices are not responding since I switched it. It would happen quite regularly before.

bdraco commented 4 years ago

Pairing is now near instant for me as well

bdraco commented 4 years ago

@Jc2k Thanks for pointing me to cryptography

Jc2k commented 4 years ago

Nice! Good work @bdraco! crpytography is my goto, so glad its paying off for you here! And conveniently for us HA already depends on a new enough version! :-)

ikalchev commented 4 years ago

Awesome work! Merged!