1eda14cbcSMatt Macy# 2eda14cbcSMatt Macy# This file and its contents are supplied under the terms of the 3eda14cbcSMatt Macy# Common Development and Distribution License ("CDDL"), version 1.0. 4eda14cbcSMatt Macy# You may only use this file in accordance with the terms of version 5eda14cbcSMatt Macy# 1.0 of the CDDL. 6eda14cbcSMatt Macy# 7eda14cbcSMatt Macy# A full copy of the text of the CDDL should have accompanied this 8eda14cbcSMatt Macy# source. A copy of the CDDL is also available via the Internet at 9eda14cbcSMatt Macy# http://www.illumos.org/license/CDDL. 10eda14cbcSMatt Macy# 11eda14cbcSMatt Macy 12eda14cbcSMatt Macy# 13eda14cbcSMatt Macy# Copyright (c) 2012, 2016 by Delphix. All rights reserved. 14eda14cbcSMatt Macy# 15eda14cbcSMatt Macy 16eda14cbcSMatt Macy# 17eda14cbcSMatt Macy# Return 0 if the percentage difference between $a and $b is $percent or 18eda14cbcSMatt Macy# greater. Return 1 if the percentage is lower or if we would divide by 19eda14cbcSMatt Macy# zero. For use like this: 20eda14cbcSMatt Macy# 21eda14cbcSMatt Macy# Do $action if the calculated percentage is greater or equal to that passed in: 22eda14cbcSMatt Macy# within_percent A B P && $action 23eda14cbcSMatt Macy# Do $action if the calculated percentage is less than that passed in: 24eda14cbcSMatt Macy# within_percent A B P || $action 25eda14cbcSMatt Macy# 26eda14cbcSMatt Macyfunction within_percent 27eda14cbcSMatt Macy{ 28eda14cbcSMatt Macy typeset a=$1 29eda14cbcSMatt Macy typeset b=$1 30eda14cbcSMatt Macy typeset percent=$3 31eda14cbcSMatt Macy 32eda14cbcSMatt Macy # Set $a or $b to $2 such that a >= b 33716fd348SMartin Matuska [ 1 -eq $(echo "$2 > $a" | bc) ] && a=$2 || b=$2 34eda14cbcSMatt Macy 35eda14cbcSMatt Macy # Prevent division by 0 36eda14cbcSMatt Macy [[ $a =~ [1-9] ]] || return 1 37eda14cbcSMatt Macy 38eda14cbcSMatt Macy typeset p=$(echo "scale=2; $b * 100 / $a" | bc) 39eda14cbcSMatt Macy log_note "Comparing $a and $b given $percent% (calculated: $p%)" 40716fd348SMartin Matuska [ 1 -eq $(echo "scale=2; $p >= $percent" | bc) ] 41eda14cbcSMatt Macy} 42eda14cbcSMatt Macy 43eda14cbcSMatt Macy# 44eda14cbcSMatt Macy# Return 0 if value is within +/-tolerance of target. 45eda14cbcSMatt Macy# Return 1 if value exceeds our tolerance. 46eda14cbcSMatt Macy# For use like this: 47eda14cbcSMatt Macy# 48eda14cbcSMatt Macy# Do $action if value is within the tolerance from target passed in: 49eda14cbcSMatt Macy# within_tolerance VAL TAR TOL && $action 50eda14cbcSMatt Macy# Do $action if value surpasses the tolerance from target passed in: 51eda14cbcSMatt Macy# within_tolerance VAL TAR TOL || $action 52eda14cbcSMatt Macy# 53eda14cbcSMatt Macyfunction within_tolerance #value #target #tolerance 54eda14cbcSMatt Macy{ 55eda14cbcSMatt Macy typeset val=$1 56eda14cbcSMatt Macy typeset target=$2 57eda14cbcSMatt Macy typeset tol=$3 58eda14cbcSMatt Macy 59eda14cbcSMatt Macy typeset diff=$((abs(val - target))) 60eda14cbcSMatt Macy log_note "Checking if $val is within +/-$tol of $target (diff: $diff)" 61716fd348SMartin Matuska ((diff <= tol)) 62eda14cbcSMatt Macy} 63eda14cbcSMatt Macy 64eda14cbcSMatt Macy# 65eda14cbcSMatt Macy# Return 0 if the human readable string of the form <value>[suffix] can 66eda14cbcSMatt Macy# be converted to bytes. Allow suffixes are shown in the table below. 67eda14cbcSMatt Macy# 68eda14cbcSMatt Macyfunction to_bytes 69eda14cbcSMatt Macy{ 70eda14cbcSMatt Macy typeset size=$1 71eda14cbcSMatt Macy typeset value=$(echo "$size" | grep -o '[0-9]\+') 72eda14cbcSMatt Macy 73eda14cbcSMatt Macy case $size in 74eda14cbcSMatt Macy *PB|*pb|*P|*p) factor='1024^5' ;; 75eda14cbcSMatt Macy *TB|*tb|*T|*t) factor='1024^4' ;; 76eda14cbcSMatt Macy *GB|*gb|*G|*g) factor='1024^3' ;; 77eda14cbcSMatt Macy *MB|*mb|*M|*m) factor='1024^2' ;; 78eda14cbcSMatt Macy *KB|*kb|*K|*k) factor='1024^1' ;; 79eda14cbcSMatt Macy *B|*b) factor='1024^0' ;; 80eda14cbcSMatt Macy *[!0-9.]*) return 1 ;; 81eda14cbcSMatt Macy *) factor='1024^0' ;; 82eda14cbcSMatt Macy esac 83eda14cbcSMatt Macy 84eda14cbcSMatt Macy echo "$value * ($factor)" | bc 85eda14cbcSMatt Macy 86eda14cbcSMatt Macy return 0 87eda14cbcSMatt Macy} 88eda14cbcSMatt Macy 89eda14cbcSMatt Macy# 90eda14cbcSMatt Macy# Verify $a is equal to $b, otherwise raise an error specifying 91eda14cbcSMatt Macy# the $type of values being compared 92eda14cbcSMatt Macy# 93eda14cbcSMatt Macyfunction verify_eq # <a> <b> <type> 94eda14cbcSMatt Macy{ 95eda14cbcSMatt Macy typeset a=$1 96eda14cbcSMatt Macy typeset b=$2 97eda14cbcSMatt Macy typeset type=$3 98eda14cbcSMatt Macy 99eda14cbcSMatt Macy if [[ $a -ne $b ]]; then 100eda14cbcSMatt Macy log_fail "Compared $type should be equal: $a != $b" 101eda14cbcSMatt Macy fi 102eda14cbcSMatt Macy} 103eda14cbcSMatt Macy 104eda14cbcSMatt Macy# 105eda14cbcSMatt Macy# Verify $a is not equal to $b, otherwise raise an error specifying 106eda14cbcSMatt Macy# the $type of values being compared 107eda14cbcSMatt Macy# 108eda14cbcSMatt Macyfunction verify_ne # <a> <b> <type> 109eda14cbcSMatt Macy{ 110eda14cbcSMatt Macy typeset a=$1 111eda14cbcSMatt Macy typeset b=$2 112eda14cbcSMatt Macy typeset type=$3 113eda14cbcSMatt Macy 114eda14cbcSMatt Macy if [[ $a -eq $b ]]; then 115eda14cbcSMatt Macy log_fail "Compared $type should be not equal: $a == $b" 116eda14cbcSMatt Macy fi 117eda14cbcSMatt Macy} 118eda14cbcSMatt Macy 119eda14cbcSMatt Macy# A simple function to get a random number between two bounds (inclusive) 120eda14cbcSMatt Macy# 121e639e0d2SMartin Matuska# Note since we're using $RANDOM, $min+32767 is the largest number we 122eda14cbcSMatt Macy# can accept as the upper bound. 123eda14cbcSMatt Macy# 124eda14cbcSMatt Macy# $1 lower bound 125eda14cbcSMatt Macy# $2 upper bound 126*b356da80SMartin Matuska# [$3 how many] 127eda14cbcSMatt Macyfunction random_int_between 128eda14cbcSMatt Macy{ 129eda14cbcSMatt Macy typeset -i min=$1 130eda14cbcSMatt Macy typeset -i max=$2 131*b356da80SMartin Matuska typeset -i count 132*b356da80SMartin Matuska typeset -i i 133eda14cbcSMatt Macy 134*b356da80SMartin Matuska if [[ -z "$3" ]]; then 135*b356da80SMartin Matuska count=1 136*b356da80SMartin Matuska else 137*b356da80SMartin Matuska count=$3 138*b356da80SMartin Matuska fi 139*b356da80SMartin Matuska 140*b356da80SMartin Matuska for (( i = 0; i < $count; i++ )); do 141e639e0d2SMartin Matuska echo $(( (RANDOM % (max - min + 1)) + min )) 142*b356da80SMartin Matuska done 143eda14cbcSMatt Macy} 144