across/bench.sh

331 lines
12 KiB
Bash
Raw Normal View History

2016-11-29 14:25:31 +09:00
#!/usr/bin/env bash
#
# Description: Auto test download & I/O speed script
#
2022-01-01 15:50:10 +09:00
# Copyright (C) 2015 - 2022 Teddysun <i@teddysun.com>
2016-11-29 14:25:31 +09:00
# Thanks: LookBack <admin@dwhd.org>
# URL: https://teddysun.com/444.html
2022-01-01 15:50:10 +09:00
# https://github.com/teddysun/across/blob/master/bench.sh
2016-11-29 14:25:31 +09:00
#
trap _exit INT QUIT TERM
2015-09-09 10:39:03 +08:00
_red() {
printf '\033[0;31;31m%b\033[0m' "$1"
}
_green() {
printf '\033[0;31;32m%b\033[0m' "$1"
}
_yellow() {
printf '\033[0;31;33m%b\033[0m' "$1"
}
_blue() {
printf '\033[0;31;36m%b\033[0m' "$1"
}
2016-11-24 22:23:24 +09:00
_exists() {
local cmd="$1"
if eval type type > /dev/null 2>&1; then
eval type "$cmd" > /dev/null 2>&1
elif command > /dev/null 2>&1; then
command -v "$cmd" > /dev/null 2>&1
else
which "$cmd" > /dev/null 2>&1
fi
local rt=$?
return ${rt}
}
2016-11-30 11:24:07 +09:00
_exit() {
_red "\nThe script has been terminated.\n"
# clean up
2021-02-10 14:59:39 +09:00
rm -fr speedtest.tgz speedtest-cli benchtest_*
exit 1
}
2015-12-14 22:15:18 +08:00
get_opsy() {
2021-02-11 15:09:56 +09:00
[ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return
2015-12-25 20:16:48 +08:00
[ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
2015-12-14 22:15:18 +08:00
[ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}
2015-09-09 10:39:03 +08:00
next() {
2015-09-23 10:04:56 +08:00
printf "%-70s\n" "-" | sed 's/\s/-/g'
2015-09-09 10:39:03 +08:00
}
speed_test() {
local nodeName="$2"
[ -z "$1" ] && ./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr > ./speedtest-cli/speedtest.log 2>&1 || \
./speedtest-cli/speedtest --progress=no --server-id=$1 --accept-license --accept-gdpr > ./speedtest-cli/speedtest.log 2>&1
if [ $? -eq 0 ]; then
local dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
local up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
local latency=$(awk '/Latency/{print $2" "$3}' ./speedtest-cli/speedtest.log)
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
printf "\033[0;33m%-18s\033[0;32m%-18s\033[0;31m%-20s\033[0;36m%-12s\033[0m\n" " ${nodeName}" "${up_speed}" "${dl_speed}" "${latency}"
fi
fi
2015-09-09 10:39:03 +08:00
}
speed() {
speed_test '' 'Speedtest.net'
2022-01-01 15:50:10 +09:00
speed_test '21541' 'Los Angeles, US'
speed_test '43860' 'Dallas, US'
speed_test '40879' 'Montreal, CA'
speed_test '24215' 'Paris, FR'
speed_test '28922' 'Amsterdam, NL'
speed_test '24447' 'Shanghai, CN'
speed_test '26352' 'Nanjing, CN'
speed_test '27594' 'Guangzhou, CN'
speed_test '32155' 'Hongkong, CN'
speed_test '6527' 'Seoul, KR'
speed_test '25960' 'Singapore, SG'
speed_test '15047' 'Tokyo, JP'
2015-09-09 10:39:03 +08:00
}
2015-12-03 14:33:33 +08:00
io_test() {
2022-01-01 15:50:10 +09:00
(LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count=$1 conv=fdatasync && rm -f benchtest_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
2015-12-03 14:33:33 +08:00
}
2016-11-24 22:23:24 +09:00
calc_disk() {
local total_size=0
2016-11-25 00:46:43 +09:00
local array=$@
for size in ${array[@]}
2016-11-24 22:23:24 +09:00
do
2016-11-25 00:46:43 +09:00
[ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}`
[ "`echo ${size:(-1)}`" == "K" ] && size=0
2016-11-27 16:37:14 +09:00
[ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' )
[ "`echo ${size:(-1)}`" == "T" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' * 1024}' )
[ "`echo ${size:(-1)}`" == "G" ] && size=${size_t}
2016-11-25 11:33:45 +09:00
total_size=$( awk 'BEGIN{printf "%.1f", '$total_size' + '$size'}' )
2016-11-24 22:23:24 +09:00
done
echo ${total_size}
}
2015-12-14 22:15:18 +08:00
check_virt(){
_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)"
if _exists "dmidecode"; then
sys_manu="$(dmidecode -s system-manufacturer 2>/dev/null)"
sys_product="$(dmidecode -s system-product-name 2>/dev/null)"
sys_ver="$(dmidecode -s system-version 2>/dev/null)"
else
sys_manu=""
sys_product=""
sys_ver=""
fi
if grep -qa docker /proc/1/cgroup; then
virt="Docker"
elif grep -qa lxc /proc/1/cgroup; then
virt="LXC"
elif grep -qa container=lxc /proc/1/environ; then
virt="LXC"
elif [[ -f /proc/user_beancounters ]]; then
virt="OpenVZ"
elif [[ "${virtualx}" == *kvm-clock* ]]; then
virt="KVM"
elif [[ "${cname}" == *KVM* ]]; then
virt="KVM"
elif [[ "${cname}" == *QEMU* ]]; then
virt="KVM"
elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; then
virt="VMware"
elif [[ "${virtualx}" == *"Parallels Software International"* ]]; then
virt="Parallels"
elif [[ "${virtualx}" == *VirtualBox* ]]; then
virt="VirtualBox"
elif [[ -e /proc/xen ]]; then
2022-01-01 15:50:10 +09:00
if grep -q "control_d" "/proc/xen/capabilities" 2>/dev/null; then
virt="Xen-Dom0"
else
virt="Xen-DomU"
fi
elif [ -f "/sys/hypervisor/type" ] && grep -q "xen" "/sys/hypervisor/type"; then
virt="Xen"
elif [[ "${sys_manu}" == *"Microsoft Corporation"* ]]; then
if [[ "${sys_product}" == *"Virtual Machine"* ]]; then
if [[ "${sys_ver}" == *"7.0"* || "${sys_ver}" == *"Hyper-V" ]]; then
virt="Hyper-V"
else
virt="Microsoft Virtual Machine"
fi
fi
else
virt="Dedicated"
fi
}
ipv4_info() {
local org="$(wget -q -T10 -O- ipinfo.io/org)"
local city="$(wget -q -T10 -O- ipinfo.io/city)"
local country="$(wget -q -T10 -O- ipinfo.io/country)"
local region="$(wget -q -T10 -O- ipinfo.io/region)"
2022-01-01 21:28:37 +09:00
if [[ -n "$org" ]]; then
echo " Organization : $(_blue "$org")"
fi
if [[ -n "$city" && -n "country" ]]; then
echo " Location : $(_blue "$city / $country")"
fi
if [[ -n "$region" ]]; then
echo " Region : $(_yellow "$region")"
fi
if [[ -z "$org" ]]; then
echo " Region : $(_red "No ISP detected")"
fi
}
install_speedtest() {
if [ ! -e "./speedtest-cli/speedtest" ]; then
2022-01-01 21:28:37 +09:00
sys_bit=""
local sysarch="$(uname -m)"
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
local sysarch="$(arch)"
fi
if [ "${sysarch}" = "x86_64" ]; then
sys_bit="x86_64"
fi
if [ "${sysarch}" = "i386" ] || [ "${sysarch}" = "i686" ]; then
sys_bit="i386"
fi
if [ "${sysarch}" = "armv8" ] || [ "${sysarch}" = "armv8l" ] || [ "${sysarch}" = "aarch64" ] || [ "${sysarch}" = "arm64" ]; then
sys_bit="aarch64"
fi
if [ "${sysarch}" = "armv7" ] || [ "${sysarch}" = "armv7l" ]; then
sys_bit="armhf"
fi
if [ "${sysarch}" = "armv6" ]; then
sys_bit="armel"
fi
[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
2022-01-01 16:07:49 +09:00
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.1.1-linux-${sys_bit}.tgz"
url2="https://dl.lamp.sh/files/ookla-speedtest-1.1.1-linux-${sys_bit}.tgz"
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}
if [ $? -ne 0 ]; then
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}
[ $? -ne 0 ] && _red "Error: Failed to download speedtest-cli.\n" && exit 1
fi
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
rm -f speedtest.tgz
fi
}
2022-01-01 15:50:10 +09:00
print_intro() {
2022-01-01 21:28:37 +09:00
echo "-------------------- A Bench.sh Script By Teddysun -------------------"
echo " Intro : https://teddysun.com/444.html"
echo " Version : $(_green v2022-01-01)"
echo " Usage : $(_red "wget -qO- bench.sh | bash")"
2022-01-01 15:50:10 +09:00
}
# Get System information
2022-01-01 15:50:10 +09:00
get_system_info() {
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
freq=$( awk -F'[ :]' '/cpu MHz/ {print $4;exit}' /proc/cpuinfo )
ccache=$( awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
tram=$( LANG=C; free -m | awk '/Mem/ {print $2}' )
uram=$( LANG=C; free -m | awk '/Mem/ {print $3}' )
swap=$( LANG=C; free -m | awk '/Swap/ {print $2}' )
uswap=$( LANG=C; free -m | awk '/Swap/ {print $3}' )
up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime )
if _exists "w"; then
load=$( LANG=C; w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
elif _exists "uptime"; then
load=$( LANG=C; uptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
fi
opsy=$( get_opsy )
arch=$( uname -m )
if _exists "getconf"; then
lbit=$( getconf LONG_BIT )
else
echo ${arch} | grep -q "64" && lbit="64" || lbit="32"
fi
kern=$( uname -r )
2022-01-01 16:39:56 +09:00
disk_size1=($( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|devtmpfs|by-uuid|chroot|Filesystem|udev|docker|snapd' | awk '{print $2}' ))
disk_size2=($( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|devtmpfs|by-uuid|chroot|Filesystem|udev|docker|snapd' | awk '{print $3}' ))
2022-01-01 15:50:10 +09:00
disk_total_size=$( calc_disk "${disk_size1[@]}" )
disk_used_size=$( calc_disk "${disk_size2[@]}" )
tcpctrl=$( sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}' )
}
# Print System information
print_system_info() {
2022-01-01 21:28:37 +09:00
echo " CPU Model : $(_blue "$cname")"
echo " CPU Cores : $(_blue "$cores")"
if [ -n "$freq" ]; then
echo " CPU Frequency : $(_blue "$freq MHz")"
fi
if [ -n "$ccache" ]; then
echo " CPU Cache : $(_blue "$ccache")"
fi
echo " Total Disk : $(_yellow "$disk_total_size GB") $(_blue "($disk_used_size GB Used)")"
echo " Total Mem : $(_yellow "$tram MB") $(_blue "($uram MB Used)")"
echo " Total Swap : $(_blue "$swap MB ($uswap MB Used)")"
echo " System uptime : $(_blue "$up")"
echo " Load average : $(_blue "$load")"
echo " OS : $(_blue "$opsy")"
echo " Arch : $(_blue "$arch ($lbit Bit)")"
echo " Kernel : $(_blue "$kern")"
echo " TCP CC : $(_yellow "$tcpctrl")"
echo " Virtualization : $(_blue "$virt")"
2022-01-01 15:50:10 +09:00
}
print_io_test() {
freespace=$( df -m . | awk 'NR==2 {print $4}' )
if [ -z "${freespace}" ]; then
freespace=$( df -m . | awk 'NR==3 {print $3}' )
fi
if [ ${freespace} -gt 1024 ]; then
writemb=2048
io1=$( io_test ${writemb} )
2022-01-01 21:28:37 +09:00
echo " I/O Speed(1st run) : $(_yellow "$io1")"
2022-01-01 15:50:10 +09:00
io2=$( io_test ${writemb} )
2022-01-01 21:28:37 +09:00
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
2022-01-01 15:50:10 +09:00
io3=$( io_test ${writemb} )
2022-01-01 21:28:37 +09:00
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
2022-01-01 15:50:10 +09:00
ioraw1=$( echo $io1 | awk 'NR==1 {print $1}' )
[ "`echo $io1 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw1=$( awk 'BEGIN{print '$ioraw1' * 1024}' )
ioraw2=$( echo $io2 | awk 'NR==1 {print $1}' )
[ "`echo $io2 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw2=$( awk 'BEGIN{print '$ioraw2' * 1024}' )
ioraw3=$( echo $io3 | awk 'NR==1 {print $1}' )
[ "`echo $io3 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw3=$( awk 'BEGIN{print '$ioraw3' * 1024}' )
ioall=$( awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}' )
ioavg=$( awk 'BEGIN{printf "%.1f", '$ioall' / 3}' )
2022-01-01 21:28:37 +09:00
echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"
2022-01-01 15:50:10 +09:00
else
echo " $(_red "Not enough space for I/O Speed test!")"
fi
}
print_end_time() {
end_time=$(date +%s)
time=$(( ${end_time} - ${start_time} ))
if [ ${time} -gt 60 ]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
2022-01-01 21:28:37 +09:00
echo " Finished in : ${min} min ${sec} sec"
2022-01-01 15:50:10 +09:00
else
2022-01-01 21:28:37 +09:00
echo " Finished in : ${time} sec"
2022-01-01 15:50:10 +09:00
fi
date_time=$(date +%Y-%m-%d" "%H:%M:%S)
2022-01-01 21:28:37 +09:00
echo " Timestamp : $date_time"
2022-01-01 15:50:10 +09:00
}
2022-01-01 21:28:37 +09:00
! _exists "wget" && _red "Error: wget command not found.\n" && exit 1
! _exists "free" && _red "Error: free command not found.\n" && exit 1
2022-01-01 15:50:10 +09:00
start_time=$(date +%s)
check_virt
2016-11-24 22:23:24 +09:00
clear
2022-01-01 15:50:10 +09:00
print_intro
next
get_system_info
print_system_info
ipv4_info
2016-11-24 22:23:24 +09:00
next
2022-01-01 15:50:10 +09:00
print_io_test
2016-11-24 22:38:36 +09:00
next
install_speedtest && printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"
speed && rm -fr speedtest-cli
next
2022-01-01 15:50:10 +09:00
print_end_time
next