regression_test.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #!/usr/bin/env bash
  2. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. # The RocksDB regression test script.
  4. # REQUIREMENT: must be able to run make db_bench in the current directory
  5. #
  6. # This script will do the following things in order:
  7. #
  8. # 1. check out the specified rocksdb commit.
  9. # 2. build db_bench using the specified commit
  10. # 3. setup test directory $TEST_PATH. If not specified, then the test directory
  11. # will be "/tmp/rocksdb/regression_test"
  12. # 4. run set of benchmarks on the specified host
  13. # (can be either locally or remotely)
  14. # 5. generate report in the $RESULT_PATH. If RESULT_PATH is not specified,
  15. # RESULT_PATH will be set to $TEST_PATH/current_time
  16. #
  17. # = Examples =
  18. # * Run the regression test using rocksdb commit abcdef that outputs results
  19. # and temp files in "/my/output/dir"
  20. #r
  21. # TEST_PATH=/my/output/dir COMMIT_ID=abcdef ./tools/regression_test.sh
  22. #
  23. # * Run the regression test on a remost host under "/my/output/dir" directory
  24. # and stores the result locally in "/my/benchmark/results" using commit
  25. # abcdef and with the rocksdb options specified in /my/path/to/OPTIONS-012345
  26. # with 1000000000 keys in each benchmark in the regression test where each
  27. # key and value are 100 and 900 bytes respectively:
  28. #
  29. # REMOTE_USER_AT_HOST=yhchiang@my.remote.host \
  30. # TEST_PATH=/my/output/dir \
  31. # RESULT_PATH=/my/benchmark/results \
  32. # COMMIT_ID=abcdef \
  33. # OPTIONS_FILE=/my/path/to/OPTIONS-012345 \
  34. # NUM_KEYS=1000000000 \
  35. # KEY_SIZE=100 \
  36. # VALUE_SIZE=900 \
  37. # ./tools/regression_test.sh
  38. #
  39. # = Regression test environmental parameters =
  40. # DEBUG: If true, then the script will not checkout master and build db_bench
  41. # if db_bench already exists
  42. # Default: 0
  43. # TEST_MODE: If 1, run fillseqdeterminstic and benchmarks both
  44. # if 0, only run fillseqdeterministc
  45. # if 2, only run benchmarks
  46. # Default: 1
  47. # TEST_PATH: the root directory of the regression test.
  48. # Default: "/tmp/rocksdb/regression_test"
  49. # RESULT_PATH: the directory where the regression results will be generated.
  50. # Default: "$TEST_PATH/current_time"
  51. # REMOTE_USER_AT_HOST: If set, then test will run on the specified host under
  52. # TEST_PATH directory and outputs test results locally in RESULT_PATH
  53. # The REMOTE_USER_AT_HOST should follow the format user-id@host.name
  54. # DB_PATH: the path where the rocksdb database will be created during the
  55. # regression test. Default: $TEST_PATH/db
  56. # WAL_PATH: the path where the rocksdb WAL will be outputed.
  57. # Default: $TEST_PATH/wal
  58. # OPTIONS_FILE: If specified, then the regression test will use the specified
  59. # file to initialize the RocksDB options in its benchmarks. Note that
  60. # this feature only work for commits after 88acd93 or rocksdb version
  61. # later than 4.9.
  62. # DELETE_TEST_PATH: If true, then the test directory will be deleted
  63. # after the script ends.
  64. # Default: 0
  65. #
  66. # = db_bench parameters =
  67. # NUM_THREADS: The number of concurrent foreground threads that will issue
  68. # database operations in the benchmark. Default: 16.
  69. # NUM_KEYS: The key range that will be used in the entire regression test.
  70. # Default: 1G.
  71. # NUM_OPS: The number of operations (reads, writes, or deletes) that will
  72. # be issued in EACH thread.
  73. # Default: $NUM_KEYS / $NUM_THREADS
  74. # KEY_SIZE: The size of each key in bytes in db_bench. Default: 100.
  75. # VALUE_SIZE: The size of each value in bytes in db_bench. Default: 900.
  76. # CACHE_SIZE: The size of RocksDB block cache used in db_bench. Default: 1G
  77. # STATISTICS: If 1, then statistics is on in db_bench. Default: 0.
  78. # COMPRESSION_RATIO: The compression ratio of the key generated in db_bench.
  79. # Default: 0.5.
  80. # HISTOGRAM: If 1, then the histogram feature on performance feature is on.
  81. # STATS_PER_INTERVAL: If 1, then the statistics will be reported for every
  82. # STATS_INTERVAL_SECONDS seconds. Default 1.
  83. # STATS_INTERVAL_SECONDS: If STATS_PER_INTERVAL is set to 1, then statistics
  84. # will be reported for every STATS_INTERVAL_SECONDS. Default 60.
  85. # MAX_BACKGROUND_FLUSHES: The maxinum number of concurrent flushes in
  86. # db_bench. Default: 4.
  87. # MAX_BACKGROUND_COMPACTIONS: The maximum number of concurrent compactions
  88. # in db_bench. Default: 16.
  89. # NUM_HIGH_PRI_THREADS: The number of high-pri threads available for
  90. # concurrent flushes in db_bench. Default: 4.
  91. # NUM_LOW_PRI_THREADS: The number of low-pri threads available for
  92. # concurrent compactions in db_bench. Default: 16.
  93. # SEEK_NEXTS: Controls how many Next() will be called after seek.
  94. # Default: 10.
  95. # SEED: random seed that controls the randomness of the benchmark.
  96. # Default: $( date +%s )
  97. #==============================================================================
  98. # CONSTANT
  99. #==============================================================================
  100. TITLE_FORMAT="%40s,%25s,%30s,%7s,%9s,%8s,"
  101. TITLE_FORMAT+="%10s,%13s,%14s,%11s,%12s,"
  102. TITLE_FORMAT+="%7s,%11s,"
  103. TITLE_FORMAT+="%9s,%10s,%10s,%10s,%10s,%10s,%5s,"
  104. TITLE_FORMAT+="%5s,%5s,%5s" # time
  105. TITLE_FORMAT+="\n"
  106. DATA_FORMAT="%40s,%25s,%30s,%7s,%9s,%8s,"
  107. DATA_FORMAT+="%10s,%13.0f,%14s,%11s,%12s,"
  108. DATA_FORMAT+="%7s,%11s,"
  109. DATA_FORMAT+="%9.0f,%10.0f,%10.0f,%10.0f,%10.0f,%10.0f,%5.0f,"
  110. DATA_FORMAT+="%5.0f,%5.0f,%5.0f" # time
  111. DATA_FORMAT+="\n"
  112. MAIN_PATTERN="$1""[[:blank:]]+:.*[[:blank:]]+([0-9\.]+)[[:blank:]]+ops/sec"
  113. PERC_PATTERN="Percentiles: P50: ([0-9\.]+) P75: ([0-9\.]+) "
  114. PERC_PATTERN+="P99: ([0-9\.]+) P99.9: ([0-9\.]+) P99.99: ([0-9\.]+)"
  115. #==============================================================================
  116. function main {
  117. TEST_ROOT_DIR=${TEST_PATH:-"/tmp/rocksdb/regression_test"}
  118. init_arguments $TEST_ROOT_DIR
  119. build_db_bench_and_ldb
  120. setup_test_directory
  121. if [ $TEST_MODE -le 1 ]; then
  122. tmp=$DB_PATH
  123. DB_PATH=$ORIGIN_PATH
  124. test_remote "test -d $DB_PATH"
  125. if [[ $? -ne 0 ]]; then
  126. echo "Building DB..."
  127. # compactall alone will not print ops or threads, which will fail update_report
  128. run_db_bench "fillseq,compactall" $NUM_KEYS 1 0 0
  129. fi
  130. DB_PATH=$tmp
  131. fi
  132. if [ $TEST_MODE -ge 1 ]; then
  133. build_checkpoint
  134. run_db_bench "readrandom"
  135. run_db_bench "readwhilewriting"
  136. run_db_bench "deleterandom" $((NUM_KEYS / 10 / $NUM_THREADS))
  137. run_db_bench "seekrandom"
  138. run_db_bench "seekrandomwhilewriting"
  139. fi
  140. cleanup_test_directory $TEST_ROOT_DIR
  141. echo ""
  142. echo "Benchmark completed! Results are available in $RESULT_PATH"
  143. }
  144. ############################################################################
  145. function init_arguments {
  146. K=1024
  147. M=$((1024 * K))
  148. G=$((1024 * M))
  149. current_time=$(date +"%F-%H:%M:%S")
  150. RESULT_PATH=${RESULT_PATH:-"$1/results/$current_time"}
  151. COMMIT_ID=`git log | head -n1 | cut -c 8-`
  152. SUMMARY_FILE="$RESULT_PATH/SUMMARY.csv"
  153. DB_PATH=${3:-"$1/db"}
  154. ORIGIN_PATH=${ORIGIN_PATH:-"$(dirname $(dirname $DB_PATH))/db"}
  155. WAL_PATH=${4:-""}
  156. if [ -z "$REMOTE_USER_AT_HOST" ]; then
  157. DB_BENCH_DIR=${5:-"."}
  158. else
  159. DB_BENCH_DIR=${5:-"$1/db_bench"}
  160. fi
  161. DEBUG=${DEBUG:-0}
  162. TEST_MODE=${TEST_MODE:-1}
  163. SCP=${SCP:-"scp"}
  164. SSH=${SSH:-"ssh"}
  165. NUM_THREADS=${NUM_THREADS:-16}
  166. NUM_KEYS=${NUM_KEYS:-$((1 * G))} # key range
  167. NUM_OPS=${NUM_OPS:-$(($NUM_KEYS / $NUM_THREADS))}
  168. KEY_SIZE=${KEY_SIZE:-100}
  169. VALUE_SIZE=${VALUE_SIZE:-900}
  170. CACHE_SIZE=${CACHE_SIZE:-$((1 * G))}
  171. STATISTICS=${STATISTICS:-0}
  172. COMPRESSION_RATIO=${COMPRESSION_RATIO:-0.5}
  173. HISTOGRAM=${HISTOGRAM:-1}
  174. NUM_MULTI_DB=${NUM_MULTI_DB:-1}
  175. STATS_PER_INTERVAL=${STATS_PER_INTERVAL:-1}
  176. STATS_INTERVAL_SECONDS=${STATS_INTERVAL_SECONDS:-600}
  177. MAX_BACKGROUND_FLUSHES=${MAX_BACKGROUND_FLUSHES:-4}
  178. MAX_BACKGROUND_COMPACTIONS=${MAX_BACKGROUND_COMPACTIONS:-16}
  179. NUM_HIGH_PRI_THREADS=${NUM_HIGH_PRI_THREADS:-4}
  180. NUM_LOW_PRI_THREADS=${NUM_LOW_PRI_THREADS:-16}
  181. DELETE_TEST_PATH=${DELETE_TEST_PATH:-0}
  182. SEEK_NEXTS=${SEEK_NEXTS:-10}
  183. SEED=${SEED:-$( date +%s )}
  184. }
  185. # $1 --- benchmark name
  186. # $2 --- number of operations. Default: $NUM_KEYS
  187. # $3 --- number of threads. Default $NUM_THREADS
  188. # $4 --- use_existing_db. Default: 1
  189. # $5 --- update_report. Default: 1
  190. function run_db_bench {
  191. # this will terminate all currently-running db_bench
  192. find_db_bench_cmd="ps aux | grep db_bench | grep -v grep | grep -v aux | awk '{print \$2}'"
  193. ops=${2:-$NUM_OPS}
  194. threads=${3:-$NUM_THREADS}
  195. USE_EXISTING_DB=${4:-1}
  196. UPDATE_REPORT=${5:-1}
  197. echo ""
  198. echo "======================================================================="
  199. echo "Benchmark $1"
  200. echo "======================================================================="
  201. echo ""
  202. db_bench_error=0
  203. options_file_arg=$(setup_options_file)
  204. echo "$options_file_arg"
  205. # use `which time` to avoid using bash's internal time command
  206. db_bench_cmd="("'\$(which time)'" -p $DB_BENCH_DIR/db_bench \
  207. --benchmarks=$1 --db=$DB_PATH --wal_dir=$WAL_PATH \
  208. --use_existing_db=$USE_EXISTING_DB \
  209. --disable_auto_compactions \
  210. --threads=$threads \
  211. --num=$NUM_KEYS \
  212. --reads=$ops \
  213. --writes=$ops \
  214. --deletes=$ops \
  215. --key_size=$KEY_SIZE \
  216. --value_size=$VALUE_SIZE \
  217. --cache_size=$CACHE_SIZE \
  218. --statistics=$STATISTICS \
  219. $options_file_arg \
  220. --compression_ratio=$COMPRESSION_RATIO \
  221. --histogram=$HISTOGRAM \
  222. --seek_nexts=$SEEK_NEXTS \
  223. --stats_per_interval=$STATS_PER_INTERVAL \
  224. --stats_interval_seconds=$STATS_INTERVAL_SECONDS \
  225. --max_background_flushes=$MAX_BACKGROUND_FLUSHES \
  226. --num_multi_db=$NUM_MULTI_DB \
  227. --max_background_compactions=$MAX_BACKGROUND_COMPACTIONS \
  228. --num_high_pri_threads=$NUM_HIGH_PRI_THREADS \
  229. --num_low_pri_threads=$NUM_LOW_PRI_THREADS \
  230. --seed=$SEED) 2>&1"
  231. ps_cmd="ps aux"
  232. if ! [ -z "$REMOTE_USER_AT_HOST" ]; then
  233. echo "Running benchmark remotely on $REMOTE_USER_AT_HOST"
  234. db_bench_cmd="$SSH $REMOTE_USER_AT_HOST \"$db_bench_cmd\""
  235. ps_cmd="$SSH $REMOTE_USER_AT_HOST $ps_cmd"
  236. fi
  237. ## make sure no db_bench is running
  238. # The following statement is necessary make sure "eval $ps_cmd" will success.
  239. # Otherwise, if we simply check whether "$(eval $ps_cmd | grep db_bench)" is
  240. # successful or not, then it will always be false since grep will return
  241. # non-zero status when there's no matching output.
  242. ps_output="$(eval $ps_cmd)"
  243. exit_on_error $? "$ps_cmd"
  244. # perform the actual command to check whether db_bench is running
  245. grep_output="$(eval $ps_cmd | grep db_bench | grep -v grep)"
  246. if [ "$grep_output" != "" ]; then
  247. echo "Stopped regression_test.sh as there're still db_bench processes running:"
  248. echo $grep_output
  249. echo "Clean up test directory"
  250. cleanup_test_directory $TEST_ROOT_DIR
  251. exit 2
  252. fi
  253. ## run the db_bench
  254. cmd="($db_bench_cmd || db_bench_error=1) | tee -a $RESULT_PATH/$1"
  255. exit_on_error $?
  256. echo $cmd
  257. eval $cmd
  258. exit_on_error $db_bench_error
  259. if [ $UPDATE_REPORT -ne 0 ]; then
  260. update_report "$1" "$RESULT_PATH/$1" $ops $threads
  261. fi
  262. }
  263. function build_checkpoint {
  264. cmd_prefix=""
  265. if ! [ -z "$REMOTE_USER_AT_HOST" ]; then
  266. cmd_prefix="$SSH $REMOTE_USER_AT_HOST "
  267. fi
  268. if [ $NUM_MULTI_DB -gt 1 ]; then
  269. dirs=$($cmd_prefix find $ORIGIN_PATH -type d -links 2)
  270. for dir in $dirs; do
  271. db_index=$(basename $dir)
  272. echo "Building checkpoints: $ORIGIN_PATH/$db_index -> $DB_PATH/$db_index ..."
  273. $cmd_prefix $DB_BENCH_DIR/ldb checkpoint --checkpoint_dir=$DB_PATH/$db_index \
  274. --db=$ORIGIN_PATH/$db_index 2>&1
  275. done
  276. else
  277. # checkpoint cannot build in directory already exists
  278. $cmd_prefix rm -rf $DB_PATH
  279. echo "Building checkpoint: $ORIGIN_PATH -> $DB_PATH ..."
  280. $cmd_prefix $DB_BENCH_DIR/ldb checkpoint --checkpoint_dir=$DB_PATH \
  281. --db=$ORIGIN_PATH 2>&1
  282. fi
  283. }
  284. function multiply {
  285. echo "$1 * $2" | bc
  286. }
  287. # $1 --- name of the benchmark
  288. # $2 --- the filename of the output log of db_bench
  289. function update_report {
  290. main_result=`cat $2 | grep $1`
  291. exit_on_error $?
  292. perc_statement=`cat $2 | grep Percentile`
  293. exit_on_error $?
  294. # Obtain micros / op
  295. [[ $main_result =~ $MAIN_PATTERN ]]
  296. ops_per_s=${BASH_REMATCH[1]}
  297. # Obtain percentile information
  298. [[ $perc_statement =~ $PERC_PATTERN ]]
  299. perc[0]=${BASH_REMATCH[1]} # p50
  300. perc[1]=${BASH_REMATCH[2]} # p75
  301. perc[2]=${BASH_REMATCH[3]} # p99
  302. perc[3]=${BASH_REMATCH[4]} # p99.9
  303. perc[4]=${BASH_REMATCH[5]} # p99.99
  304. # Parse the output of the time command
  305. real_sec=`tail -3 $2 | grep real | awk '{print $2}'`
  306. user_sec=`tail -3 $2 | grep user | awk '{print $2}'`
  307. sys_sec=`tail -3 $2 | grep sys | awk '{print $2}'`
  308. (printf "$DATA_FORMAT" \
  309. $COMMIT_ID $1 $REMOTE_USER_AT_HOST $NUM_MULTI_DB $NUM_KEYS $KEY_SIZE $VALUE_SIZE \
  310. $(multiply $COMPRESSION_RATIO 100) \
  311. $3 $4 $CACHE_SIZE \
  312. $MAX_BACKGROUND_FLUSHES $MAX_BACKGROUND_COMPACTIONS \
  313. $ops_per_s \
  314. $(multiply ${perc[0]} 1000) \
  315. $(multiply ${perc[1]} 1000) \
  316. $(multiply ${perc[2]} 1000) \
  317. $(multiply ${perc[3]} 1000) \
  318. $(multiply ${perc[4]} 1000) \
  319. $DEBUG \
  320. $real_sec \
  321. $user_sec \
  322. $sys_sec \
  323. >> $SUMMARY_FILE)
  324. exit_on_error $?
  325. }
  326. function exit_on_error {
  327. if [ $1 -ne 0 ]; then
  328. echo ""
  329. echo "ERROR: Benchmark did not complete successfully."
  330. if ! [ -z "$2" ]; then
  331. echo "Failure command: $2"
  332. fi
  333. echo "Partial results are output to $RESULT_PATH"
  334. echo "ERROR" >> $SUMMARY_FILE
  335. exit $1
  336. fi
  337. }
  338. function checkout_rocksdb {
  339. echo "Checking out commit $1 ..."
  340. git fetch --all
  341. exit_on_error $?
  342. git checkout $1
  343. exit_on_error $?
  344. }
  345. function build_db_bench_and_ldb {
  346. echo "Building db_bench & ldb ..."
  347. make clean
  348. exit_on_error $?
  349. DEBUG_LEVEL=0 PORTABLE=1 make db_bench ldb -j32
  350. exit_on_error $?
  351. }
  352. function run_remote {
  353. test_remote "$1"
  354. exit_on_error $? "$1"
  355. }
  356. function test_remote {
  357. if ! [ -z "$REMOTE_USER_AT_HOST" ]; then
  358. cmd="$SSH $REMOTE_USER_AT_HOST '$1'"
  359. else
  360. cmd="$1"
  361. fi
  362. eval "$cmd"
  363. }
  364. function run_local {
  365. eval "$1"
  366. exit_on_error $?
  367. }
  368. function setup_options_file {
  369. if ! [ -z "$OPTIONS_FILE" ]; then
  370. if ! [ -z "$REMOTE_USER_AT_HOST" ]; then
  371. options_file="$DB_BENCH_DIR/OPTIONS_FILE"
  372. run_local "$SCP $OPTIONS_FILE $REMOTE_USER_AT_HOST:$options_file"
  373. else
  374. options_file="$OPTIONS_FILE"
  375. fi
  376. echo "--options_file=$options_file"
  377. fi
  378. echo ""
  379. }
  380. function setup_test_directory {
  381. echo "Deleting old regression test directories and creating new ones"
  382. run_remote "rm -rf $DB_PATH"
  383. run_remote "rm -rf $DB_BENCH_DIR"
  384. run_local "rm -rf $RESULT_PATH"
  385. if ! [ -z "$WAL_PATH" ]; then
  386. run_remote "rm -rf $WAL_PATH"
  387. run_remote "mkdir -p $WAL_PATH"
  388. fi
  389. run_remote "mkdir -p $DB_PATH"
  390. run_remote "mkdir -p $DB_BENCH_DIR"
  391. run_remote "ls -l $DB_BENCH_DIR"
  392. if ! [ -z "$REMOTE_USER_AT_HOST" ]; then
  393. run_local "$SCP ./db_bench $REMOTE_USER_AT_HOST:$DB_BENCH_DIR/db_bench"
  394. run_local "$SCP ./ldb $REMOTE_USER_AT_HOST:$DB_BENCH_DIR/ldb"
  395. fi
  396. run_local "mkdir -p $RESULT_PATH"
  397. (printf $TITLE_FORMAT \
  398. "commit id" "benchmark" "user@host" "num-dbs" "key-range" "key-size" \
  399. "value-size" "compress-rate" "ops-per-thread" "num-threads" "cache-size" \
  400. "flushes" "compactions" \
  401. "ops-per-s" "p50" "p75" "p99" "p99.9" "p99.99" "debug" \
  402. "real-sec" "user-sec" "sys-sec" \
  403. >> $SUMMARY_FILE)
  404. exit_on_error $?
  405. }
  406. function cleanup_test_directory {
  407. if [ $DELETE_TEST_PATH -ne 0 ]; then
  408. echo "Clear old regression test directories and creating new ones"
  409. run_remote "rm -rf $DB_PATH"
  410. run_remote "rm -rf $WAL_PATH"
  411. if ! [ -z "$REMOTE_USER_AT_HOST" ]; then
  412. run_remote "rm -rf $DB_BENCH_DIR"
  413. fi
  414. run_remote "rm -rf $1"
  415. else
  416. echo "------------ DEBUG MODE ------------"
  417. echo "DB PATH: $DB_PATH"
  418. echo "WAL PATH: $WAL_PATH"
  419. fi
  420. }
  421. ############################################################################
  422. # shellcheck disable=SC2068
  423. main $@