Skip to content

Commit 3bef19f

Browse files
author
Raphael Freudiger
committed
speed up transfer rate script
The script does the following: 1. Read first values for all interfaces and store them in an array 2. Sleep once. I even decreased the time to half a second. 3. Get second values and do the calculation.
1 parent 0db709f commit 3bef19f

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

server/modules/shell_files/download_transfer_rate.sh

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,37 @@
33
files=(/sys/class/net/*)
44
pos=$(( ${#files[*]} - 1 ))
55
last=${files[$pos]}
6+
in1=()
67

78
json_output="{"
89

9-
for interface in "${files[@]}"
10+
for interface in "${files[@]}"
1011
do
1112
basename=$(basename "$interface")
12-
13+
1314
# find the number of bytes transfered for this interface
14-
in1=$(cat /sys/class/net/"$basename"/statistics/rx_bytes)
15+
in1+=( $(cat /sys/class/net/"$basename"/statistics/rx_bytes) )
16+
done
1517

16-
# wait a second
17-
sleep 1
18+
# wait a moment
19+
sleep 0.5
1820

21+
for interface in "${files[@]}"
22+
do
23+
basename=$(basename "$interface")
1924
# check same interface again
2025
in2=$(cat /sys/class/net/"$basename"/statistics/rx_bytes)
2126

27+
# read and remove first element
28+
in=${in1[0]}
29+
unset in1[0]
30+
in1=( "${in1[@]}" )
31+
2232
# get the difference (transfer rate)
23-
in_bytes=$((in2 - in1))
33+
in_bytes=$((in2 - in))
2434

2535
# convert transfer rate to KB
26-
in_kbytes=$((in_bytes / 1024))
36+
in_kbytes=$((in_bytes / 1024 * 2))
2737

2838
# convert transfer rate to KB
2939
json_output="$json_output \"$basename\": $in_kbytes"
@@ -33,7 +43,7 @@ do
3343
then
3444
# add a comma to the line (JSON formatting)
3545
json_output="$json_output,"
36-
fi
46+
fi
3747
done
3848

3949
# close the JSON object & print to screen

server/modules/shell_files/upload_transfer_rate.sh

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,38 @@
33
files=(/sys/class/net/*)
44
pos=$(( ${#files[*]} - 1 ))
55
last=${files[$pos]}
6+
out1=()
67

78
json_output="{"
89

9-
for interface in "${files[@]}"
10+
for interface in "${files[@]}"
1011
do
1112
basename=$(basename "$interface")
12-
13+
1314
# find the number of bytes transfered for this interface
14-
out1=$(cat /sys/class/net/"$basename"/statistics/tx_bytes)
15+
out1+=( $(cat /sys/class/net/"$basename"/statistics/tx_bytes) )
16+
done
17+
18+
# wait a moment
19+
sleep 0.5
1520

16-
# wait a second
17-
sleep 1
21+
for interface in "${files[@]}"
22+
do
23+
basename=$(basename "$interface")
1824

1925
# check same interface again
20-
out2=$(cat /sys/class/net/"$basename"/statistics/tx_bytes)
26+
out2=$(cat /sys/class/net/"$basename"/statistics/tx_bytes)
27+
28+
# read and remove first element
29+
out=${out1[0]}
30+
unset out1[0]
31+
out1=( ${out1[@]} )
2132

2233
# get the difference (transfer rate)
23-
out_bytes=$((out2 - out1))
34+
out_bytes=$((out2 - out))
2435

2536
# convert transfer rate to KB
26-
out_kbytes=$((out_bytes / 1024))
37+
out_kbytes=$((out_bytes / 1024 * 2))
2738

2839
# convert transfer rate to KB
2940
json_output="$json_output \"$basename\": $out_kbytes"
@@ -33,7 +44,7 @@ do
3344
then
3445
# add a comma to the line (JSON formatting)
3546
json_output="$json_output,"
36-
fi
47+
fi
3748
done
3849

3950
# close the JSON object & print to screen

0 commit comments

Comments
 (0)