目录
服务器性能测试在Linux系统中是一项重要的任务,它可以帮助你了解服务器在不同负载下的表现,从而优化服务器配置和资源分配。以下是关于Linux服务器性能测试的基础概念、优势、类型、应用场景以及常见问题的解答。
服务器性能测试通常涉及对CPU、内存、磁盘I/O、网络带宽等资源的测试。这些测试可以帮助你确定服务器的最大承载能力,以及在特定负载下的响应时间和吞吐量。
bench.sh 是一个 Linux 系统性能基准测试工具。可以快速给出服务器的整体配置信息,IO 性能,网络性能。很多人使用它测试 vps 性能。再配合 unixbench.sh 脚本测试,即可全面测试 VPS 的性能。
本地测试服务器配置:2核2G,3M带宽,40G硬盘。
测试结果
使用以下命令运行测试
wget -qO- bench.sh | bash
代码解释:`wget`:用于从指定URL下载文件`-qO-`:参数用于不输出下载过程信息,将文件输出到标准输出`| bash`:将下载内容通过管道传递给bash执行
结果如下图:
bench.sh主要功能
bench.sh的优势
与其他系统检测工具相比,bench.sh具有以下优势:
bench.sh使用方法
国外VPS云服务器
使用bench.sh脚本非常简单,只需要在终端中执行以下命令:
wget -qO- bench.sh | bash
或者
curl -Lso- bench.sh | bash
执行后,脚本会自动运行并显示系统各项信息。‘’
国内VPS云服务器
wget -qO-| bash
在国内,可以从 GitHub下载脚本()到本地,或者复制下面的脚本代码,保存到 bench.sh 文件中
#!/usr/bin/env bash## Description: A Bench Script by Teddysun## Copyright (C) 2015 - 2025 Teddysun# Thanks: LookBack# URL:HTTPS://github.com/teddysun/across/blob/master/bench.sh#trap _exit INT QUIT TERM_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"}_exists() {local cmd="$1"if eval type type >/dev/null 2>&1; theneval type "$cmd" >/dev/null 2>&1elif command >/dev/null 2>&1; thencommand -v "$cmd" >/dev/null 2>&1elsewhich "$cmd" >/dev/null 2>&1filocal rt=$?return ${rt}}_exit() {_red "\nThe script has been terminated. Cleaning up files...\n"# clean uprm -fr speedtest.tgz speedtest-cli benchtest_*exit 1}get_opsy() {[ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return[ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return[ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return}next() {printf "%-70s\n" "-" | sed 's/\s/-/g'}speed_test() {local nodeName="$2"if [ -z "$1" ];then./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1else./speedtest-cli/speedtest --progress=no --server-id="$1" --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1fiif [ $? -eq 0 ]; thenlocal dl_speed up_speed latencydl_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}" ]]; thenprintf "\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}"fifi}speed() {speed_test '' 'Speedtest.net'speed_test '21541' 'Los Angeles, US'speed_test '43860' 'Dallas, US'speed_test '40879' 'Montreal, CA'speed_test '61933' 'Paris, FR'speed_test '28922' 'Amsterdam, NL'speed_test '25858' 'Beijing, CN'speed_test '24447' 'Shanghai, CN'speed_test '60572' 'Guangzhou, CN'speed_test '32155' 'Hong Kong, CN'speed_test '13623' 'Singapore, SG'speed_test '48463' 'Tokyo, JP'}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]*$//'}calc_size() {local raw=$1local total_size=0local num=1local unit="KB"if ! [[ ${raw} =~ ^[0-9]+$ ]]; thenecho ""returnfiif [ "${raw}" -ge 1073741824 ]; thennum=1073741824unit="TB"elif [ "${raw}" -ge 1048576 ]; thennum=1048576unit="GB"elif [ "${raw}" -ge 1024 ]; thennum=1024unit="MB"elif [ "${raw}" -eq 0 ]; thenecho "${total_size}"returnfitotal_size=$(awk 'BEGIN{printf "%.1f", '"$raw"' / '$num'}')echo "${total_size} ${unit}"}# since calc_size converts kilobyte to MB, GB and TB# to_kibyte converts zfs size from bytes to kilobyteto_kibyte() {local raw=$1awk 'BEGIN{printf "%.0f", '"$raw"' / 1024}'}calc_sum() {local arr=("$@")local ss=0for i in "${arr[@]}"; dos=$((s + i))doneecho ${s}}check_virt() {_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)"if _exists "dmidecode"; thensys_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)"elsesys_manu=""sys_product=""sys_ver=""fiif grep -qa docker /proc/1/cgroup; thenvirt="Docker"elif grep -qa lxc /proc/1/cgroup; thenvirt="LXC"elif grep -qa container=lxc /proc/1/environ; thenvirt="LXC"elif [[ -f /proc/user_beancounters ]]; thenvirt="OpenVZ"elif [[ "${virtualx}" == *kvm-clock* ]]; thenvirt="KVM"elif [[ "${sys_product}" == *KVM* ]]; thenvirt="KVM"elif [[ "${sys_manu}" == *QEMU* ]]; thenvirt="KVM"elif [[ "${cname}" == *KVM* ]]; thenvirt="KVM"elif [[ "${cname}" == *QEMU* ]]; thenvirt="KVM"elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; thenvirt="VMware"elif [[ "${sys_product}" == *"VMware Virtual Platform"* ]]; thenvirt="VMware"elif [[ "${virtualx}" == *"Parallels Software International"* ]]; thenvirt="Parallels"elif [[ "${virtualx}" == *VirtualBox* ]]; thenvirt="VirtualBox"elif [[ -e /proc/xen ]]; thenif grep -q "control_d" "/proc/xen/capabilities" 2>/dev/null; thenvirt="Xen-Dom0"elsevirt="Xen-DomU"fielif [ -f "/sys/hypervisor/type" ] && grep -q "xen" "/sys/hypervisor/type"; thenvirt="Xen"elif [[ "${sys_manu}" == *"Microsoft Corporation"* ]]; thenif [[ "${sys_product}" == *"Virtual Machine"* ]]; thenif [[ "${sys_ver}" == *"7.0"* || "${sys_ver}" == *"Hyper-V" ]]; thenvirt="Hyper-V"elsevirt="Microsoft Virtual Machine"fifielsevirt="Dedicated"fi}ipv4_info() {local org city country regionorg="$(wget -q -T10 -O-"city="$(wget -q -T10 -O-"country="$(wget -q -T10 -O-"region="$(wget -q -T10 -O-"if [[ -n "${org}" ]]; thenecho " Organization: $(_blue "${org}")"fiif [[ -n "${city}" && -n "${country}" ]]; thenecho " Location: $(_blue "${city} / ${country}")"fiif [[ -n "${region}" ]]; thenecho " Region: $(_yellow "${region}")"fiif [[ -z "${org}" ]]; thenecho " Region: $(_red "No ISP detected")"fi}install_speedtest() {if [ ! -e "./speedtest-cli/speedtest" ]; thensys_bit=""local sysarchsysarch="$(uname -m)"if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; thensysarch="$(arch)"fiif [ "${sysarch}" = "x86_64" ]; thensys_bit="x86_64"fiif [ "${sysarch}" = "i386" ] || [ "${sysarch}" = "i686" ]; thensys_bit="i386"fiif [ "${sysarch}" = "armv8" ] || [ "${sysarch}" = "armv8l" ] || [ "${sysarch}" = "aarch64" ] || [ "${sysarch}" = "arm64" ]; thensys_bit="aarch64"fiif [ "${sysarch}" = "armv7" ] || [ "${sysarch}" = "armv7l" ]; thensys_bit="armhf"fiif [ "${sysarch}" = "armv6" ]; thensys_bit="armel"fi[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1url1="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}; thenif ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then_red "Error: Failed to download speedtest-cli.\n" && exit 1fifimkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chMod +x ./speedtest-cli/speedtestrm -f speedtest.tgzfiprintf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"}print_intro() {echo "-------------------- A Bench.sh Script By Teddysun -------------------"echo " Version: $(_green v2025-05-08)"echo " Usage: $(_red "wget -qO- bench.sh | bash")"}# Get System informationget_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=Cfree | awk '/Mem/ {print $2}')tram=$(calc_size "$tram")uram=$(LANG=Cfree | awk '/Mem/ {print $3}')uram=$(calc_size "$uram")swap=$(LANG=Cfree | awk '/Swap/ {print $2}')swap=$(calc_size "$swap")uswap=$(LANG=Cfree | 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)if _exists "w"; thenload=$(LANG=Cw | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')elif _exists "uptime"; thenload=$(LANG=Cuptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//')fiopsy=$(get_opsy)arch=$(uname -m)if _exists "getconf"; thenlbit=$(getconf LONG_BIT)elseecho "${arch}" | grep -q "64" && lbit="64" || lbit="32"fikern=$(uname -r)in_kernel_no_swap_total_size=$(LANG=Cdf -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=Cdf -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}')}# Print System informationprint_system_info() {if [ -n "$cname" ]; thenecho " CPU Model: $(_blue "$cname")"elseecho " CPU Model: $(_blue "CPU model not detected")"fiif [ -n "$freq" ]; thenecho " CPU Cores: $(_blue "$cores @ $freq MHz")"elseecho " CPU Cores: $(_blue "$cores")"fiif [ -n "$ccache" ]; thenecho " CPU Cache: $(_blue "$ccache")"fiif [ -n "$cpu_aes" ]; thenecho " AES-NI: $(_green "\xe2\x9c\x93 Enabled")"elseecho " AES-NI: $(_red "\xe2\x9c\x97 Disabled")"fiif [ -n "$cpu_virt" ]; thenecho " VM-x/AMD-V: $(_green "\xe2\x9c\x93 Enabled")"elseecho " VM-x/AMD-V: $(_red "\xe2\x9c\x97 Disabled")"fiecho " Total Disk: $(_yellow "$disk_total_size") $(_blue "($disk_used_size Used)")"echo " Total Mem: $(_yellow "$tram") $(_blue "($uram Used)")"if [ "$swap" != "0" ]; thenecho " Total Swap: $(_blue "$swap ($uswap Used)")"fiecho " 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")"echo " IPv4/IPv6: $online"}print_io_test() {freespace=$(df -m . | awk 'NR==2 {print $4}')if [ -z "${freespace}" ]; thenfreespace=$(df -m . | awk 'NR==3 {print $3}')fiif [ "${freespace}" -gt 1024 ]; thenwritemb=2048io1=$(io_test ${writemb})echo " I/O Speed(1st run) : $(_yellow "$io1")"io2=$(io_test ${writemb})echo " I/O Speed(2nd run) : $(_yellow "$io2")"io3=$(io_test ${writemb})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}')echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"elseecho " $(_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 ]; thenmin=$((time / 60))sec=$((time % 60))echo " Finished in: ${min} min ${sec} sec"elseecho " Finished in: ${time} sec"fidate_time=$(date '+%Y-%m-%d %H:%M:%S %Z')echo " Timestamp: $date_time"}! _exists "wget" && _red "Error: wget command not found.\n" && exit 1! _exists "free" && _red "Error: free command not found.\n" && exit 1# check for curl/wget_exists "curl" && local_curl=true# 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)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")"start_time=$(date +%s)get_system_infocheck_virtclearprint_intronextprint_system_infoipv4_infonextprint_io_testnextinstall_speedtest && speed && rm -fr speedtest-clinextprint_end_timenext
文件赋予执行权限(
chmod +x bench.sh
),然后执行脚本
./bench.sh
chmod +x bench.sh./bench.sh
注意事项
使用bench.sh脚本时,请注意以下几点:
结语
bench.sh脚本是一个便捷而强大的Linux系统信息检测工具。通过使用这个一键测试脚本,系统管理员可以快速获取服务器的各项重要信息,为优化系统性能和解决潜在问题提供有力支持。无论是日常维护还是故障排查,bench.sh都是一个值得收藏的实用工具。














发表评论