park-bench / netcheck

Finds the best network with access to the Internet.
GNU General Public License v3.0
0 stars 0 forks source link

netcheck's Main Loop Runs Too Slowly #26

Open eviljoel opened 5 years ago

eviljoel commented 5 years ago

Right now netcheck's main loop generally takes just under 1 second to complete. I feel this is too slow and uses up too much CPU. I believe much of the slowness comes from the fact that there is no mapping from a NetworkManager connection to a network interface device. Since I made the decision to have netcheck.py not have any knowledge of network devices (for separation of concerns and to help the programmer understand the code), this means that the applicable network devices have to be discovered at the beginning of almost all NetworkManagerHelper methods. This results in a whole lot of looping and external calls that don't provide any real value.

I want to point out that I realized that coding in this manner might be slow, but I didn't know how slow. I didn't know if the main loop would run in 1 second or .01 seconds. If it had run in .01 seconds, then avoiding this from the get-go would have been premature optimization.

Also, it is important to note that a mapping from connections to devices can quickly become stale. New devices can be plugged in or removed and NetworkManager can be randomly upgraded and restarted. Therefore, it is important that any connection to device mapping be refreshed often.

I propose having a NetworkManagerContext object built at the beginning of each main loop. This context object would essentially have a mapping from connections to devices and will be passed in to most NetworkManagerHelper method calls as the first parameter. We will assume that the mapping is valid for the duration of the main program loop. This might not actually be the case, but netcheck should be written to recover in the event of stale context data. At the end of each main loop, the context object is discarded.