benchmark_leveldb.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env bash
  2. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. # REQUIRE: db_bench binary exists in the current directory
  4. #
  5. # This should be used with the LevelDB fork listed here to use additional test options.
  6. # For more details on the changes see the blog post listed below.
  7. # https://github.com/mdcallag/leveldb-1
  8. # http://smalldatum.blogspot.com/2015/04/comparing-leveldb-and-rocksdb-take-2.html
  9. if [ $# -ne 1 ]; then
  10. echo -n "./benchmark.sh [fillseq/overwrite/readrandom/readwhilewriting]"
  11. exit 0
  12. fi
  13. # size constants
  14. K=1024
  15. M=$((1024 * K))
  16. G=$((1024 * M))
  17. if [ -z $DB_DIR ]; then
  18. echo "DB_DIR is not defined"
  19. exit 0
  20. fi
  21. output_dir=${OUTPUT_DIR:-/tmp/}
  22. if [ ! -d $output_dir ]; then
  23. mkdir -p $output_dir
  24. fi
  25. # all multithreaded tests run with sync=1 unless
  26. # $DB_BENCH_NO_SYNC is defined
  27. syncval="1"
  28. if [ ! -z $DB_BENCH_NO_SYNC ]; then
  29. echo "Turning sync off for all multithreaded tests"
  30. syncval="0";
  31. fi
  32. num_threads=${NUM_THREADS:-16}
  33. # Only for *whilewriting, *whilemerging
  34. writes_per_second=${WRITES_PER_SECOND:-$((10 * K))}
  35. cache_size=${CACHE_SIZE:-$((1 * G))}
  36. num_keys=${NUM_KEYS:-$((1 * G))}
  37. key_size=20
  38. value_size=${VALUE_SIZE:-400}
  39. block_size=${BLOCK_SIZE:-4096}
  40. const_params="
  41. --db=$DB_DIR \
  42. \
  43. --num=$num_keys \
  44. --value_size=$value_size \
  45. --cache_size=$cache_size \
  46. --compression_ratio=0.5 \
  47. \
  48. --write_buffer_size=$((2 * M)) \
  49. \
  50. --histogram=1 \
  51. \
  52. --bloom_bits=10 \
  53. --open_files=$((20 * K))"
  54. params_w="$const_params "
  55. function summarize_result {
  56. test_out=$1
  57. test_name=$2
  58. bench_name=$3
  59. nthr=$4
  60. usecs_op=$( grep ^${bench_name} $test_out | awk '{ printf "%.1f", $3 }' )
  61. mb_sec=$( grep ^${bench_name} $test_out | awk '{ printf "%.1f", $5 }' )
  62. ops=$( grep "^Count:" $test_out | awk '{ print $2 }' )
  63. ops_sec=$( echo "scale=0; (1000000.0 * $nthr) / $usecs_op" | bc )
  64. avg=$( grep "^Count:" $test_out | awk '{ printf "%.1f", $4 }' )
  65. p50=$( grep "^Min:" $test_out | awk '{ printf "%.1f", $4 }' )
  66. echo -e "$ops_sec\t$mb_sec\t$usecs_op\t$avg\t$p50\t$test_name" \
  67. >> $output_dir/report.txt
  68. }
  69. function run_fillseq {
  70. # This runs with a vector memtable and the WAL disabled to load faster. It is still crash safe and the
  71. # client can discover where to restart a load after a crash. I think this is a good way to load.
  72. echo "Loading $num_keys keys sequentially"
  73. cmd="./db_bench --benchmarks=fillseq \
  74. --use_existing_db=0 \
  75. --sync=0 \
  76. $params_w \
  77. --threads=1 \
  78. --seed=$( date +%s ) \
  79. 2>&1 | tee -a $output_dir/benchmark_fillseq.v${value_size}.log"
  80. echo $cmd | tee $output_dir/benchmark_fillseq.v${value_size}.log
  81. eval $cmd
  82. summarize_result $output_dir/benchmark_fillseq.v${value_size}.log fillseq.v${value_size} fillseq 1
  83. }
  84. function run_change {
  85. operation=$1
  86. echo "Do $num_keys random $operation"
  87. out_name="benchmark_${operation}.t${num_threads}.s${syncval}.log"
  88. cmd="./db_bench --benchmarks=$operation \
  89. --use_existing_db=1 \
  90. --sync=$syncval \
  91. $params_w \
  92. --threads=$num_threads \
  93. --seed=$( date +%s ) \
  94. 2>&1 | tee -a $output_dir/${out_name}"
  95. echo $cmd | tee $output_dir/${out_name}
  96. eval $cmd
  97. summarize_result $output_dir/${out_name} ${operation}.t${num_threads}.s${syncval} $operation $num_threads
  98. }
  99. function run_readrandom {
  100. echo "Reading $num_keys random keys"
  101. out_name="benchmark_readrandom.t${num_threads}.log"
  102. cmd="./db_bench --benchmarks=readrandom \
  103. --use_existing_db=1 \
  104. $params_w \
  105. --threads=$num_threads \
  106. --seed=$( date +%s ) \
  107. 2>&1 | tee -a $output_dir/${out_name}"
  108. echo $cmd | tee $output_dir/${out_name}
  109. eval $cmd
  110. summarize_result $output_dir/${out_name} readrandom.t${num_threads} readrandom $num_threads
  111. }
  112. function run_readwhile {
  113. operation=$1
  114. echo "Reading $num_keys random keys while $operation"
  115. out_name="benchmark_readwhile${operation}.t${num_threads}.log"
  116. cmd="./db_bench --benchmarks=readwhile${operation} \
  117. --use_existing_db=1 \
  118. --sync=$syncval \
  119. $params_w \
  120. --threads=$num_threads \
  121. --writes_per_second=$writes_per_second \
  122. --seed=$( date +%s ) \
  123. 2>&1 | tee -a $output_dir/${out_name}"
  124. echo $cmd | tee $output_dir/${out_name}
  125. eval $cmd
  126. summarize_result $output_dir/${out_name} readwhile${operation}.t${num_threads} readwhile${operation} $num_threads
  127. }
  128. function now() {
  129. echo `date +"%s"`
  130. }
  131. report="$output_dir/report.txt"
  132. schedule="$output_dir/schedule.txt"
  133. echo "===== Benchmark ====="
  134. # Run!!!
  135. IFS=',' read -a jobs <<< $1
  136. # shellcheck disable=SC2068
  137. for job in ${jobs[@]}; do
  138. if [ $job != debug ]; then
  139. echo "Start $job at `date`" | tee -a $schedule
  140. fi
  141. start=$(now)
  142. if [ $job = fillseq ]; then
  143. run_fillseq
  144. elif [ $job = overwrite ]; then
  145. run_change overwrite
  146. elif [ $job = readrandom ]; then
  147. run_readrandom
  148. elif [ $job = readwhilewriting ]; then
  149. run_readwhile writing
  150. elif [ $job = debug ]; then
  151. num_keys=1000; # debug
  152. echo "Setting num_keys to $num_keys"
  153. else
  154. echo "unknown job $job"
  155. exit
  156. fi
  157. end=$(now)
  158. if [ $job != debug ]; then
  159. echo "Complete $job in $((end-start)) seconds" | tee -a $schedule
  160. fi
  161. echo -e "ops/sec\tmb/sec\tusec/op\tavg\tp50\tTest"
  162. tail -1 $output_dir/report.txt
  163. done