nrkno / sofie-atem-connection

Sofie ATEM Connection: A Part of the Sofie TV Studio Automation System
https://github.com/nrkno/Sofie-TV-automation/
MIT License
134 stars 36 forks source link

Rapidly (10ms) firing _retransmitTimer causes high CPU usage #96

Open endreszabo opened 4 years ago

endreszabo commented 4 years ago

I'm running this lib in a simple script on a relatively low performance Raspberry PI and after a week of runtime I noticed that the node processes using atem-connection has high (8% average) overall CPU usage. Here is my proposal.

I dug down deep and found that this timer called _retransmitTimer fires every 10 milliseconds a function called _checkForRetransmit to check if there is any packets to resend (or reconnect if stuck, etc).

Since all its logic depends on the members of the _inFlight array, I would kindly ask to enable the timer only when there are actual packets referenced in this array, and close() it once all packets are sent. This would also affect the startTimers function which creates this timer right after connecting. This way we can spare CPU time.

If busy I'm happy to create a PR later but first I'll have to get familiar with TS.

Julusian commented 4 years ago

What model of pi are you using here? On a pi4 I am seeing around 2% usage of a single core, so not a very significant load.

My only concern with this is that it will mean we have to be very careful to ensure the timer gets started and stopped at the right times. That said I'm not opposed to this, but it'll need to be well tested before being merged into a release version