├── .gitignore ├── INSTALL ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.md ├── install-serverscope.sh ├── packaging ├── deb │ └── build_deb.sh └── rpm │ ├── build_rpm.sh │ ├── python-serverscope_benchmark.spec.el8 │ └── python3-serverscope_benchmark.spec.el7 ├── serverscope_benchmark ├── __init__.py ├── __main__.py ├── benchmarks.py ├── cli.py ├── server.py └── utils.py ├── setup.py ├── tests ├── general-test-deb.sh └── general-test-el7_el8.sh └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw[po] 2 | *.pyc 3 | /build/ 4 | /dist/ 5 | /serverscope_benchmark.egg-info/ 6 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | ====================== 2 | 3 | Currently serverscope-benchmark supports following distro: 4 | 5 | * Debian 9, 10 6 | * CentOS 7, 8 7 | * Ubuntu 16.04, 18.04, 20.04, 20.10, 21.04 8 | 9 | Use https://github.com/serverscope/serverscope-benchmark/raw/master/install-serverscope.sh 10 | to install serverscope-benchmark package in your system. 11 | 12 | Some notes about install-serverscope.sh: 13 | 14 | ---------------------- 15 | CentOS notes: 16 | 17 | * CentOS 7 enables uses EPEL repo in order to satisfy runtime dependencies (python36 from EPEL) 18 | 19 | ---------------------- 20 | Ubuntu notes: 21 | 22 | * Ubuntu 16.04 installs additional python3-distro in order to satisfy distro missing dep 23 | 24 | ====================== 25 | 26 | You can also build the packages manually. 27 | Clone the project from 28 | 29 | https://github.com/serverscope/serverscope-benchmark.git 30 | 31 | and use: 32 | 33 | make rpm (make deb) 34 | 35 | ====================== 36 | All other distributions may manually install following packages in your system: 37 | 38 | fio, gcc, make, which, python3, perl, python3-distro, python3-requests, python3-setuptools 39 | 40 | Clone the project from 41 | 42 | https://github.com/serverscope/serverscope-benchmark.git 43 | 44 | and use: 45 | 46 | python3 setup.py install 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 serverscope.io 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | include LICENSE 3 | recursive-include serverscope_benchmark *.py 4 | recursive-exclude * *.pyc 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | DEB_PKGR='packaging/deb/build_deb.sh' 3 | DEB_CHECKER='tests/general-test-deb.sh' 4 | RPM_PKGR='packaging/rpm/build_rpm.sh' 5 | RPM_CHECKER='tests/general-test-el7_el8.sh' 6 | 7 | all: 8 | 9 | rpm: 10 | $(RPM_PKGR) 11 | 12 | deb: 13 | $(DEB_PKGR) 14 | cp deb_dist/python3-serverscope-benchmark*deb ./ 15 | 16 | check-rpm: 17 | $(RPM_CHECKER) 18 | 19 | check-deb: 20 | $(DEB_CHECKER) 21 | 22 | .PHONY: clean 23 | clean: 24 | rm -rf deb_dist/ dist/ serverscope_benchmark.egg-info/ build/ 25 | rm -f python3-serverscope-benchmark*.deb serverscope_benchmark*.tar.gz python*.noarch.rpm 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | serverscope.io benchmark tool 2 | ============================= 3 | 4 | A benchmarking suite focused on processing speed, I/O performance and 5 | network bandwidth. 6 | 7 | Install 8 | ------- 9 | 10 | pip3 install serverscope-benchmark 11 | 12 | Alternatively, go to [serverscope.io](https://serverscope.io/) and 13 | generate a command line that will install all dependencies and run it. 14 | 15 | Run 16 | --- 17 | 18 | To run all benchmarks: 19 | 20 | python3 -m serverscope_benchmark -e "youremail@yourdomain.com" -p "Plan|Hosting provider" 21 | 22 | To run only one or more benchmarks: 23 | 24 | python3 -m serverscope_benchmark -e "youremail@yourdomain.com" -p "Plan|Hosting provider" -i BENCHMARKS 25 | 26 | where BENCHMARKS is comma separated list of possible benchmarks: speedtest,download,dd,fio,unixbench 27 | 28 | For example, to run only dd and fio benchmarks, run it like this: 29 | 30 | python3 -m serverscope_benchmark -e "youremail@yourdomain.com" -p "Plan|Hosting provider" -i dd,fio 31 | 32 | After running it, the results will be posted to 33 | [serverscope.io](https://serverscope.io/) and you will get a link to 34 | your report. 35 | 36 | License 37 | ------- 38 | 39 | MIT 40 | -------------------------------------------------------------------------------- /install-serverscope.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Installs serverscope 4 | 5 | CENTOS8_PKG="https://serverscope.io/packages/rpm/python3-serverscope_benchmark-2.0.1-4.el8.noarch.rpm" 6 | CENTOS7_PKG="https://serverscope.io/packages/rpm/python36-serverscope_benchmark-2.0.1-2.el7.noarch.rpm" 7 | DEB_PKG="https://serverscope.io/packages/deb/python3-serverscope-benchmark_2.0.1.1-1_all.deb" 8 | UBUNTU_16_04_DEP="https://serverscope.io/packages/deb/python3-distro_1.0.1-2_all.deb" 9 | 10 | # process command line switches 11 | _email= 12 | _plan= 13 | _included_benchmarks=all 14 | 15 | while getopts "e:p:i:" opt; do 16 | case $opt in 17 | e) _email=$OPTARG;; 18 | p) _plan=$OPTARG;; 19 | i) _included_benchmarks=$OPTARG;; 20 | esac 21 | done 22 | shift $((OPTIND - 1)) 23 | 24 | __install_deb_url() { 25 | PKGS="" 26 | apt update -y 27 | for x in $@; do 28 | TEMP_DEB="$(mktemp --suffix ".deb")" 29 | wget -O "$TEMP_DEB" $x 30 | chmod 444 $TEMP_DEB 31 | PKGS="$PKGS $TEMP_DEB" 32 | done 33 | apt install -y $PKGS 34 | rm -f "$PKGS" 35 | } 36 | 37 | # try hard to determine locale 38 | LOCALER="$(locale -a | grep -i '^c.utf' | head -1)" 39 | if [ -z "$LOCALER" ]; then 40 | LOCALER="$(locale -a | grep -i '^en_us.utf' | head -1)" 41 | if [ -z "$LOCALER" ]; then 42 | echo "No any acceptable UTF8 locale has been found, add support of C.UTF8 first" 43 | exit 1 44 | fi 45 | fi 46 | 47 | LANG_PREFIX="LC_ALL=\"$LOCALER\"" 48 | echo "Using locale: $LANG_PREFIX" 49 | 50 | SS_BENCH_CMD="$LANG_PREFIX python3 -m serverscope_benchmark -e \"$_email\" -p \"$_plan\" -i \"$_included_benchmarks\"" 51 | 52 | source /etc/os-release 53 | if [ "$NAME" == "CentOS Linux" ]; then 54 | echo "Detected $NAME" 55 | if [ "$VERSION_ID" == "8" ]; then 56 | dnf install -y $CENTOS8_PKG 57 | elif [ "$VERSION_ID" == "7" ]; then 58 | yum install -y epel-release 59 | yum install -y $CENTOS7_PKG 60 | else 61 | echo "Only packages for CentOS Linux 7/8 are available for installation" 62 | exit 1 63 | fi 64 | elif [ "$NAME" == "Ubuntu" ]; then 65 | echo "Detected $NAME" 66 | if [ "$VERSION_CODENAME" == "xenial" ]; then 67 | __install_deb_url $UBUNTU_16_04_DEP $DEB_PKG 68 | elif [ "$VERSION_CODENAME" == "bionic" ] || \ 69 | [ "$VERSION_CODENAME" == "focal" ] || \ 70 | [ "$VERSION_CODENAME" == "groovy" ] || \ 71 | [ "$VERSION_CODENAME" == "hirsute" ]; then 72 | __install_deb_url $DEB_PKG 73 | else 74 | echo "Only packages for Ubuntu 16.04/18.04/20.04/20.10/21.04 are available for installation" 75 | exit 1 76 | fi 77 | elif [ "$NAME" == "Debian GNU/Linux" ]; then 78 | if [ "$VERSION_ID" == "9" ] || \ 79 | [ "$VERSION_ID" == "10" ]; then 80 | __install_deb_url $DEB_PKG 81 | else 82 | echo "Only packages for Debian 9/10 are available for installation" 83 | exit 1 84 | fi 85 | else 86 | echo "Your distro $NAME $VERSION_ID currently is not supported by script" 87 | echo "You might manually install [gcc, make, perl, python3 >= 3.5, curl, python3-setuptools, fio] + 'pip3 install serverscope-benchmark'" 88 | echo "And run: '$SS_BENCH_CMD'" 89 | exit 1 90 | fi 91 | 92 | # run serverscope_benchmark 93 | if [ -z "$_plan" ] || [ -z "$_email" ]; then 94 | echo Run serverscope manually: 95 | echo 96 | echo " python3 -m serverscope_benchmark -e \"youremail@yourdomain.com\" -p \"Plan\|Hosting provider\"" 97 | echo 98 | else 99 | bash -c "$SS_BENCH_CMD" 100 | fi 101 | 102 | exit 0 103 | -------------------------------------------------------------------------------- /packaging/deb/build_deb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sudo apt-get install python3-all python-all dh-python python3-stem python3-stdeb -y 4 | 5 | DIRA=$(dirname "$0") 6 | 7 | pushd $DIRA/../../ 8 | rm -rf deb_dist/ 9 | rm -rf dist/ 10 | python3 setup.py --command-packages=stdeb.command sdist_dsc --depends3='curl, fio, make, gcc, perl' --upstream-version-suffix='.1' 11 | cd $(ls -d to deb_dist/serverscope-benchmark-*/) 12 | dpkg-buildpackage -rfakeroot -uc -us 13 | popd 14 | -------------------------------------------------------------------------------- /packaging/rpm/build_rpm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # universal el7/el8 package builder script 4 | 5 | 6 | if [ "$#" -eq 1 ]; then 7 | if [ "$1" == "8" -o "$1" == "7" ];then 8 | DISTRO="el$1" 9 | VERSION=$1 10 | else 11 | echo "Invalid distro: $1, must be 7 or 8" 12 | exit 1 13 | fi 14 | else 15 | source /etc/os-release 16 | DISTRO="el$VERSION_ID" 17 | VERSION="$VERSION_ID" 18 | fi 19 | 20 | echo "Building for version $DISTRO" 21 | 22 | sudo yum install python3-setuptools mock -y 23 | 24 | DIRA=$(dirname "$0") 25 | 26 | pushd $DIRA/../../ 27 | rm -rf dist/ 28 | python3 setup.py sdist 29 | pushd dist/ 30 | mkdir {SPECS,SOURCES} 31 | cp ./serverscope_benchmark*tar.gz SOURCES/ 32 | cp ../packaging/rpm/python*spec.$DISTRO SPECS/ 33 | NORM_SPEC=$(ls ./SPECS/*spec.$DISTRO) 34 | NORM_SPEC=${NORM_SPEC%.$DISTRO} 35 | mv SPECS/*spec.$DISTRO $NORM_SPEC 36 | sudo mock -r epel-$VERSION-x86_64 --resultdir ./ --source SOURCES/ --spec SPECS/*.spec 37 | cp ./*noarch.rpm ../ 38 | echo "Built: $(ls ./*noarch.rpm)" 39 | popd 40 | popd 41 | -------------------------------------------------------------------------------- /packaging/rpm/python-serverscope_benchmark.spec.el8: -------------------------------------------------------------------------------- 1 | %global pversion 2.0.1 2 | %global prelease 4 3 | 4 | %global modname serverscope_benchmark 5 | %global build_wheel 0 6 | 7 | %bcond_without python3 8 | 9 | %global python3_wheelname %{modname}-%{version}-py3-none-any.whl 10 | 11 | Epoch: 1 12 | Name: python-%{modname} 13 | Version: %{pversion} 14 | Release: %{prelease}%{?dist} 15 | Summary: A benchmarking suite 16 | 17 | License: MIT 18 | URL: https://pypi.org/project/serverscope-benchmark/ 19 | Source0: https://files.pythonhosted.org/packages/c5/87/0054f51fdfdac911862a0d1138944085312b836f9dbc5e3f623ab09c603a/serverscope_benchmark-%{version}.tar.gz 20 | 21 | BuildArch: noarch 22 | 23 | %global _description \ 24 | A benchmarking suite focused on processing speed, I/O performance and \ 25 | network bandwidth. 26 | 27 | %description %{_description} 28 | 29 | %if %{with python3} 30 | %package -n python3-%{modname} 31 | Summary: %{summary} 32 | %{?python_provide:%python_provide python3-%{modname}} 33 | Obsoletes: platform-python-%{modname} < %{version}-%{release} 34 | BuildRequires: python3-devel 35 | BuildRequires: python3-setuptools 36 | 37 | %if 0%{?build_wheel} 38 | BuildRequires: python%{python3_pkgversion}-pip 39 | BuildRequires: python%{python3_pkgversion}-wheel 40 | %endif 41 | 42 | Requires: fio 43 | Requires: gcc 44 | Requires: make 45 | Requires: which 46 | Requires: python3 47 | Requires: perl 48 | Requires: python%{python3_pkgversion}-requests 49 | # needs epel8 50 | Requires: python%{python3_pkgversion}-distro 51 | 52 | %description -n python3-%{modname} %{_description} 53 | Python 3 version. 54 | 55 | %endif 56 | 57 | 58 | %prep 59 | %autosetup -n %{modname}-%{version} 60 | 61 | 62 | %build 63 | %if %{with python3} 64 | %if 0%{?build_wheel} 65 | %py3_build_wheel 66 | %else 67 | %py3_build 68 | %endif 69 | %endif 70 | 71 | 72 | %install 73 | %if %{with python3} 74 | %if 0%{?build_wheel} 75 | %py3_install_wheel %{python3_wheelname} 76 | %else 77 | %py3_install 78 | %endif 79 | %endif 80 | 81 | 82 | %if %{with python3} 83 | %files -n python3-%{modname} 84 | %license LICENSE 85 | %doc README.md 86 | %if 0%{?build_wheel} 87 | %{python3_sitelib}/%{modname}-*.dist-info/ 88 | %endif 89 | %{python3_sitelib}/%{modname}* 90 | %endif 91 | 92 | 93 | %changelog 94 | * Mon Apr 12 2021 Mikhail Campos - 1:2.0.1-4 95 | - make fio to consume less memory 96 | 97 | * Thu Apr 08 2021 Mikhail Campos - 1:2.0.1-3 98 | - adding missing perl dep 99 | 100 | * Thu Apr 08 2021 Mikhail Campos - 1:2.0.1-2 101 | - adding missing python3 dep 102 | 103 | * Fri Apr 02 2021 Mikhail Campos - 1:2.0.1-1 104 | - version bump up to 2.0.1 105 | 106 | * Fri Mar 26 2021 Mikhail Campos - 1:1.0.4-2 107 | - switching to use the system fio requirement 108 | 109 | * Thu Mar 25 2021 Mikhail Campos - 1:1.0.4-1 110 | - initial packaging 111 | -------------------------------------------------------------------------------- /packaging/rpm/python3-serverscope_benchmark.spec.el7: -------------------------------------------------------------------------------- 1 | %global pversion 2.0.1 2 | %global prelease 2 3 | 4 | Name: python3-serverscope_benchmark 5 | Version: %{pversion} 6 | Release: %{prelease}%{?dist} 7 | Summary: A benchmarking suite 8 | 9 | License: MIT 10 | URL: https://pypi.org/project/serverscope-benchmark/ 11 | Source0: https://files.pythonhosted.org/packages/c5/87/0054f51fdfdac911862a0d1138944085312b836f9dbc5e3f623ab09c603a/serverscope_benchmark-%{version}.tar.gz 12 | 13 | BuildArch: noarch 14 | 15 | %bcond_with python3_other 16 | 17 | %description 18 | A benchmarking suite focused on processing speed, I/O performance and 19 | network bandwidth. 20 | 21 | %package -n python%{python3_pkgversion}-serverscope_benchmark 22 | Summary: A benchmarking suite 23 | BuildRequires: python%{python3_pkgversion}-devel 24 | 25 | Requires: fio 26 | Requires: gcc 27 | Requires: make 28 | Requires: which 29 | Requires: python%{python3_pkgversion}-devel 30 | Requires: python%{python3_pkgversion}-requests 31 | Requires: python%{python3_pkgversion}-distro 32 | 33 | 34 | %description -n python%{python3_pkgversion}-serverscope_benchmark 35 | A benchmarking suite focused on processing speed, I/O performance and 36 | network bandwidth. 37 | 38 | This is the Python %{python3_pkgversion} build of the module. 39 | 40 | 41 | %prep 42 | %setup -q -n serverscope_benchmark-%{version} 43 | 44 | 45 | %build 46 | %py3_build 47 | 48 | 49 | %install 50 | %py3_install 51 | 52 | %files -n python%{python3_pkgversion}-serverscope_benchmark 53 | %license LICENSE 54 | %doc README.md 55 | %{python3_sitelib}/serverscope_benchmark* 56 | 57 | 58 | %changelog 59 | * Mon Apr 12 2021 Mikhail Campos - 2.0.1-2 60 | - make fio to consume less memory 61 | 62 | * Fri Apr 02 2021 Mikhail Campos - 2.0.1-1 63 | - version bump to 2.0.1 64 | 65 | * Fri Mar 26 2021 Mikhail Campos - 1.0.4-3 66 | - switching to use the system fio requirement 67 | 68 | * Wed Mar 24 2021 Mikhail Campos - 1.0.4-2 69 | - General code adaptation to python3.5+ 70 | 71 | * Tue Mar 23 2021 Mikhail Campos - 1.0.4-1 72 | - Initial package 73 | -------------------------------------------------------------------------------- /serverscope_benchmark/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = '2.0.1' 2 | -------------------------------------------------------------------------------- /serverscope_benchmark/__main__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Serverscope.io benchmark tool 5 | """ 6 | 7 | import os 8 | import sys 9 | import tempfile 10 | import shutil 11 | 12 | # Needed until support for old distros is done 13 | try: 14 | import distro 15 | get_dist = distro.linux_distribution 16 | except ImportError: 17 | if not (sys.version_info.major == 3 and sys.version_info.minor >= 8): 18 | import platform 19 | get_dist = platform.dist 20 | else: 21 | print('python3-distro is required, please install it either with pip3 or package manager') 22 | sys.exit(1) 23 | 24 | 25 | from .cli import get_parser 26 | from .benchmarks import get_selected_benchmark_classes 27 | from .utils import Color as c, get_geo_info, post_results, pushd 28 | from .server import get_server_specs 29 | 30 | 31 | if __name__ == '__main__': 32 | args = get_parser() 33 | 34 | payload = { 35 | "email": args["email"], "plan": args["plan"], "locale": args["locale"]} 36 | payload["os"] = get_dist() 37 | 38 | with tempfile.TemporaryDirectory(prefix='serverscope-', dir=os.getcwd()) as tmp_dir, pushd(tmp_dir): 39 | 40 | payload['version'] = sys.modules[sys.modules[__name__].__package__].__version__ 41 | payload['geo'] = get_geo_info() 42 | payload['specs'] = get_server_specs() 43 | 44 | benchmarks = {} 45 | print("", end=c.RESET) 46 | 47 | for BenchmarkClass in get_selected_benchmark_classes(args.get('include', None)): 48 | benchmark = BenchmarkClass(specs=payload['specs']) 49 | benchmark.download() 50 | result = benchmark.run() 51 | if result: 52 | benchmarks[benchmark.code] = result 53 | 54 | payload['benchmarks'] = benchmarks 55 | 56 | if payload.get('benchmarks', None): 57 | print(c.GREEN + c.BOLD) 58 | print("All done! Submitting the results..." + c.RESET) 59 | post_results(payload) 60 | -------------------------------------------------------------------------------- /serverscope_benchmark/benchmarks.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import sys 5 | import re 6 | import subprocess 7 | import tarfile 8 | from contextlib import redirect_stdout 9 | 10 | from .utils import Color as c, run_and_print 11 | from .server import get_total_ram 12 | 13 | 14 | class Benchmark: 15 | def __init__(self, specs): 16 | self.specs = specs 17 | 18 | def download(self): 19 | """Abstract method, may be overridden in child class.""" 20 | pass 21 | 22 | def run(self): 23 | """Abstract method, must be overridden in child class.""" 24 | raise NotImplementedError 25 | 26 | 27 | class SpeedtestBenchmark(Benchmark): 28 | code = 'speedtest' 29 | min_distance = 30.0 30 | serv_count = 15 31 | 32 | def _closest_servers(self): 33 | servers = [] 34 | i = 0 35 | 36 | with open(os.devnull,'w') as devnull, redirect_stdout(devnull): 37 | sp_list = run_and_print(["python3", "speedtest.py", "--list"]) 38 | print('Selecting %s servers that are not too close:' % self.serv_count) 39 | pattern = re.compile('(\d+)\).*\[(\d+.\d+) km\]') 40 | 41 | for line in sp_list.split('\n'): 42 | result = pattern.search(line) 43 | if not result: 44 | continue 45 | distance = result.group(2) 46 | if float(distance) > self.min_distance: 47 | servers.append(result.group(1)) 48 | print("%s. %s" % (i + 1, line)) 49 | i = i + 1 50 | if i >= self.serv_count: 51 | break 52 | 53 | return servers 54 | 55 | def download(self): 56 | url = 'https://raw.githubusercontent.com/serverscope/serverscope-tools/master/speedtest_py3.py' 57 | print(c.GREEN + 'Downloading bandwidth benchmark from %s ' % url + c.RESET) 58 | subprocess.call(['curl', '-s', '-L', '-o', 'speedtest.py', url], stdout=subprocess.DEVNULL) 59 | 60 | def run(self): 61 | print(c.GREEN + "Running speedtest benchmark:" + c.RESET) 62 | 63 | result = {} 64 | servers = self._closest_servers() 65 | if not servers: 66 | print(c.RED + "speedtest interface returns empty data, skipping benchmark..." + c.RESET) 67 | return result 68 | 69 | print('Testing upload speeds') 70 | 71 | for i, sp_serv in enumerate(servers): 72 | out = run_and_print(["python3", "speedtest.py", '--no-download', '--server', sp_serv, '--json' ]).replace("'", "'") 73 | if not out.startswith('{'): 74 | out = '' 75 | result[str(i + 1)] = out 76 | 77 | return result 78 | 79 | 80 | class DownloadBenchmark(Benchmark): 81 | code = 'download' 82 | 83 | def run(self): 84 | print(c.GREEN + "Running download benchmark:" + c.RESET) 85 | url = 'http://cachefly.cachefly.net/100mb.test' 86 | count = 5 87 | 88 | print(c.GREEN + " Downloading %s x%d" % (url, count) + c.RESET) 89 | 90 | curl = ["curl", "-o", "/dev/null", "--silent", "--progress-bar", 91 | "--write-out", 'Downloaded %{size_download} bytes in %{time_total} sec\n', 92 | url] 93 | result = [] 94 | size = 0 95 | time = 0 96 | 97 | for _ in range(count): 98 | s = run_and_print(curl) 99 | match = re.search(r"Downloaded\s+([0-9]+)\sbytes\sin\s([0-9.,]+)\ssec", s) 100 | if match: 101 | size += round(int(match.group(1)) / 1024 / 1024, 2) # megabytes 102 | try: 103 | time += float(match.group(2)) # sec 104 | except ValueError: 105 | time += float(match.group(2).replace(',', '.')) 106 | result.append(s) 107 | v = round(size * 8 / time, 2) 108 | r = "Finished! Average download speed is %.2f Mbit/s" % v 109 | result.append(r) 110 | print(c.GREEN + r + c.RESET) 111 | return "".join(result) 112 | 113 | 114 | class DDBenchmark(Benchmark): 115 | code = 'dd' 116 | 117 | def run(self): 118 | result = {} 119 | 120 | dd_size = 32 121 | cmd = [ 122 | 'dd', 'if=/dev/zero', 'of=benchmark', 123 | 'bs=64K', 'count=%sK' % dd_size, 'conv=fdatasync'] 124 | dd_str = ' '.join(cmd) 125 | print(c.GREEN + "Running dd as follows:\n " + dd_str + c.RESET) 126 | result["base64k"] = dd_str + "\n" + run_and_print(cmd) 127 | 128 | dd_size = dd_size * 64 129 | cmd = [ 130 | 'dd', 'if=/dev/zero', 'of=benchmark', 131 | 'bs=1M', 'count=%s' % dd_size, 'conv=fdatasync'] 132 | dd_str = ' '.join(cmd) 133 | print(c.GREEN + " " + dd_str + c.RESET) 134 | result["base1m"] = dd_str + "\n" + run_and_print(cmd) 135 | 136 | os.remove('benchmark') 137 | print("", end=c.RESET) 138 | 139 | return result 140 | 141 | 142 | class FioBenchmark(Benchmark): 143 | code = 'fio' 144 | 145 | def run(self): 146 | jobs = 8 147 | size = round(2048 / jobs) 148 | result = {} 149 | 150 | if not os.path.exists('/usr/bin/fio'): 151 | print("{}{}{}".format(c.ORANGE, 152 | "fio is not available, skipping. Please install fio package", 153 | c.RESET)) 154 | return result 155 | else: 156 | print(c.GREEN + 'Running IO tests:' + c.RESET) 157 | 158 | cmd = [ 159 | '/usr/bin/fio', '--time_based', '--name=benchmark', '--size=%dM' % size, 160 | '--runtime=60', '--randrepeat=1', 161 | '--invalidate=1', '--verify=0', 162 | '--verify_fatal=0', '--numjobs=%d' % jobs, '--rw=randread', '--blocksize=4k', 163 | '--group_reporting', '--output-format=json' 164 | ] 165 | result['random-read'] = " ".join(cmd) + "\n" + \ 166 | run_and_print(cmd) 167 | 168 | cmd = [ 169 | '/usr/bin/fio', '--time_based', '--name=benchmark', '--size=%dM' % size, 170 | '--runtime=60', '--randrepeat=1', 171 | '--direct=1', '--invalidate=1', '--verify=0', '--verify_fatal=0', 172 | '--numjobs=%d' % jobs, '--rw=randread', '--blocksize=4k', 173 | '--group_reporting', '--output-format=json' 174 | ] 175 | result['random-read-direct'] = " ".join(cmd) + "\n" + \ 176 | run_and_print(cmd) 177 | 178 | cmd = [ 179 | '/usr/bin/fio', '--time_based', '--name=benchmark', '--size=%dM' % size, 180 | '--runtime=60', '--filename=benchmark', 181 | '--randrepeat=1', '--direct=1', '--invalidate=1', 182 | '--verify=0', '--verify_fatal=0', '--numjobs=%d' % jobs, '--rw=randwrite', 183 | '--blocksize=4k', '--group_reporting', '--output-format=json' 184 | ] 185 | result['random-write-direct'] = " ".join(cmd) + "\n" + \ 186 | run_and_print(cmd) 187 | 188 | cmd = [ 189 | '/usr/bin/fio', '--time_based', '--name=benchmark', '--size=%dM' % size, '--runtime=60', 190 | '--filename=benchmark', '--randrepeat=1', 191 | '--end_fsync=1', '--invalidate=1', '--verify=0', 192 | '--verify_fatal=0', '--numjobs=%d' % jobs, '--rw=randwrite', '--blocksize=4k', 193 | '--group_reporting', '--output-format=json' 194 | ] 195 | result['random-write'] = " ".join(cmd) + "\n" + \ 196 | run_and_print(cmd) 197 | 198 | return result 199 | 200 | 201 | class UnixbenchBenchmark(Benchmark): 202 | code = 'unixbench' 203 | _unixbench_dir = './byte-unixbench' 204 | 205 | def download(self): 206 | url = 'https://github.com/serverscope/serverscope-tools/raw/master/unixbench-5.1.3-patched.tar.gz' # noqa 207 | print(c.GREEN + 'Downloading & running UnixBench from %s' % url + c.RESET) 208 | 209 | subprocess.call(['curl', '-s', '-L', '-o', 'unixbench.tar.gz', url], 210 | stdout=subprocess.DEVNULL) 211 | with tarfile.open("unixbench.tar.gz") as tar: 212 | tar.extractall() 213 | 214 | def run(self): 215 | # TODO: if failed while was runnning, the only stdout could show this 216 | return run_and_print(['./Run'], cwd='%s/UnixBench' % self._unixbench_dir) 217 | 218 | 219 | class DummyBenchmark(Benchmark): 220 | code = 'dummy' 221 | 222 | def download(self): 223 | pass 224 | 225 | def run(self): 226 | return "dummy" 227 | 228 | 229 | ALL_BENCHMARKS = [SpeedtestBenchmark, DDBenchmark, FioBenchmark, 230 | UnixbenchBenchmark, DownloadBenchmark] 231 | 232 | 233 | def get_benchmark_class(code): 234 | """Return benchmark class with given code or None.""" 235 | for x in ALL_BENCHMARKS: 236 | if x.code == code: 237 | return x 238 | 239 | def get_selected_benchmark_classes(include): 240 | """Return a list of benchmark classes specified with include argument. 241 | 242 | Eg, if include equals 'speedtest,dd' the function returns 243 | [SpeedtestBenchmark, DDBenchmark] 244 | 245 | To include all benchmarks, pass in any falsy value or `all`. 246 | """ 247 | 248 | if not include or ('all' in include.split(',')): 249 | print("All benchmarks selected.") 250 | return ALL_BENCHMARKS 251 | 252 | if include: 253 | result = [] 254 | for i in include.split(','): 255 | cls = get_benchmark_class(i) 256 | if cls: 257 | result.append(cls) 258 | else: 259 | print("%s benchmark hasn't been recognised. Use these: " % i, end="") 260 | print(', '.join([bb.code for bb in ALL_BENCHMARKS])) 261 | return result 262 | -------------------------------------------------------------------------------- /serverscope_benchmark/cli.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import sys 4 | 5 | from .utils import Color as c 6 | from argparse import ArgumentParser as ArgParser 7 | 8 | 9 | def get_parser(): 10 | parser = ArgParser(description="ServerScope.io benchmark kit") 11 | 12 | parser.add_argument('-p', '--plan', help='Required. Server provider and plan' + 13 | ' names as follows: "Plan name|Provider name"') 14 | parser.add_argument('-e', '--email', help='Required. An e-mail to receive online report link') 15 | parser.add_argument('-i', '--include', 16 | help='Comma-separated list of benchmarks to run if you don\'t want to ' + 17 | 'run all of them: dd, fio, speedtest, unixbench') 18 | parser.add_argument('--locale', default="en") 19 | 20 | args = parser.parse_args() 21 | 22 | if args is not dict: 23 | args = vars(args) 24 | 25 | for m in ['plan', 'email']: 26 | if (m not in args) or args[m] is None: 27 | print("Required parameter " + c.RED + c.BOLD + m + c.RESET + " is missing") 28 | parser.print_help() 29 | sys.exit(1) 30 | 31 | return args 32 | -------------------------------------------------------------------------------- /serverscope_benchmark/server.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ Server utilities """ 4 | 5 | import subprocess 6 | import re 7 | 8 | from .utils import Color as c 9 | 10 | 11 | def get_sys_info(obj): 12 | r = 'N/A' 13 | try: 14 | r = subprocess.Popen(['cat', '/proc/%s' % (obj)], 15 | stdout=subprocess.PIPE, 16 | stderr=subprocess.DEVNULL, 17 | universal_newlines=True).communicate()[0] 18 | except subprocess.CalledProcessError: 19 | print('Warning: /proc/%s does not exist' % (obj)) 20 | return r 21 | 22 | 23 | def get_total_ram(meminfo): 24 | match = re.findall(r"DirectMap.+:\s+([0-9]+)\s", meminfo) 25 | 26 | if match: 27 | ram = sum(map(int, match)) 28 | else: 29 | match = re.search(r"MemTotal:\s+([0-9]+)\s", meminfo) 30 | ram = int(match.group(1)) # kB 31 | 32 | ram = round(ram/1024) # MB 33 | ram_mb = ram 34 | if (ram > 1024): 35 | ram_units = 'G' 36 | ram = round(ram/1024) 37 | else: 38 | ram_units = 'M' 39 | 40 | return {'ram': ram, 'units': ram_units, 'ram_mb': ram_mb} 41 | 42 | 43 | def get_cpu_info_val(property, cpuinfo): 44 | match = re.search(property + r"\s+:\s(.+)", cpuinfo) 45 | if match: 46 | return match.group(1) 47 | else: 48 | return 'N/A' 49 | 50 | 51 | def get_cpu_info(cpuinfo): 52 | r = {} 53 | r['name'] = get_cpu_info_val('model name', cpuinfo) 54 | r['count'] = len(re.findall(r"processor\s+:\s", cpuinfo)) 55 | r['cores'] = get_cpu_info_val('cpu cores', cpuinfo) 56 | 57 | return r 58 | 59 | 60 | def get_nodev_filesystems(): 61 | r = [] 62 | with open('/proc/filesystems', 'r') as f: 63 | for line in f: 64 | match = re.search(r'^nodev\s+(\S+)', line) 65 | if match: 66 | r.append(match.group(1)) 67 | 68 | return r 69 | 70 | 71 | def get_total_disk(): 72 | nodevs = get_nodev_filesystems() 73 | command = ['df'] 74 | for fs in nodevs: 75 | command.append('-x') 76 | command.append(fs) 77 | df = subprocess.Popen(command, 78 | stdout=subprocess.PIPE, 79 | stderr=subprocess.DEVNULL, 80 | universal_newlines=True).communicate()[0] 81 | 82 | lines = df.split('\n')[1:] 83 | total = 0 84 | for line in lines: 85 | match = re.search(r'\S+\s+([0-9]+)', line) 86 | if match: 87 | total += int(match.group(1)) 88 | total = round(total / 1000 / 1000) 89 | return {"output": df, "total": '%dGB' % total} 90 | 91 | 92 | def get_server_specs(): 93 | """Return server specs.""" 94 | print(c.GREEN + 'Collecting server specs... ' + c.RESET) 95 | specs = {} 96 | specs['cpuinfo'] = get_sys_info('cpuinfo') 97 | specs['meminfo'] = get_sys_info('meminfo') 98 | df = get_total_disk() 99 | specs['diskinfo'] = df['output'] 100 | print(df['output']) 101 | ram = get_total_ram(specs['meminfo']) 102 | cpu = get_cpu_info(specs['cpuinfo']) 103 | print('%(count)s x %(name)s' % cpu, end="") 104 | print(' | %(ram)s%(units)s RAM' % ram, end="") 105 | print(' | %s disk' % df['total']) 106 | 107 | return specs 108 | -------------------------------------------------------------------------------- /serverscope_benchmark/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import sys 5 | import subprocess 6 | import locale 7 | import urllib 8 | import requests 9 | 10 | from contextlib import contextmanager 11 | 12 | 13 | class Color: 14 | PURPLE = '\033[95m' 15 | BLUE = '\033[94m' 16 | GREEN = '\033[92m' 17 | ORANGE = '\033[93m' 18 | RED = '\033[91m' 19 | BOLD = '\033[1m' 20 | UNDERLINE = '\033[4m' 21 | RESET = '\033[0m' 22 | 23 | c = Color 24 | 25 | 26 | def run_and_print(command, cwd=None): 27 | 28 | chunks = [] 29 | 30 | with subprocess.Popen(command, 31 | stdout=subprocess.PIPE, 32 | stderr=subprocess.STDOUT, 33 | cwd=cwd, 34 | universal_newlines=True) as p: 35 | chunk = p.stdout.readline() 36 | while chunk: 37 | sys.stdout.write(chunk) 38 | chunks.append(chunk) 39 | sys.stdout.flush() 40 | chunk = p.stdout.readline() 41 | 42 | return ''.join(chunks) 43 | 44 | 45 | def post_results(data): 46 | url = 'https://serverscope.io/api/trials.txt' 47 | headers = { 48 | 'Content-Type': 'application/x-www-form-urlencoded', 49 | 'Accept': 'text/plain', 50 | 'User-Agent': 'serverscope.io benchmark tool' 51 | } 52 | 53 | response = requests.post(url, data=urllib.parse.urlencode(data), headers=headers) 54 | print(response.text) 55 | 56 | 57 | def get_geo_info(): 58 | """Return geo location information.""" 59 | print(c.GREEN + 'Retrieving server location... ' + c.RESET) 60 | try: 61 | cmd = ['curl', '-s', 'http://geoip.nekudo.com/api/'] 62 | geo = subprocess.Popen(cmd, 63 | stdout=subprocess.PIPE, 64 | universal_newlines=True).communicate()[0] 65 | except ValueError: 66 | print(c.RED + "geoip API error. Terminating..." + c.RESET) 67 | sys.exit(1) 68 | 69 | return geo 70 | 71 | @contextmanager 72 | def pushd(new_d): 73 | """ Implements pushd/popd interface """ 74 | previous_d = os.getcwd() 75 | os.chdir(new_d) 76 | yield 77 | os.chdir(previous_d) 78 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup, find_packages 3 | import serverscope_benchmark 4 | 5 | 6 | # allow setup.py to be run from any path 7 | os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) 8 | 9 | setup( 10 | name='serverscope_benchmark', 11 | version=serverscope_benchmark.__version__, 12 | author='ServerScope', 13 | author_email='contact@serverscope.io', 14 | packages=find_packages(), 15 | url='https://github.com/serverscope/serverscope-benchmark', 16 | license='MIT', 17 | description='serverscope.io benchmark tool', 18 | long_description=open('README.md').read(), 19 | include_package_data=True, 20 | python_requires='>=3.5', 21 | install_requires=[ 22 | 'requests', 23 | 'distro' 24 | ], 25 | classifiers=[ 26 | 'Development Status :: 4 - Beta', 27 | 'Environment :: Console', 28 | 'Intended Audience :: System Administrators', 29 | 'Intended Audience :: Developers', 30 | 'License :: OSI Approved :: MIT License', 31 | 'Operating System :: POSIX :: Linux', 32 | 'Operating System :: POSIX :: Other', 33 | 'Operating System :: Unix', 34 | 'Environment :: Console', 35 | 'Programming Language :: Python', 36 | 'Programming Language :: Python :: 3', 37 | 'Topic :: System :: Benchmark', 38 | ], 39 | ) 40 | -------------------------------------------------------------------------------- /tests/general-test-deb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ `whoami` != 'root' ]; then 4 | echo "You must be root to run tests" 5 | exit 1 6 | fi 7 | 8 | cat /etc/os-release | grep -E 'Ubuntu|Debian' > /dev/null 9 | if [ $? -ne 0 ]; then 10 | echo "Test is intended to be run on Ubuntu/Debian, exiting" 11 | exit 1 12 | fi 13 | 14 | source /etc/os-release 15 | 16 | dpkg-query -l | grep debootstrap > /dev/null 17 | if [ $? -ne 0 ]; then 18 | echo "debootstrap package is required for testing" 19 | echo "use: apt-get install debootstrap" 20 | exit 1 21 | fi 22 | 23 | 24 | if [ "$(pwd)" == "/" ]; then 25 | echo "It's prohibided to be run from system root (/)!" 26 | exit 1 27 | fi 28 | 29 | RELVER=$UBUNTU_CODENAME 30 | if [ -z $RELVER ]; then 31 | RELVER=$VERSION_CODENAME 32 | if [ -z $RELVER ]; then 33 | echo "This is not a valid Ubuntu/Debian release, aborting..." 34 | exit 1 35 | fi 36 | echo "Bootstrapping for Debian $RELVER" 37 | RELURL="http://deb.debian.org/debian/" 38 | else 39 | echo "Bootstrapping for Ubuntu $RELVER" 40 | RELURL="http://archive.ubuntu.com/ubuntu/" 41 | fi 42 | TESTROOT=$(pwd)/testroot-$RELVER/ 43 | SS_DIR=$TESTROOT/tmp/ss_dir 44 | RESULT="1" 45 | DEPS_LIST="make perl fio gcc python3-setuptools python3-distro python3-requests curl locales" 46 | DIRA=$(dirname "$0") 47 | 48 | function __setup_testroot() { 49 | mount --bind /proc $TESTROOT/proc 50 | mount --bind -o ro /etc/apt/sources.list $TESTROOT/etc/apt/sources.list 51 | mount --bind -o ro /etc/resolv.conf $TESTROOT/etc/resolv.conf 52 | } 53 | 54 | function __clean_up_testroot() { 55 | # clean up 56 | 57 | if [[ "$TESTROOT" == "/" ]] || [[ -z "$TESTROOT" ]]; then 58 | echo "$TESTROOT is broken while was testing and won't be deleted" 59 | exit 1 60 | fi 61 | 62 | umount $TESTROOT/proc 63 | umount $TESTROOT/etc/resolv.conf 64 | umount $TESTROOT/etc/apt/sources.list 65 | 66 | echo "rm -rf $TESTROOT" 67 | rm -rf $TESTROOT 68 | } 69 | 70 | trap ctrl_c INT 71 | function ctrl_c() { 72 | # to prevent SIGPIPE for broken pipe's stdout 73 | exec &>$(tty) 74 | echo "** Test aborted by CTRL-C" 75 | __clean_up_testroot 76 | exit 1 77 | } 78 | 79 | debootstrap --variant=buildd --arch amd64 $RELVER $TESTROOT $RELURL 80 | 81 | __setup_testroot 82 | 83 | chroot $TESTROOT apt-get update -y 84 | for DEP in $DEPS_LIST; do 85 | chroot $TESTROOT apt-get install -y $DEP 86 | done 87 | chroot $TESTROOT locale-gen en_GB.UTF-8 88 | 89 | mkdir -p $SS_DIR 90 | 91 | cp -r $DIRA/../serverscope_benchmark $SS_DIR 92 | cp -r $DIRA/../setup.py $SS_DIR 93 | cp -r $DIRA/../README.md $SS_DIR 94 | 95 | chroot $TESTROOT python3 /tmp/ss_dir/setup.py install 96 | 97 | # Do actual test 98 | LC_ALL="C.UTF-8" chroot $TESTROOT python3 -m serverscope_benchmark -e "test-development@broken.com" -p "Plan|HostingP" -i dd,speedtest,download,dd,fio,unixbench 99 | RESULT="$?" 100 | 101 | __clean_up_testroot 102 | 103 | if [ "$RESULT" -eq "0" ]; then 104 | echo "TEST PASSED" 105 | exit 0 106 | else 107 | echo "TEST FAILED" 108 | exit 1 109 | fi 110 | -------------------------------------------------------------------------------- /tests/general-test-el7_el8.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ `whoami` != 'root' ]; then 4 | echo "You must be root to run tests" 5 | exit 1 6 | fi 7 | 8 | source /etc/os-release 9 | RELVER=$VERSION 10 | 11 | if [ "$(pwd)" == "/" ]; then 12 | echo "It's prohibided to be run from system root (/)!" 13 | exit 1 14 | fi 15 | 16 | TESTROOT=$(pwd)/testroot-el$RELVER/ 17 | SS_DIR=$TESTROOT/tmp/ss_dir 18 | RESULT="1" 19 | DEPS_LIST="system-release epel-release basesystem curl python3 python3-setuptools perl make gcc fio" 20 | DIRA=$(dirname "$0") 21 | 22 | function __setup_testroot() { 23 | mount --bind /proc $TESTROOT/proc 24 | 25 | chroot $TESTROOT mknod -m 622 /dev/console c 5 1 26 | chroot $TESTROOT mknod -m 666 /dev/null c 1 3 27 | chroot $TESTROOT mknod -m 666 /dev/zero c 1 5 28 | chroot $TESTROOT mknod -m 666 /dev/ptmx c 5 2 29 | chroot $TESTROOT mknod -m 666 /dev/tty c 5 0 30 | chroot $TESTROOT mknod -m 444 /dev/random c 1 8 31 | chroot $TESTROOT mknod -m 444 /dev/urandom c 1 9 32 | chroot $TESTROOT chown -v root:tty /dev/{console,ptmx,tty} 33 | 34 | # TODO: ugly code 35 | # but just for networking 36 | # but anyway, ro mode, should be safe 37 | mount --bind -o ro /etc/ $TESTROOT/etc/ 38 | } 39 | 40 | function __clean_up_testroot() { 41 | # clean up 42 | if [[ "$TESTROOT" == "/" ]] || [[ -z "$TESTROOT" ]]; then 43 | echo "$TESTROOT is broken while was testing and won't be deleted" 44 | exit 1 45 | fi 46 | 47 | umount $TESTROOT/proc 48 | umount $TESTROOT/etc/ 49 | 50 | echo "rm -rf $TESTROOT" 51 | rm -rf $TESTROOT 52 | } 53 | 54 | trap ctrl_c INT 55 | function ctrl_c() { 56 | # to prevent SIGPIPE for broken pipe's stdout 57 | exec &>$(tty) 58 | echo "** Test aborted by CTRL-C" 59 | __clean_up_testroot 60 | exit 1 61 | } 62 | 63 | yum install -y --setopt=releasever=$RELVER --installroot $TESTROOT $DEPS_LIST 64 | 65 | __setup_testroot 66 | 67 | mkdir -p $SS_DIR 68 | cp -r $DIRA/../serverscope_benchmark $SS_DIR 69 | cp -r $DIRA/../setup.py $SS_DIR 70 | cp -r $DIRA/../README.md $SS_DIR 71 | 72 | chroot $TESTROOT python3 /tmp/ss_dir/setup.py install 73 | 74 | # Do actual test 75 | LC_ALL="C.UTF-8" chroot $TESTROOT python3 -m serverscope_benchmark -e "test-development@broken.com" -p "Plan|HostingP" -i speedtest,download,dd,fio,unixbench 76 | RESULT="$?" 77 | 78 | __clean_up_testroot 79 | 80 | if [ "$RESULT" -eq "0" ]; then 81 | echo "TEST PASSED" 82 | exit 0 83 | else 84 | echo "TEST FAILED" 85 | exit 1 86 | fi 87 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length=100 3 | --------------------------------------------------------------------------------