Skip to content

Instantly share code, notes, and snippets.

@IvanChepurnyi
Created September 12, 2016 01:32
Show Gist options
  • Save IvanChepurnyi/b30f1f036904d87357571cba6fd09d4e to your computer and use it in GitHub Desktop.
Save IvanChepurnyi/b30f1f036904d87357571cba6fd09d4e to your computer and use it in GitHub Desktop.
Benchmark Scripts (Root User Usage)
#
# The MIT License (MIT)
#
# Copyright (c) 2014 Mathias Leppich <mleppich@muhqu.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
usage() {
cat <<USAGE
Usage: $(basename $0) [-connect CONN] -r <code>
Options:
-connect CONN Passed to cgi-fcgi, default: /var/run/php5-fpm.sock
examples: ip.add.re.ss:port or /path/to/php-fpm.sock
-r <code> Run PHP <code> without using script tags <?..?>
USAGE
}
main() {
if [ -z "$PHPCODE" ]
then
execute_status
else
execute_code
fi
}
function execute_status() {
execute_fpm /$PHPSTATUS
}
function execute_code() {
echo "<?php $PHPCODE" > $PHPFPMCLI_FILE
execute_fpm $PHPFPMCLI_FILE
rm $PHPFPMCLI_FILE
}
function execute_fpm() {
SCRIPT_FILENAME=$1 \
SCRIPT_NAME=$1 \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect "$CONN" | stripheaders
}
stripheaders() {
sed -E '1,/^.$/ d'
}
PHPFPMCLI_FILE="/tmp/php-fpm-cli.$$.php"
CONN="127.0.0.1:9000"
PHPCODE=""
init() {
until [ -z "$1" ]; do
case "$1" in
-connect|--connect) shift; CONN="$1"; shift; ;;
-r) shift; PHPCODE="$1"; shift; ;;
-s) shift; PHPSTATUS="$1"; shift; ;;
help|-h|-help|--help)
usage;
exit 0
;;
*)
echo "unknown option: $1";
usage;
exit 1
;;
esac
done
if [ -z "$PHPCODE" -a -z "$PHPSTATUS" ]; then
echo "missing option: -r <code> or -s <status_path>";
usage;
exit 1
fi
}
init "$@" && main
#!/bin/bash
if [ ! -d $HOME/cpu-usage ]
then
mkdir -p $HOME/cpu-usage
fi
if [ -f $HOME/cpu-marker ]
then
report_name=$(cat $HOME/cpu-marker)
while [ -f $HOME/cpu-marker ]
do
sleep 5
php $HOME/stats.php >> $HOME/cpu-usage/$report_name.csv
done
fi
#!/bin/bash
sudo -u app redis-cli FLUSHALL
service mysql stop
if [ -z $SKIP_VARNISH ]
then
service varnish stop
fi
service nginx stop
service php7.0-fpm stop
service redis-server stop
echo 3 > /proc/sys/vm/drop_caches
service redis-server start
service php7.0-fpm start
service nginx start
if [ -z $SKIP_VARNISH ]
then
service varnish start
else
# Instead of restart we ban all varnish requests
echo "Banning all varnish pages in current instance"
varnishadm 'ban req.url ~ /'
fi
service mysql start
#!/bin/bash
dir=/data/web
magebase=$1
dbtype=$2
if [ -f $dir/nginx/magento2.flag ]
then
rm $dir/nginx/magento2.flag
fi
cd $dir/$magebase
if [[ -v NO_IMAGES ]]
then
echo "Installing Magento without images"
sudo -i -u app bash -c "export NO_IMAGES=1; $dir/$magebase/setup.sh magento grow-m1.ecomdev.org $dbtype"
else
echo "Installing Magento with images"
sudo -i -u app bash $dir/$magebase/setup.sh magento grow-m1.ecomdev.org $dbtype
fi
SKIP_VARNISH=1 $HOME/restart.sh
echo $magebase > $HOME/magebase
#!/bin/bash
dir=/data/web
magebase=$1
version=$2
dbtype=$3
touch $dir/nginx/magento2.flag
cd $dir/$magebase
if [[ -v NO_IMAGES ]]
then
echo "Installing Magento without images"
sudo -i -u app bash -c "export NO_IMAGES=1; $dir/$magebase/setup.sh magento grow-m1.ecomdev.org $version $dbtype"
else
echo "Installing Magento with images"
sudo -i -u app bash $dir/$magebase/setup.sh magento grow-m1.ecomdev.org $version $dbtype
if [[ -v RESIZE_IMAGES ]]
then
echo "Resizing Magento catalog images"
sudo -i -u app bash $dir/$magebase/resize-images.sh
fi
fi
if [[ -v FIX_MAGENTO ]]
then
echo "Fixing performance issues in Magento 2.x"
sudo -i -u app bash $dir/$magebase/fix-performance.sh
fi
SKIP_VARNISH=1 $HOME/restart.sh
echo $magebase > $HOME/magebase
<?php
$mysqlSettings = parse_ini_file(getenv('HOME') . '/.my.cnf');
$conn = new PDO("mysql:host=127.0.0.1", $mysqlSettings['user'], $mysqlSettings['password']);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$fetchPairs = function ($stmt) {
$result = [];
foreach ($stmt->fetchAll(PDO::FETCH_NUM) as $row) {
$result[$row[0]] = $row[1];
}
return $result;
};
$fetchMySQLStats = function ($query) use ($fetchPairs, $conn) {
$stmt = $conn->prepare($query);
$stmt->execute();
return $fetchPairs($stmt);
};
$stats = $fetchMySQLStats('SHOW GLOBAL STATUS');
$loadAverage = preg_replace('/.*load average\: (.*?),.*/', '\1', shell_exec('uptime'));
$memoryProc = file_get_contents('/proc/meminfo');
$memoryInfo = [];
if (preg_match_all('/^([^\:]+)\:\s+([0-9]+)\s+([a-zA-Z]{1,2})/m', $memoryProc, $matches)) {
foreach ($matches[0] as $index => $value) {
$value = $matches[2][$index];
if (strtolower($matches[3][$index]) == 'kb') {
$value = $value / 1024;
}
$memoryInfo[$matches[1][$index]] = $value;
}
}
$fpmOutput = shell_exec('./php-fpm-cli -s fpm_status');
$fpmInfo = [];
if (preg_match_all('/^([^\:]+)\:\s+([\S]+?)$/m', $fpmOutput, $matches)) {
foreach ($matches[0] as $index => $value) {
$value = $matches[2][$index];
$fpmInfo[$matches[1][$index]] = $value;
}
}
$info = [
'mysql_table_scans_pct' => ($stats['Handler_read_rnd_next'] + $stats['Handler_read_rnd']) / ($stats['Handler_read_rnd_next'] + $stats['Handler_read_rnd'] + $stats['Handler_read_first'] + $stats['Handler_read_next'] + $stats['Handler_read_key'] + $stats['Handler_read_prev']),
'mysql_deadlocks' => $stats['Innodb_deadlocks'],
'mysql_wait_locks' => $stats['Innodb_row_lock_current_waits'],
'mysql_current_locks' => $stats['Innodb_current_row_locks'],
'mysql_total_locks' => $stats['Innodb_row_lock_waits'],
'mysql_lock_time_avg' => $stats['Innodb_row_lock_time_avg'],
'mysql_lock_time_max' => $stats['Innodb_row_lock_time_max'],
'mysql_select_scans' => $stats['Select_scan'],
'mysql_sort_scans' => $stats['Sort_scan'],
'mysql_join_scans' => $stats['Select_full_join'],
'sys_load_avg' => (float)$loadAverage,
'sys_memory_used' => $memoryInfo['MemTotal'] - $memoryInfo['MemFree'],
'sys_memory_used_buffers' => $memoryInfo['Buffers'],
'sys_memory_used_cache' => $memoryInfo['Cached'],
'sys_memory_swap_used' => $memoryInfo['SwapTotal'] - $memoryInfo['SwapFree'],
'sys_memory_swap_used_cache' => $memoryInfo['SwapCached'],
'fpm_listen_queue' => $fpmInfo['listen queue'],
'fpm_listen_queue_length' => $fpmInfo['listen queue len'],
'fpm_total_processes' => $fpmInfo['total processes'],
'fpm_active_processes' => $fpmInfo['active processes']
];
fputcsv(STDOUT, [time(), json_encode($info)]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment