Files
percona-toolkit/lib/bash/report_formatting.sh
Brian Fraser fraserb@gmail.com 55c7248c54 (more syncing)
2012-03-20 17:16:06 -03:00

113 lines
3.5 KiB
Bash

# This program is copyright 2011 Percona Inc.
# Feedback and improvements are welcome.
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
# ###########################################################################
# report_formatting package
# ###########################################################################
# Package: report_formatting
# Common report formatting functions for the summary tools.
set -u
POSIXLY_CORRECT=1
export POSIXLY_CORRECT
fuzzy_formula='
rounded = 0;
if (fuzzy_var <= 10 ) {
rounded = 1;
}
factor = 1;
while ( rounded == 0 ) {
if ( fuzzy_var <= 50 * factor ) {
fuzzy_var = sprintf("%.0f", fuzzy_var / (5 * factor)) * 5 * factor;
rounded = 1;
}
else if ( fuzzy_var <= 100 * factor) {
fuzzy_var = sprintf("%.0f", fuzzy_var / (10 * factor)) * 10 * factor;
rounded = 1;
}
else if ( fuzzy_var <= 250 * factor) {
fuzzy_var = sprintf("%.0f", fuzzy_var / (25 * factor)) * 25 * factor;
rounded = 1;
}
factor = factor * 10;
}'
# Does fuzzy rounding: rounds to nearest interval, but the interval gets larger
# as the number gets larger. This is to make things easier to diff.
fuzz () {
echo $1 | awk "{fuzzy_var=\$1; ${fuzzy_formula} print fuzzy_var;}"
}
# Fuzzy computes the percent that $1 is of $2
fuzzy_pct () {
local pct="$(echo $1 $2 | awk '{ if ($2 > 0) { printf "%d", $1/$2*100; } else {print 0} }')";
echo "$(fuzz "${pct}")%"
}
# Prints a section header. All spaces in the string passed in are replaced
# with #'s and all underscores with spaces.
section () {
local str="$1"
local line="$(printf '#_%-60s' "${str}_" | sed -e 's/[[:space:]]/#/g' -e 's/_/ /g')"
printf "%s\n" "${line}"
}
NAME_VAL_LEN=12
name_val () {
# We use $NAME_VAL_LEN here because the two summary tools, as well as
# the tests, use diffent widths.
printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2"
}
# Sub: shorten
# Shorten a value in bytes to another representation.
#
shorten() {
local num="$1"
local prec="${2:-2}"
local div="${3:-1024}"
echo "$num" | awk -v prec="$prec" -v div="$div" '
{
size = 4;
val = $1;
unit = val >= 1099511627776 ? "T" : val >= 1073741824 ? "G" : val >= 1048576 ? "M" : val >= 1024 ? "k" : "";
while ( int(val) && !(val % 1024) ) {
val /= 1024;
}
while ( val > 1000 ) {
val /= div;
}
printf "%.*f%s", prec, val, unit;
}
'
}
group_concat () {
sed -e '{H; $!d;}' -e 'x' -e 's/\n[[:space:]]*\([[:digit:]]*\)[[:space:]]*/, \1x/g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/, //' "${1}"
}
# ###########################################################################
# End report_formatting package
# ###########################################################################