#!/usr/bin/env bash # vps-bench: a small, readable Linux VPS benchmark script. # Measures system info, CPU, disk and network download speed. # It only downloads public test files; it never uploads results anywhere. # # Project: https://github.com/PikoSystem/vps-bench # License: MIT set -u VERSION="1.0.0" DISK_SIZE_MB=1024 NET_SECONDS=10 RUN_CPU=1 RUN_DISK=1 RUN_NET=1 TEST_DIR="${PWD}" TMP_FILE="" # name|location|url (public test files hosted by the providers themselves) NET_TARGETS=( "Hetzner|Falkenstein, DE|https://fsn1-speed.hetzner.com/100MB.bin" "Hetzner|Nuremberg, DE|https://nbg1-speed.hetzner.com/100MB.bin" "Hetzner|Helsinki, FI|https://hel1-speed.hetzner.com/100MB.bin" "OVHcloud|Roubaix, FR|https://proof.ovh.net/files/100Mb.dat" "Hetzner|Ashburn, US|https://ash-speed.hetzner.com/100MB.bin" "Cloudflare|Nearest edge|https://speed.cloudflare.com/__down?bytes=50000000" ) if [ -t 1 ]; then B=$'\033[1m'; D=$'\033[2m'; G=$'\033[32m'; Y=$'\033[33m'; C=$'\033[36m'; R=$'\033[0m' else B=""; D=""; G=""; Y=""; C=""; R="" fi usage() { cat <&2; usage >&2; exit 2 ;; esac shift done case "${DISK_SIZE_MB}" in ''|*[!0-9]*) echo "--disk-size must be a number of MB" >&2; exit 2 ;; esac case "${NET_SECONDS}" in ''|*[!0-9]*) echo "--net-seconds must be a number" >&2; exit 2 ;; esac if [ "$(uname -s)" != "Linux" ]; then echo "vps-bench runs on Linux only." >&2 exit 1 fi cleanup() { [ -n "${TMP_FILE}" ] && rm -f "${TMP_FILE}"; } trap cleanup EXIT INT TERM have() { command -v "$1" >/dev/null 2>&1; } header() { printf '\n%s%s%s\n' "${B}${C}" "$1" "${R}"; printf '%s\n' "${D}$(printf '%.0s-' $(seq 1 60))${R}"; } row() { printf ' %-22s %s\n' "$1" "$2"; } human_kib() { awk -v k="$1" 'BEGIN { if (k >= 1048576) printf "%.1f GiB", k/1048576; else printf "%.0f MiB", k/1024 }' } # ---------------------------------------------------------------- system info system_info() { header "System" local os cpu cores mem swap virt disk up aes vmx os="unknown" if [ -r /etc/os-release ]; then # shellcheck disable=SC1091 os="$(. /etc/os-release && printf '%s' "${PRETTY_NAME:-${NAME:-unknown}}")" fi cpu="$(awk -F: '/model name/ { sub(/^[ \t]+/, "", $2); print $2; exit }' /proc/cpuinfo)" [ -z "${cpu}" ] && cpu="$(uname -m)" cores="$(nproc 2>/dev/null || grep -c ^processor /proc/cpuinfo)" mem="$(awk '/MemTotal/ { print $2 }' /proc/meminfo)" swap="$(awk '/SwapTotal/ { print $2 }' /proc/meminfo)" virt="unknown" if have systemd-detect-virt; then virt="$(systemd-detect-virt 2>/dev/null || true)" [ "${virt}" = "none" ] && virt="none (bare metal)" [ -z "${virt}" ] && virt="unknown" fi disk="$(df -h "${TEST_DIR}" 2>/dev/null | awk 'NR==2 { print $2 " total, " $4 " free" }')" up="$(uptime -p 2>/dev/null | sed 's/^up //')" grep -qw aes /proc/cpuinfo && aes="${G}yes${R}" || aes="${Y}no${R}" grep -qwE 'vmx|svm' /proc/cpuinfo && vmx="yes" || vmx="no" row "OS" "${os}" row "Kernel" "$(uname -r) ($(uname -m))" row "Virtualization" "${virt}" row "CPU" "${cpu}" row "CPU cores" "${cores}" row "AES-NI" "${aes}" row "Nested virt" "${vmx}" row "RAM" "$(human_kib "${mem}")" row "Swap" "$(human_kib "${swap}")" row "Disk (${TEST_DIR})" "${disk:-unknown}" row "Uptime" "${up:-unknown}" } # ---------------------------------------------------------------------- CPU cpu_test() { header "CPU" if have sysbench; then local single multi threads threads="$(nproc 2>/dev/null || echo 1)" single="$(sysbench cpu --threads=1 --time=10 run 2>/dev/null | awk '/events per second/ { print $4 }')" multi="$(sysbench cpu --threads="${threads}" --time=10 run 2>/dev/null | awk '/events per second/ { print $4 }')" row "sysbench 1 thread" "${single:-failed} events/s" row "sysbench ${threads} threads" "${multi:-failed} events/s" elif have openssl; then local out out="$(openssl speed -seconds 5 -bytes 16384 sha256 2>/dev/null | awk '/^sha256/ { print $NF }')" if [ -n "${out}" ]; then row "SHA-256, 1 thread" "$(awk -v v="${out%k}" 'BEGIN { printf "%.0f MB/s", v/1000 }')" else row "SHA-256, 1 thread" "failed" fi out="$(openssl speed -seconds 5 -bytes 16384 -evp aes-256-gcm 2>/dev/null | awk '/^(AES-256-GCM|aes-256-gcm)/ { print $NF }')" [ -n "${out}" ] && row "AES-256-GCM, 1 thread" "$(awk -v v="${out%k}" 'BEGIN { printf "%.0f MB/s", v/1000 }')" printf ' %s\n' "${D}Install sysbench for single and multi-thread scores.${R}" else row "Result" "skipped (install sysbench or openssl)" fi } # --------------------------------------------------------------------- disk dd_speed() { # prints the speed reported by dd, e.g. "512 MB/s" LC_ALL=C dd "$@" 2>&1 | awk -F, '/copied/ { gsub(/^ +/, "", $NF); print $NF }' } disk_test() { header "Disk" if [ ! -w "${TEST_DIR}" ]; then row "Result" "skipped (${TEST_DIR} is not writable)" return fi local free_kb free_kb="$(df -Pk "${TEST_DIR}" | awk 'NR==2 { print $4 }')" if [ "${free_kb:-0}" -lt $(( (DISK_SIZE_MB + 256) * 1024 )) ]; then row "Result" "skipped (not enough free space for ${DISK_SIZE_MB} MB)" return fi TMP_FILE="$(mktemp "${TEST_DIR}/.vps-bench.XXXXXX")" local w r w="$(dd_speed if=/dev/zero of="${TMP_FILE}" bs=1M count="${DISK_SIZE_MB}" conv=fdatasync)" row "Sequential write" "${w:-failed} (dd, ${DISK_SIZE_MB} MB, fdatasync)" if [ -w /proc/sys/vm/drop_caches ]; then sync; echo 3 > /proc/sys/vm/drop_caches r="$(dd_speed if="${TMP_FILE}" of=/dev/null bs=1M)" row "Sequential read" "${r:-failed} (dd, cache dropped)" else row "Sequential read" "skipped (run as root to drop the page cache first)" fi if have fio; then local iops iops="$(fio --name=randrw --filename="${TMP_FILE}" --rw=randrw --rwmixread=50 --bs=4k \ --iodepth=64 --ioengine=libaio --direct=1 --size=512M --runtime=15 --time_based \ --group_reporting --output-format=terse 2>/dev/null \ | awk -F';' '{ printf "%d read / %d write IOPS", $8, $49 }')" row "4K random 50/50 (fio)" "${iops:-failed}" else printf ' %s\n' "${D}Install fio for 4K random IOPS.${R}" fi rm -f "${TMP_FILE}"; TMP_FILE="" } # ------------------------------------------------------------------ network net_test() { header "Network download (max ${NET_SECONDS}s each)" if ! have curl; then row "Result" "skipped (curl is not installed)" return fi printf ' %s%-10s %-18s %14s %10s%s\n' "${B}" "Provider" "Location" "Speed" "Connect" "${R}" local t name loc url out speed conn code rc reason for t in "${NET_TARGETS[@]}"; do IFS='|' read -r name loc url <<<"${t}" out="$(curl -4 -s -o /dev/null --connect-timeout 5 --max-time "${NET_SECONDS}" \ -w '%{speed_download} %{time_connect} %{http_code}' "${url}" 2>/dev/null; printf ' %s' "$?")" read -r speed conn code rc <<<"${out}" # exit code 28 only means the time limit was reached, which is expected if [ "${code:-000}" = "200" ] && [ "${speed%.*}" -gt 0 ] 2>/dev/null; then printf ' %-10s %-18s %14s %10s\n' "${name}" "${loc}" \ "$(awk -v s="${speed}" 'BEGIN { m = s*8/1000000; if (m < 10) printf "%.1f Mbit/s", m; else printf "%.0f Mbit/s", m }')" \ "$(awk -v c="${conn}" 'BEGIN { printf "%.0f ms", c*1000 }')" else case "${rc:-1}" in 6) reason="DNS failed" ;; 7) reason="no connection" ;; 28) reason="timeout" ;; 35) reason="TLS error" ;; 52) reason="empty reply" ;; 0) reason="HTTP ${code}" ;; *) reason="error ${rc}" ;; esac printf ' %-10s %-18s %14s %10s\n' "${name}" "${loc}" "${reason}" "-" fi done } # --------------------------------------------------------------------- main printf '%svps-bench %s%s %shttps://github.com/PikoSystem/vps-bench%s\n' "${B}" "${VERSION}" "${R}" "${D}" "${R}" printf '%sStarted %s%s\n' "${D}" "$(date -u '+%Y-%m-%d %H:%M UTC')" "${R}" system_info [ "${RUN_CPU}" -eq 1 ] && cpu_test [ "${RUN_DISK}" -eq 1 ] && disk_test [ "${RUN_NET}" -eq 1 ] && net_test printf '\n%sDone.%s Results are a snapshot: run a few times at different hours before comparing providers.\n' "${G}" "${R}"