-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_range.sh
executable file
·158 lines (125 loc) · 4.15 KB
/
test_range.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
#
# This program simulates the insertion of N random range-matching
# entries in the table and allows one to estimate how many TCAM entries
# will be required
#
#
# Set up the defaults
#
iterations=1000
width=16
max_range=0
algo='./bf_range'
verbose=0
function parse_options() {
opts=`getopt -o hi:w:r:a:v \
-l help \
-l iterations: \
-l width: \
-l range: \
-l algorithm: \
-l verbose \
-- "$@"`
if [ $? != 0 ]; then
print_help
exit 1
fi
eval set -- "$opts"
while true; do
case "$1" in
-h|--help) print_help; exit 0 ;;
-i|--iterations) iterations=$2; shift 2 ;;
-w|--width) width=$2; shift 2 ;;
-r|--range) max_range=$2; shift 2 ;;
-a|--algorithm) algo=$2; shift 2 ;;
-v|--verbose) verbose=1; shift 1 ;;
--) shift; break
esac
done
#
# Let's do a separate check for algo, since we rely on external
# programs.
if [ ! -x $algo ]; then
cat <<EOF
ERROR: $algo is not an executable.
Typically you need to build the bf_range and tcam_range programs by
running command
make -C ~/tools
EOF
exit 1
fi
}
function print_help() {
prog=$0
cat <<EOF
Usage:
$prog [-i iterations] [-w bit_width] [-r range_len] [-a algorithm] [-v]
-i iterations
Specify the number of entries for the simulation. Default is 1000
-w bitwidth
Specify the number of bits in the key (up to 32). Default is 16
-r range_len
Specify the maximum size of the range. Actual ranges will have the
a length between 0 and range_len inclusively. 0 -- no caps on the
range length (totally random). Default is 0
-a algorithm
Specify the program that will perform the calculation (typically
it is ./bf_range or ./tcam_range). Default is ./bf_range
-v
Verbose mode: print the results for each entry
EOF
}
#
# This function allows us to generate a random number without $RANDOM
# because it only provides the numbers in the range beween 0 and 32767
function get_random() {
echo `od -A n -t u -N 4 /dev/urandom`
}
function run_test() {
total=0
total_range_len=0
max_val=$(( (1 << $width) - 1 ))
for n in `seq 0 $iterations`; do
# Start of the range is always random
start=$(( `get_random` % $max_val ))
# Handle the case, where this is the max value
if [ $start -eq $max_val ]; then
# The range will then always be [max:max]
end=$start
else
# If max_range has not been specified
if [ $max_range -eq 0 ]; then
# Then we'll randomly select the width
max_end_val=$max_val
else
# Otherwise, we'll be selecting randomly withing
# specified range (and do it safely)
max_end_val=$(( $start + $max_range ))
if [ $max_end_val -gt $max_val ]; then
max_end_val=$max_val
fi
fi
# The range boundaries are determined, so we can
# now randomly select where the end will be
end=$(($start + `get_random` % ($max_end_val - $start)))
fi
range_len=$(( $end - $start + 1 ))
total_range_len=$(($total_range_len + $range_len))
# Compute the number of entries
entries=`$algo $start $end | wc -l`
total=$(($total + $entries))
if [ $verbose -gt 0 ]; then
echo -e "${n}\t${start}\t${end}\t${range_len}\t${entries}"
fi
done
avg_range_len=`echo "scale=2;${total_range_len}/${iterations}" | bc`
avg_entries=`echo "scale=2;${total}/${iterations}" | bc`
echo "Algorithm: $algo"
echo "Bitwidth: $width"
echo "Iterations: $iterations"
echo "Avg. Range: $avg_range_len"
echo "Avg. Entries: $avg_entries"
}
parse_options $@
run_test