2 # these are the pieces that we'll use to assemble mostly random files.
3 RANDOM_CHUNK_FILES=($TEST_TEMP/random.0 $TEST_TEMP/random.1 $TEST_TEMP/random.2 $TEST_TEMP/random.3 $TEST_TEMP/random.4)
5 # largest chunk of random data we'll actually generate at a time, in each chunk file.
6 export MAX_CHUNK_FILE=65536
8 # returns the file size for the first argument.
11 local file="$1"; shift
15 stat --printf="%s" "$file"
19 # outputs the number of seconds since the epoch.
20 function getTimeStamp()
25 # makes sure the chunk files are all generated.
26 function prepareRandomChunks()
29 for ((i = 0; i < ${#RANDOM_CHUNK_FILES[@]}; i++)); do
30 # make the chunk files if they don't exist.
31 local currfile="${RANDOM_CHUNK_FILES[$i]}"
32 if [ ! -f "$currfile" ]; then
33 local filesize=$MAX_CHUNK_FILE
34 # pick a value to add or subtract from the constant sized chunk, so we won't always be
35 # using files at the same boundaries or with a power of 2 size.
36 local moddy=$(( ($(echo $RANDOM) % 128) - 64 ))
37 ((filesize -= $moddy))
38 #echo creating chunk file $currfile of size $filesize
39 dd if=/dev/urandom of=$currfile bs=1 count=$filesize &>/dev/null
40 assertEquals "creating random chunk file $currfile" 0 $?
45 # creates a somewhat random file for testing. this will be assembled out of
46 # our chunks of random files, so is not truly random, but we've found that the
47 # random number generator is a HUGE drag on our testing speed. this is still
48 # pretty random data. the first argument is the file name and the second is
49 # the desired file size.
50 function createRandomFile()
52 local file="$1"; shift
53 local size="$1"; shift
57 local stampBefore=$(getTimeStamp)
59 # truncate any existing stuff.
62 while [ $(getFileSize "$file") -lt $size ]; do
63 which_chunker=$(expr $(echo $RANDOM) % ${#RANDOM_CHUNK_FILES[@]})
64 #echo choosing chunk file $which_chunker
65 cat "${RANDOM_CHUNK_FILES[$which_chunker]}" >>"$file"
68 #echo file size after random chunkings is $(getFileSize "$file")
70 local fsizenow="$(getFileSize "$file")"
71 #echo size now is $fsizenow and desired is $size
72 if [ $fsizenow -gt $size ]; then
73 #echo trying to truncate file
74 truncate -s $size "$file"
76 #echo file size after truncate is $(getFileSize "$file") and expected size is $size
78 local stampAfter=$(getTimeStamp)
79 local secs=$(($stampAfter - $stampBefore))
80 if [ $secs -le 0 ]; then
81 # even though it claims it took zero time, we know better, but we also don't want to
82 # divide by zero, so it loses its credit for being very fast here.
85 local kbs=$(( $size / $secs / 1024))
87 local fsizenow="$(getFileSize "$file")"
88 assertEquals "Creating random file of $size bytes at ${kbs} kbps in: $file" $size $fsizenow