Skip to content

Commit

Permalink
Cache results to prevent miscalculations
Browse files Browse the repository at this point in the history
The code assumes that the status bar will only be updated every
status-interval seconds

In practice status-interval is a maximum value, not a minimum value. So
if the status bar is actually updated every second even though the
configuration is for 5 seconds all of the calculations are off and the
bitrate will be calculated as a fifth of the real rate.

See tmux-plugins/tmux-cpu#15 for a similar
issue with a different plugin, except that here it is much worse because
it actually leads to a WRONG RESULT and not just a faster update. This
commit implements @BrainMaestro 's suggestion from that thread.
  • Loading branch information
jgeralnik committed Feb 6, 2021
1 parent 58abb61 commit 3ed49da
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
18 changes: 12 additions & 6 deletions scripts/download_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ sum_download_speed()
main()
{
# TODO make configurable
#local file=$(get_tmux_option $DOWNLOAD_FILE)
local file=$DOWNLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_download_speed)
if ! is_update_needed $DOWNLOAD_TIME_FILE; then
local vel=$(read_file $DOWNLOAD_CACHE_FILE)
else
local file=$DOWNLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_download_speed)

write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)
write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)

write_file $DOWNLOAD_TIME_FILE $(date +%s)
write_file $DOWNLOAD_CACHE_FILE "$vel"
fi

## Format output
local format=$(get_tmux_option @download_speed_format "%s")
Expand Down
17 changes: 17 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
##
DOWNLOAD_FILE="/tmp/tmux_net_speed.download"
UPLOAD_FILE="/tmp/tmux_net_speed.upload"
DOWNLOAD_CACHE_FILE="/tmp/tmux_net_speed.download.cache"
UPLOAD_CACHE_FILE="/tmp/tmux_net_speed.upload.cache"
DOWNLOAD_TIME_FILE="/tmp/tmux_net_speed.download.time"
UPLOAD_TIME_FILE="/tmp/tmux_net_speed.upload.time"

get_tmux_option() {
local option=$1
Expand All @@ -24,6 +28,19 @@ set_tmux_option() {
tmux set-option -gq "$option" "$value"
}

is_update_needed()
{
local update_file=$1

local interval=$(get_tmux_option 'status-interval' 5)
local update_time=$(read_file $update_file)
local cur_time=$(date +%s)
if [ $((update_time + interval)) -gt $cur_time ]; then
return 1;
fi;
return 0;
}

get_velocity()
{
local new_value=$1
Expand Down
17 changes: 12 additions & 5 deletions scripts/upload_speed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ main()
{
# TODO make configurable
#local upload_file=$(get_tmux_option $UPLOAD_FILE)
local file=$UPLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_upload_speed)
if ! is_update_needed $UPLOAD_TIME_FILE; then
local vel=$(read_file $UPLOAD_CACHE_FILE)
else
local file=$UPLOAD_FILE
local old_val=$(read_file $file)
local new_val=$(sum_upload_speed)

write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)
write_file $file $new_val
local vel=$(get_velocity $new_val $old_val)

write_file $UPLOAD_TIME_FILE $(date +%s)
write_file $UPLOAD_CACHE_FILE "$vel"
fi

## Format output
local format=$(get_tmux_option @upload_speed_format "%s")
Expand Down

0 comments on commit 3ed49da

Please sign in to comment.