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