saviorand / lightbug_http

Simple and fast HTTP framework for Mojo! 🔥
MIT License
578 stars 35 forks source link

Remove unnecessary string conversions #50

Closed saviorand closed 5 months ago

alainrollejr commented 5 months ago

To get a feel for the problem, I basically searched for all magic number occurences equal to 4096 (there are quite a lot) and replaced them all by 1048576. Might be not all of them needed enlarging but then I have not grown with the code so hard for me to go analyse that. I can then increase packet sizes without the client or the server crashing. The fundamental problem remains though that speed still rapidly decreases with packet size. I suspect there is room for optimization by considering whether all those allocs really need to be within the function calls themselves. I.e. you could probably allocate a big buffer at struct instantiation time and then point to that one over and over again rather than alloc and free ? For what it's worth, output of my test client:

=======================
packet size 1000 Bytes:
=========================
Sent and received 1000 packets in 0.4144 seconds
Packet rate 2.41 kilo packets/s
Bit rate 19.3  Mbps
=======================
packet size 2000 Bytes:
=========================
Sent and received 1000 packets in 0.4268 seconds
Packet rate 2.34 kilo packets/s
Bit rate 37.5  Mbps
=======================
packet size 4000 Bytes:
=========================
Sent and received 1000 packets in 0.4800 seconds
Packet rate 2.08 kilo packets/s
Bit rate 66.7  Mbps
=======================
packet size 8000 Bytes:
=========================
Sent and received 1000 packets in 0.5553 seconds
Packet rate 1.80 kilo packets/s
Bit rate 115.2  Mbps
=======================
packet size 16000 Bytes:
=========================
Sent and received 1000 packets in 0.7618 seconds
Packet rate 1.31 kilo packets/s
Bit rate 168.0  Mbps
=======================
packet size 32000 Bytes:
=========================
Sent and received 1000 packets in 1.1680 seconds
Packet rate 0.86 kilo packets/s
Bit rate 219.2  Mbps
=======================
packet size 64000 Bytes:
=========================
Sent and received 1000 packets in 1.7495 seconds
Packet rate 0.57 kilo packets/s
Bit rate 292.7  Mbps
=======================
packet size 128000 Bytes:
=========================
Sent and received 1000 packets in 1.7782 seconds
Packet rate 0.56 kilo packets/s
Bit rate 575.9  Mbps
=======================
packet size 256000 Bytes:
=========================
Traceback (most recent call last):
  File "/Users/alainrolle/miniconda3/envs/py312/lib/python3.12/site-packages/urllib3/response.py", line 737, in _error_catcher
    yield
saviorand commented 5 months ago

@alainrollejr ah, good to know, thanks. I'm refactoring more today, expecting some major improvements both in reliability in performance after it's done. Will keep you posted

saviorand commented 5 months ago

@alainrollejr made some really major refactoring this weekend on this branch. Below is a selection of a couple test runs I made. I don't see a steep increase anymore with the size of the packet, although as you can see overall the rate is slower now. This is with connection: close though, which is not how it should work by default. Setting tcp_keep_alive should improve the performance up to 2x-2.5x based on experience. Working on this currently.

=======================
packet size 1280 Bytes:
=========================
Sent and received 1000 packets in 1.4183 seconds
Packet rate 0.71 kilo packets/s
Bit rate 7.2  Mbps
=======================

=======================
packet size 8500 Bytes:
=========================
Sent and received 1000 packets in 2.3081 seconds
Packet rate 0.43 kilo packets/s
Bit rate 29.5  Mbps

=======================
packet size 10000 Bytes:
=========================
Sent and received 1000 packets in 2.2904 seconds
Packet rate 0.44 kilo packets/s
Bit rate 34.9  Mbps

packet size 12800 Bytes:
=========================
Sent and received 1000 packets in 2.5152 seconds
Packet rate 0.40 kilo packets/s
Bit rate 40.7  Mbps
=======================

=======================
packet size 100000 Bytes:
=========================
Sent and received 1000 packets in 2.0973 seconds
Packet rate 0.48 kilo packets/s
Bit rate 381.4  Mbps

=======================
packet size 128000 Bytes:
=========================
Sent and received 1000 packets in 2.1425 seconds
Packet rate 0.47 kilo packets/s
Bit rate 477.9  Mbps

packet size 128000 Bytes:
=========================
Sent and received 1000 packets in 1.9405 seconds
Packet rate 0.52 kilo packets/s
Bit rate 527.7  Mbps
thatstoasty commented 5 months ago

@saviorand It's cool to see you're using some of the other structs from Gojo! I'm just going through the code to see if I spot anything else that might help speed up the code.

Copyable has been removed from these structs in the latest gojo release to prevent this from accidentally happening.