cloudflare/ClickHouse
Publicmirrored fromhttps://github.com/cloudflare/ClickHouse
benchmark/benchmark.sh
180lines · modecode
| 1 | #!/usr/bin/env bash |
| 2 | # script to run query to databases |
| 3 | |
| 4 | function usage() |
| 5 | { |
| 6 | cat <<EOF |
| 7 | usage: $0 options |
| 8 | |
| 9 | This script run benhmark for database |
| 10 | |
| 11 | OPTIONS: |
| 12 | -c config file where some script variables are defined |
| 13 | -n table name |
| 14 | |
| 15 | -h Show this message |
| 16 | -t how many times execute each query. default is '3' |
| 17 | -q query file |
| 18 | -e expect file |
| 19 | -s /etc/init.d/service |
| 20 | -p table name pattern to be replaced to name. default is 'hits_10m' |
| 21 | EOF |
| 22 | } |
| 23 | |
| 24 | TIMES=3 |
| 25 | table_name_pattern=hits_10m |
| 26 | |
| 27 | while getopts “c:ht:n:q:e:s:r” OPTION |
| 28 | do |
| 29 | case $OPTION in |
| 30 | c) |
| 31 | source $OPTARG |
| 32 | ;; |
| 33 | ?) |
| 34 | ;; |
| 35 | esac |
| 36 | done |
| 37 | |
| 38 | OPTIND=1 |
| 39 | |
| 40 | while getopts “c:ht:n:q:e:s:r” OPTION |
| 41 | do |
| 42 | case $OPTION in |
| 43 | h) |
| 44 | usage |
| 45 | exit 0 |
| 46 | ;; |
| 47 | t) |
| 48 | TIMES=$OPTARG |
| 49 | ;; |
| 50 | n) |
| 51 | table_name=$OPTARG |
| 52 | ;; |
| 53 | q) |
| 54 | test_file=$OPTARG |
| 55 | ;; |
| 56 | e) |
| 57 | expect_file=$OPTARG |
| 58 | ;; |
| 59 | s) |
| 60 | etc_init_d_service=$OPTARG |
| 61 | ;; |
| 62 | p) |
| 63 | table_name_pattern=$OPTARG |
| 64 | ;; |
| 65 | c) |
| 66 | ;; |
| 67 | r) |
| 68 | restart_server_each_query=1 |
| 69 | ;; |
| 70 | ?) |
| 71 | usage |
| 72 | exit 0 |
| 73 | ;; |
| 74 | esac |
| 75 | done |
| 76 | |
| 77 | if [[ ! -f $expect_file ]]; then |
| 78 | echo "Not found: expect file" |
| 79 | exit 1 |
| 80 | fi |
| 81 | if [[ ! -f $test_file ]]; then |
| 82 | echo "Not found: test file" |
| 83 | exit 1 |
| 84 | fi |
| 85 | |
| 86 | if [[ ! -f $etc_init_d_service ]]; then |
| 87 | echo "Not found: /etc/init.d/service with path=$etc_init_d_service" |
| 88 | use_service=0 |
| 89 | else |
| 90 | use_service=1 |
| 91 | fi |
| 92 | |
| 93 | if [[ "$table_name_pattern" == "" ]]; then |
| 94 | echo "Empty table_name_pattern" |
| 95 | exit 1 |
| 96 | fi |
| 97 | if [[ "$table_name" == "" ]]; then |
| 98 | echo "Empty table_name" |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
| 102 | function execute() |
| 103 | { |
| 104 | queries=("${@}") |
| 105 | queries_count=${#queries[@]} |
| 106 | |
| 107 | if [ -z $TIMES ]; then |
| 108 | TIMES=1 |
| 109 | fi |
| 110 | |
| 111 | index=0 |
| 112 | while [ "$index" -lt "$queries_count" ]; do |
| 113 | query=${queries[$index]} |
| 114 | |
| 115 | if [[ $query == "" ]]; then |
| 116 | let "index = $index + 1" |
| 117 | continue |
| 118 | fi |
| 119 | |
| 120 | comment_re='--.*' |
| 121 | if [[ $query =~ $comment_re ]]; then |
| 122 | echo "$query" |
| 123 | echo |
| 124 | else |
| 125 | sync |
| 126 | sudo sh -c "echo 3 > /proc/sys/vm/drop_caches" |
| 127 | |
| 128 | if [[ "$restart_server_each_query" == "1" && "$use_service" == "1" ]]; then |
| 129 | echo "restart server: $etc_init_d_service restart" |
| 130 | sudo $etc_init_d_service restart |
| 131 | fi |
| 132 | |
| 133 | for i in $(seq $TIMES) |
| 134 | do |
| 135 | if [[ -f $etc_init_d_service && "$use_service" == "1" ]]; then |
| 136 | sudo $etc_init_d_service status |
| 137 | server_status=$? |
| 138 | expect -f $expect_file "" |
| 139 | |
| 140 | if [[ "$?" != "0" || $server_status != "0" ]]; then |
| 141 | echo "restart server: $etc_init_d_service restart" |
| 142 | sudo $etc_init_d_service restart |
| 143 | fi |
| 144 | |
| 145 | #wait until can connect to server |
| 146 | restart_timer=0 |
| 147 | restart_limit=60 |
| 148 | expect -f $expect_file "" &> /dev/null |
| 149 | while [ "$?" != "0" ]; do |
| 150 | echo "waiting" |
| 151 | sleep 1 |
| 152 | let "restart_timer = $restart_timer + 1" |
| 153 | if (( $restart_limit < $restart_timer )); then |
| 154 | sudo $etc_init_d_service restart |
| 155 | restart_timer=0 |
| 156 | fi |
| 157 | expect -f $expect_file "" &> /dev/null |
| 158 | done |
| 159 | fi |
| 160 | |
| 161 | echo |
| 162 | echo "times: $i" |
| 163 | |
| 164 | echo "query:" "$query" |
| 165 | expect -f $expect_file "$query" |
| 166 | |
| 167 | done |
| 168 | fi |
| 169 | |
| 170 | let "index = $index + 1" |
| 171 | done |
| 172 | } |
| 173 | |
| 174 | temp_test_file=temp_queries_$table_name |
| 175 | cat $test_file | sed s/$table_name_pattern/$table_name/g > $temp_test_file |
| 176 | mapfile -t test_queries < $temp_test_file |
| 177 | |
| 178 | echo "start time: $(date)" |
| 179 | time execute "${test_queries[@]}" |
| 180 | echo "stop time: $(date)" |