across/bench.sh

432 lines
15 KiB
Bash
Raw Normal View History

2016-11-29 14:25:31 +09:00
#!/usr/bin/env bash
#
2022-06-01 21:33:27 +09:00
# Description: A Bench Script by Teddysun
2016-11-29 14:25:31 +09:00
#
2024-11-12 20:16:15 +09:00
# Copyright (C) 2015 - 2024 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() {
2023-06-11 10:00:13 +09:00
_red "\nThe script has been terminated. Cleaning up files...\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"
if [ -z "$1" ];then
./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
else
./speedtest-cli/speedtest --progress=no --server-id="$1" --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
fi
if [ $? -eq 0 ]; then
local dl_speed up_speed latency
dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
latency=$(awk '/Latency/{print $3" "$4}' ./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'
2024-11-12 20:16:15 +09:00
speed_test '25858' 'Beijing, CN'
2022-01-01 15:50:10 +09:00
speed_test '24447' 'Shanghai, CN'
2024-11-12 20:16:15 +09:00
speed_test '60794' 'Guangzhou, CN'
speed_test '32155' 'Hong Kong, CN'
speed_test '13623' 'Singapore, SG'
2024-11-12 20:16:15 +09:00
speed_test '50686' 'Tokyo, JP'
2015-09-09 10:39:03 +08:00
}
2015-12-03 14:33:33 +08:00
io_test() {
(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
}
calc_size() {
local raw=$1
2016-11-24 22:23:24 +09:00
local total_size=0
local num=1
local unit="KB"
if ! [[ ${raw} =~ ^[0-9]+$ ]]; then
echo ""
return
fi
if [ "${raw}" -ge 1073741824 ]; then
num=1073741824
unit="TB"
elif [ "${raw}" -ge 1048576 ]; then
num=1048576
unit="GB"
elif [ "${raw}" -ge 1024 ]; then
num=1024
unit="MB"
elif [ "${raw}" -eq 0 ]; then
echo "${total_size}"
return
fi
total_size=$(awk 'BEGIN{printf "%.1f", '"$raw"' / '$num'}')
echo "${total_size} ${unit}"
2016-11-24 22:23:24 +09:00
}
2015-12-14 22:15:18 +08:00
# since calc_size converts kilobyte to MB, GB and TB
# to_kibyte converts zfs size from bytes to kilobyte
to_kibyte() {
local raw=$1
awk 'BEGIN{printf "%.0f", '"$raw"' / 1024}'
}
calc_sum() {
local arr=("$@")
local s
s=0
for i in "${arr[@]}"; do
s=$((s + i))
done
echo ${s}
}
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 [[ "${sys_product}" == *KVM* ]]; then
virt="KVM"
elif [[ "${sys_manu}" == *QEMU* ]]; then
virt="KVM"
elif [[ "${cname}" == *KVM* ]]; then
virt="KVM"
elif [[ "${cname}" == *QEMU* ]]; then
virt="KVM"
elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; then
virt="VMware"
2022-06-01 21:33:27 +09:00
elif [[ "${sys_product}" == *"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 city country region
org="$(wget -q -T10 -O- ipinfo.io/org)"
city="$(wget -q -T10 -O- ipinfo.io/city)"
country="$(wget -q -T10 -O- ipinfo.io/country)"
region="$(wget -q -T10 -O- ipinfo.io/region)"
if [[ -n "${org}" ]]; then
echo " Organization : $(_blue "${org}")"
2022-01-01 21:28:37 +09:00
fi
if [[ -n "${city}" && -n "${country}" ]]; then
echo " Location : $(_blue "${city} / ${country}")"
2022-01-01 21:28:37 +09:00
fi
if [[ -n "${region}" ]]; then
echo " Region : $(_yellow "${region}")"
2022-01-01 21:28:37 +09:00
fi
if [[ -z "${org}" ]]; then
2022-01-01 21:28:37 +09:00
echo " Region : $(_red "No ISP detected")"
fi
}
install_speedtest() {
2022-01-02 21:32:29 +09:00
if [ ! -e "./speedtest-cli/speedtest" ]; then
2022-01-01 21:28:37 +09:00
sys_bit=""
local sysarch
sysarch="$(uname -m)"
2022-01-01 21:28:37 +09:00
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
sysarch="$(arch)"
2022-01-01 21:28:37 +09:00
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
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
url2="https://dl.lamp.sh/files/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}; then
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then
_red "Error: Failed to download speedtest-cli.\n" && exit 1
fi
fi
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
rm -f speedtest.tgz
fi
2022-06-01 21:33:27 +09:00
printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"
}
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 -------------------"
2024-11-12 20:16:15 +09:00
echo " Version : $(_green v2024-11-11)"
2022-01-01 21:28:37 +09:00
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: '/^processor/ {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]*$//')
cpu_aes=$(grep -i 'aes' /proc/cpuinfo)
cpu_virt=$(grep -Ei 'vmx|svm' /proc/cpuinfo)
tram=$(
LANG=C
free | awk '/Mem/ {print $2}'
)
tram=$(calc_size "$tram")
uram=$(
LANG=C
free | awk '/Mem/ {print $3}'
)
uram=$(calc_size "$uram")
swap=$(
LANG=C
free | awk '/Swap/ {print $2}'
)
swap=$(calc_size "$swap")
uswap=$(
LANG=C
free | awk '/Swap/ {print $3}'
)
uswap=$(calc_size "$uswap")
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)
2022-01-01 15:50:10 +09:00
if _exists "w"; then
load=$(
LANG=C
w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//'
)
2022-01-01 15:50:10 +09:00
elif _exists "uptime"; then
load=$(
LANG=C
uptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//'
)
2022-01-01 15:50:10 +09:00
fi
opsy=$(get_opsy)
arch=$(uname -m)
2022-01-01 15:50:10 +09:00
if _exists "getconf"; then
lbit=$(getconf LONG_BIT)
2022-01-01 15:50:10 +09:00
else
echo "${arch}" | grep -q "64" && lbit="64" || lbit="32"
2022-01-01 15:50:10 +09:00
fi
kern=$(uname -r)
in_kernel_no_swap_total_size=$(
LANG=C
df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $2 }'
)
swap_total_size=$(free -k | grep Swap | awk '{print $2}')
zfs_total_size=$(to_kibyte "$(calc_sum "$(zpool list -o size -Hp 2> /dev/null)")")
disk_total_size=$(calc_size $((swap_total_size + in_kernel_no_swap_total_size + zfs_total_size)))
in_kernel_no_swap_used_size=$(
LANG=C
df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $3 }'
)
swap_used_size=$(free -k | grep Swap | awk '{print $3}')
zfs_used_size=$(to_kibyte "$(calc_sum "$(zpool list -o allocated -Hp 2> /dev/null)")")
disk_used_size=$(calc_size $((swap_used_size + in_kernel_no_swap_used_size + zfs_used_size)))
tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}')
2022-01-01 15:50:10 +09:00
}
# Print System information
print_system_info() {
2022-01-02 21:44:51 +09:00
if [ -n "$cname" ]; then
echo " CPU Model : $(_blue "$cname")"
else
echo " CPU Model : $(_blue "CPU model not detected")"
fi
2022-01-01 21:28:37 +09:00
if [ -n "$freq" ]; then
2022-02-24 09:39:57 +09:00
echo " CPU Cores : $(_blue "$cores @ $freq MHz")"
else
echo " CPU Cores : $(_blue "$cores")"
2022-01-01 21:28:37 +09:00
fi
if [ -n "$ccache" ]; then
2022-01-02 21:32:29 +09:00
echo " CPU Cache : $(_blue "$ccache")"
2022-01-01 21:28:37 +09:00
fi
if [ -n "$cpu_aes" ]; then
echo " AES-NI : $(_green "\xe2\x9c\x93 Enabled")"
else
echo " AES-NI : $(_red "\xe2\x9c\x97 Disabled")"
fi
if [ -n "$cpu_virt" ]; then
echo " VM-x/AMD-V : $(_green "\xe2\x9c\x93 Enabled")"
else
echo " VM-x/AMD-V : $(_red "\xe2\x9c\x97 Disabled")"
fi
echo " Total Disk : $(_yellow "$disk_total_size") $(_blue "($disk_used_size Used)")"
echo " Total Mem : $(_yellow "$tram") $(_blue "($uram Used)")"
if [ "$swap" != "0" ]; then
echo " Total Swap : $(_blue "$swap ($uswap Used)")"
fi
2022-01-01 21:28:37 +09:00
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")"
2023-06-11 10:00:13 +09:00
echo " IPv4/IPv6 : $online"
2022-01-01 15:50:10 +09:00
}
print_io_test() {
freespace=$(df -m . | awk 'NR==2 {print $4}')
2022-01-01 15:50:10 +09:00
if [ -z "${freespace}" ]; then
freespace=$(df -m . | awk 'NR==3 {print $3}')
2022-01-01 15:50:10 +09:00
fi
if [ "${freespace}" -gt 1024 ]; then
2022-01-01 15:50:10 +09:00
writemb=2048
io1=$(io_test ${writemb})
2022-01-01 21:28:37 +09:00
echo " I/O Speed(1st run) : $(_yellow "$io1")"
io2=$(io_test ${writemb})
2022-01-01 21:28:37 +09:00
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
io3=$(io_test ${writemb})
2022-01-01 21:28:37 +09:00
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
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))
2022-01-01 15:50:10 +09:00
if [ ${time} -gt 60 ]; then
min=$((time / 60))
sec=$((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 %Z')
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
2023-06-11 10:00:13 +09:00
# check for curl/wget
_exists "curl" && local_curl=true
2023-06-11 10:00:13 +09:00
# test if the host has IPv4/IPv6 connectivity
[[ -n ${local_curl} ]] && ip_check_cmd="curl -s -m 4" || ip_check_cmd="wget -qO- -T 4"
ipv4_check=$( (ping -4 -c 1 -W 4 ipv4.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -4 icanhazip.com 2> /dev/null)
ipv6_check=$( (ping -6 -c 1 -W 4 ipv6.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -6 icanhazip.com 2> /dev/null)
2023-06-11 10:00:13 +09:00
if [[ -z "$ipv4_check" && -z "$ipv6_check" ]]; then
_yellow "Warning: Both IPv4 and IPv6 connectivity were not detected.\n"
fi
[[ -z "$ipv4_check" ]] && online="$(_red "\xe2\x9c\x97 Offline")" || online="$(_green "\xe2\x9c\x93 Online")"
[[ -z "$ipv6_check" ]] && online+=" / $(_red "\xe2\x9c\x97 Offline")" || online+=" / $(_green "\xe2\x9c\x93 Online")"
2022-01-01 15:50:10 +09:00
start_time=$(date +%s)
2022-01-02 21:32:29 +09:00
get_system_info
check_virt
2016-11-24 22:23:24 +09:00
clear
2022-01-01 15:50:10 +09:00
print_intro
next
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
2022-06-01 21:33:27 +09:00
install_speedtest && speed && rm -fr speedtest-cli
next
2022-01-01 15:50:10 +09:00
print_end_time
next