From 7a6d7c25dec2e9f051a744bdbb1d96d58f074182 Mon Sep 17 00:00:00 2001 From: Keith Chiem Date: Mon, 15 May 2023 18:23:41 -0700 Subject: [PATCH 1/2] support freebsd/osx --- init.sh | 2 +- net_speed.tmux | 4 +-- run-tests.sh | 2 +- scripts/download_speed.sh | 10 ++----- scripts/helpers.sh | 56 ++++++++++++++++++++++++++++++------ scripts/net_speed.sh | 2 +- scripts/upload_speed.sh | 10 ++----- tests/suites/helpers.test.sh | 2 +- tests/test_utils.sh | 2 +- 9 files changed, 59 insertions(+), 31 deletions(-) diff --git a/init.sh b/init.sh index 65235f0..8e6b906 100755 --- a/init.sh +++ b/init.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/scripts/helpers.sh" diff --git a/net_speed.tmux b/net_speed.tmux index a81f908..07691bf 100755 --- a/net_speed.tmux +++ b/net_speed.tmux @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/scripts/helpers.sh" @@ -13,7 +13,7 @@ upload_interpolation="\#{upload_speed}" do_interpolation() { local input=$1 - local result="" + local result="" result=${input/$download_interpolation/$download_speed} result=${result/$net_interpolation/$net_speed} diff --git a/run-tests.sh b/run-tests.sh index 70f0d41..7ed1757 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" for testSuite in $CURRENT_DIR/tests/suites/* ; do diff --git a/scripts/download_speed.sh b/scripts/download_speed.sh index 66f5511..617cf5e 100755 --- a/scripts/download_speed.sh +++ b/scripts/download_speed.sh @@ -1,21 +1,15 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" -sum_download_speed() -{ - # Output uses first column - sum_speed 1 -} - 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) + local new_val=$(sum_speed 0) write_file $file $new_val local vel=$(get_velocity $new_val $old_val) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 6877eac..8eb517b 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash ## # Varialbes @@ -87,12 +87,22 @@ write_file() get_interfaces() { + local os=${1:-$(os_type)} local interfaces=$(get_tmux_option @net_speed_interfaces "") if [[ -z "$interfaces" ]] ; then - for interface in /sys/class/net/*; do - interfaces+=$(echo $(basename $interface) " "); - done + case $os in + freebsd|osx) + for interface in $(ifconfig -l); do + interfaces+="$interface " + done + ;; + linux) + for interface in /sys/class/net/*; do + interfaces+=$(echo $(basename $interface) " "); + done + ;; + esac fi # Do not quote the variable. This way will handle trailing whitespace @@ -102,15 +112,23 @@ get_interfaces() sum_speed() { local column=$1 + local os=$(os_type) - declare -a interfaces=$(get_interfaces) + declare -a interfaces=$(get_interfaces $os) local line="" local val=0 for intf in ${interfaces[@]} ; do - line=$(cat /proc/net/dev | grep "$intf" | cut -d':' -f 2) - speed="$(echo -n $line | cut -d' ' -f $column)" - let val+=${speed:=0} + case $os in + freebsd|osx) + line=( $(netstat -I "$intf" -ibn | grep 'Link#' | awk '{ print $(NF-4), $(NF-1) }') ) + ;; + linux) + line=( $(cat /proc/net/dev | grep "$intf" | cut -d':' -f 2 | awk '{ print $1, $9 }') ) + ;; + esac + speed=${line[$column]} + let val+=${speed:=0} done echo $val @@ -128,3 +146,25 @@ command_exists() { local command="$1" type "$command" >/dev/null 2>&1 } + +os_type() { + local os_name="unknown" + + case $(uname | tr '[:upper:]' '[:lower:]') in + linux*) + os_name="linux" + ;; + darwin*) + os_name="osx" + ;; + msys*) + os_name="windows" + ;; + freebsd*) + os_name="freebsd" + ;; + esac + + echo -n $os_name +} + diff --git a/scripts/net_speed.sh b/scripts/net_speed.sh index 5390925..e61524d 100755 --- a/scripts/net_speed.sh +++ b/scripts/net_speed.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" diff --git a/scripts/upload_speed.sh b/scripts/upload_speed.sh index b66477b..db9f97b 100755 --- a/scripts/upload_speed.sh +++ b/scripts/upload_speed.sh @@ -1,21 +1,15 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" -sum_upload_speed() -{ - # Output uses ninth column - sum_speed 9 -} - 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) + local new_val=$(sum_speed 1) write_file $file $new_val local vel=$(get_velocity $new_val $old_val) diff --git a/tests/suites/helpers.test.sh b/tests/suites/helpers.test.sh index a68bbb1..8062305 100755 --- a/tests/suites/helpers.test.sh +++ b/tests/suites/helpers.test.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPTS="$CURRENT_DIR/../../scripts" diff --git a/tests/test_utils.sh b/tests/test_utils.sh index 045d3e5..934767f 100644 --- a/tests/test_utils.sh +++ b/tests/test_utils.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash ############################################################################### # Author: Travis Goldie # Purpose: Util funcs for unit tests From 9144ab3e94e3de081f59344fad67c18c9fbb4c19 Mon Sep 17 00:00:00 2001 From: Keith Chiem Date: Tue, 30 May 2023 04:46:43 -0700 Subject: [PATCH 2/2] - lookup up and down speeds at the same time to reduce overhead. - store results with timestamp so multiple instances work correctly. --- scripts/download_speed.sh | 11 ++------ scripts/helpers.sh | 55 ++++++++++++++++++++++++++++++++------- scripts/net_speed.sh | 6 +++-- scripts/upload_speed.sh | 11 ++------ 4 files changed, 53 insertions(+), 30 deletions(-) diff --git a/scripts/download_speed.sh b/scripts/download_speed.sh index 617cf5e..493b77e 100755 --- a/scripts/download_speed.sh +++ b/scripts/download_speed.sh @@ -5,18 +5,11 @@ source "$CURRENT_DIR/helpers.sh" 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_speed 0) - - write_file $file $new_val - local vel=$(get_velocity $new_val $old_val) + local speed=$(get_speed 1) ## Format output local format=$(get_tmux_option @download_speed_format "%s") - printf "$format" "$vel" + printf "$format" "$speed" } main diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 8eb517b..aa7b3ca 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -3,8 +3,9 @@ ## # Varialbes ## -DOWNLOAD_FILE="/tmp/tmux_net_speed.download" -UPLOAD_FILE="/tmp/tmux_net_speed.upload" +LAST_FILE="/tmp/tmux_net_speed.last" +_last=() +_current=() get_tmux_option() { local option=$1 @@ -28,12 +29,12 @@ get_velocity() { local new_value=$1 local old_value=$2 + local interval=${3:-$(get_tmux_option 'status-interval' 5)} # Consts local THOUSAND=1024 local MILLION=1048576 - local interval=$(get_tmux_option 'status-interval' 5) local vel=$(( ( new_value - old_value ) / interval )) local vel_kb=$(( vel / THOUSAND )) local vel_mb=$(( vel / MILLION )) @@ -111,13 +112,12 @@ get_interfaces() sum_speed() { - local column=$1 local os=$(os_type) + local down_total=0 + local up_total=0 declare -a interfaces=$(get_interfaces $os) - local line="" - local val=0 for intf in ${interfaces[@]} ; do case $os in freebsd|osx) @@ -126,12 +126,47 @@ sum_speed() linux) line=( $(cat /proc/net/dev | grep "$intf" | cut -d':' -f 2 | awk '{ print $1, $9 }') ) ;; - esac - speed=${line[$column]} - let val+=${speed:=0} + esac + let down_total+=${line[0]} + let up_total+=${line[1]} done - echo $val + echo $down_total $up_total +} + +# 1 - download, 2 - upload +get_speed() +{ + local column=$1 + local subprocess=${2:-0} + local interval=$(get_tmux_option 'status-interval' 5) + local now=$(date +%s) + + # timestamp download_cumulative upload_cumulative + local last=( "${_last[@]}" ) + if [ ${#last[@]} -eq 0 ]; then + last=( $(read_file "$LAST_FILE" "$now 0 0") ) + _last=( "${last[@]}" ) + fi + # protect against division by 0 + if [[ $last -eq $now ]]; then sleep 1; now=$(date +%s); fi + local current=( "${_current[@]}" ) + if [ ${#current[@]} -eq 0 ]; then + current=( $now $(sum_speed) ) + _current=( "${current[@]}" ) + fi + local delta_t=$(( $current - $last )) + local speed=$(get_velocity ${current[$column]} ${last[$column]} $delta_t) + + if [[ $subprocess -eq 0 ]] && [[ $((${last[0]}+$interval)) -le $now ]]; then + function update_last() + { + write_file "$LAST_FILE" "${_current[*]}" + } + trap update_last EXIT + fi + + echo $speed } is_osx() { diff --git a/scripts/net_speed.sh b/scripts/net_speed.sh index e61524d..b4ca7ea 100755 --- a/scripts/net_speed.sh +++ b/scripts/net_speed.sh @@ -5,8 +5,10 @@ source "$CURRENT_DIR/helpers.sh" main() { - local download=$("$CURRENT_DIR/download_speed.sh") - local upload=$("$CURRENT_DIR/upload_speed.sh") + # calls within $() are in subprocess shells, run first to cache values + get_speed 1 >/dev/null + local download=$(get_speed 1 1) + local upload=$(get_speed 2 1) ## Format output local format=$(get_tmux_option @net_speed_format "D:%10s U:%10s") diff --git a/scripts/upload_speed.sh b/scripts/upload_speed.sh index db9f97b..a76341a 100755 --- a/scripts/upload_speed.sh +++ b/scripts/upload_speed.sh @@ -5,18 +5,11 @@ source "$CURRENT_DIR/helpers.sh" 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_speed 1) - - write_file $file $new_val - local vel=$(get_velocity $new_val $old_val) + local speed=$(get_speed 2) ## Format output local format=$(get_tmux_option @upload_speed_format "%s") - printf "$format" "$vel" + printf "$format" "$speed" } main