Blob Encryption bottleneck
It was reported that Blob Encryption was a bottleneck for publishing large files. For encryption we use Pycrypto which provides python interfaces for encryption algorithms written in C.
Quick improvements can likely be made by improving the lbrynet python code that calls Pycrypto by following some basic optimization tips https://wiki.python.org/moin/PythonSpeed/PerformanceTips. This class is responsible for encrypting and decrypting blobs: https://github.com/lbryio/lbry/blob/master/lbrynet/cryptstream/CryptBlob.py
We could also consider alternative encryption algorithms that are faster than what we are using now (AES-128 CBC). Profiling along with research on the potential disadvantages should be considered.
This PR https://github.com/lbryio/lbry/pull/733 adds unit tests for CryptBlob.py in tests/unit/cryptstream/test_cryptblob.py which can be used as reference for writing tests for Encryption speed.
Currently I'm getting that it takes roughly 0.012 seconds to encrypt a blob (which is 2 megabytes) on my 6th gen i5 laptop. This means that it has a throughput of about 166 megabytes/second. In this test, I did not write the blob to disk , which could be the actual bottleneck depending on the system.
Another point to note that improving performance of CryptBlob.py may not necessarily solve the bottleneck isssue. Their could be problems / inefficiencies in code that is using/calling CryptBlob.py.
Thanks I sent to wolf0
Shockr from Slack is looking at this
It would help if I could get some test code for the publish process which I can use to benchmark and identify possible speed improvements.
You could either look at the functional tests https://github.com/lbryio/lbry/blob/master/tests/functional/test_misc.py#L395 which sets up the minimum classes required to test upload/download
Or look at lbry-in-a-box https://github.com/lbryio/lbry-in-a-box which uses docker to setup everything required to run lbry in a single machine, more specifically, look at the integration testing we run on lbry-in-a-box where we test publishing:
https://github.com/lbryio/lbry-in-a-box/blob/master/integration_testing.py#L290
@akinwale please post your benchmark findings. Thank you
Next step: profile encryption algo, figure out where the actual bottleneck is
@finer9 I was able to benchmark using the functional test at https://github.com/lbryio/lbry/blob/master/tests/functional/test_streamify.py#L117, particularly the create_stream callback at https://github.com/lbryio/lbry/blob/master/tests/functional/test_streamify.py#L163
Using the default test value at ~50MB, I get the following results:
genfile took 0ms
create_lbry_file took 793ms
publish_sd_blob took 9ms
Using a value of 681,574,400 (~650MB) which is the size of a random 30-minute 1080p Youtube video I downloaded, I get the following results:
genfile took 0ms
create_lbry_file took 12788ms
publish_sd_blob took 30ms
It does appear that the create_lbry_file method is creating the blobs serially, based on my observation of the log output from the _blob_finished callback. Due to my limited familiarity with the architecture of the application at this point, and my limited experience with Python, I am unable to identify where the blobs are split and individually created, but I believe that if the process can be refactored to run in parallel, using multiple I/O threads or by forking the running process, this would definitely reduce the execution time.
I took a look at your code and did a similar test as @akinwale with a ~600MB file and a profiler (plop). Results can be seen here: http://imgur.com/a/KHkFZ
I used encrypt_blob.py script, which I fixed for splitting. Will do a PR if you agree it is broken (it was generating a single blob and stopping, instead of the whole thing).
If you modify BLOB_SIZE on conf.py to a huge value, then you will notice a time decrease. Here are my numbers for 601MB:
- With BLOB_SIZE of 1000 MB, 8.3s
- With default blob size of 2 MB, 13,96s
Decreasing the BLOB_SIZE to 1MB gave me another jump in time (18s). So the splitting overhead isn't just crypto and disk. From the profile pasted, it looks like you have too many components interacting in this tight loop with many instantiations. For a 600MB, that's 300 chunks. Some interesting part I found is this: https://github.com/lbryio/lbry/blob/906e6b238e737429f063ad5eaba369c98bd1148f/lbrynet/cryptstream/CryptStreamCreator.py#L77 which can be improved to create the writers in threads as suggested, but instead it is writing directly, blocking the caller (the reader).
There is also another side, the read size. It's controlled today by FileSender, which has a CHUNK_SIZE of 2**14, you can check it here.
On the next weekend or earlier (when I get some free time) I will try to dig deeper and optimize it. I still feel that the overhead is somewhere in the interaction between CryptStreamCreator, CryptBlob and HashBlob.
Now, some questions:
- Why AES-CBC and SHA384? AES-GCM can provide integrity on the cipher itself, which also uses hardware acceleration on AES-NI devices. So you would remove the hashing overhead.
- Does the blob creation always go disk->disk? Or there is a scenario where it's streamed to a server directly? If it's just disk->disk then I think a small script in pure python would be better. You could also use Cython to make this simpler loop faster.
- Why do you use pycryptopp if Twisted already depends on cryptography library? This library also provides pbkdf2, which is in another dependency (although I can't see it being used).
Btw, that's my branch with encrypt_blob.py generating all blobs: https://github.com/shyba/lbry/tree/update_scripts
You can use it to profile, like this (supposing you already are in virtualenv):
- pip install plop
- cd scripts
- python -m plop.collector encrypt_blob.py /tmp/test '61616161616161616161616161616161' '69696969696969696969696969696969'
- python -m plop.viewer
Great comment, well written! @jackrobison and @kauffj prob should comment on this, i think we should keep the blob size small so we easily can get from multiple peers faster ( just my own thought )
Merged in https://github.com/lbryio/lbry/pull/898