|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | | -# This script runs a benchmark on a locally started etcd server |
| 16 | +# This utility script helps to set up the test environment for benchmark tests and runs the provided tests sequentially. |
17 | 17 |
|
18 | 18 | set -euo pipefail |
19 | 19 |
|
20 | 20 | source ./scripts/test_lib.sh |
21 | 21 |
|
22 | | -COMMON_BENCHMARK_FLAGS="--report-perfdash" |
| 22 | +declare -A BENCHMARK_FLAGS |
| 23 | +BENCHMARKS_TO_RUN=() |
| 24 | +COMMON_BENCHMARK_FLAGS=(--report-perfdash) |
23 | 25 |
|
24 | | -if [[ $# -lt 1 ]]; then |
25 | | - echo "Usage: $0 <benchmark-name> [tester args...]" |
26 | | - exit 1 |
27 | | -fi |
| 26 | +parse_args() { |
| 27 | + for arg in "$@"; do |
| 28 | + if [[ "$arg" == *:* ]]; then |
| 29 | + key="${arg%%:*}" |
| 30 | + value="${arg#*:}" |
| 31 | + BENCHMARKS_TO_RUN+=("$key") |
| 32 | + BENCHMARK_FLAGS["$key"]="$value" |
| 33 | + else |
| 34 | + BENCHMARKS_TO_RUN+=("$arg") |
| 35 | + BENCHMARK_FLAGS["$arg"]="" |
| 36 | + fi |
| 37 | + done |
28 | 38 |
|
29 | | -BENCHMARK_NAME="$1" |
30 | | -ARGS="${*:2}" |
| 39 | + if [ ${#BENCHMARKS_TO_RUN[@]} -eq 0 ]; then |
| 40 | + log_error "Usage: ./benchmark_test.sh 'benchmark-operation:\"test_arguments\"'" |
| 41 | + log_error "Example: ./benchmark_test.sh 'put:\"--clients=1000\" range:\"--conns=100\"'" |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +} |
31 | 45 |
|
32 | | -echo "Starting the etcd server..." |
| 46 | +start_etcd() { |
| 47 | + log_callout "Setup: Starting the etcd server..." |
| 48 | + # Create a directory for etcd data under /tmp/etcd |
| 49 | + mkdir -p /tmp/etcd |
| 50 | + DATA_DIR=$(mktemp -d /tmp/etcd/data-XXXXXX) |
| 51 | + ARTIFACTS_DIR="${ARTIFACTS:-./_artifacts}" |
| 52 | + mkdir -p "$ARTIFACTS_DIR" |
| 53 | + ETCD_LOG_FILE="${ARTIFACTS_DIR}/etcd-${bench}-$(date +%Y%m%d-%H%M%S).log" |
| 54 | + log_callout -e "Setup: etcd log file path set to $ETCD_LOG_FILE. DATA_DIR set to $DATA_DIR" |
| 55 | + ./bin/etcd --data-dir="$DATA_DIR" > "${ETCD_LOG_FILE}" 2>&1 & |
| 56 | + ETCD_PID=$! |
| 57 | + RETRY=0 |
| 58 | + MAX_RETRY_ATTEMPTS=10 |
33 | 59 |
|
34 | | -# Create a directory for etcd data under /tmp/etcd |
35 | | -mkdir -p /tmp/etcd |
36 | | -DATA_DIR=$(mktemp -d /tmp/etcd/data-XXXXXX) |
37 | | -./bin/etcd --data-dir="$DATA_DIR" > /tmp/etcd.log 2>&1 & |
38 | | -etcd_pid=$! |
| 60 | + # Set up the trap to handle errors/interrupts which leads to cleaning up of the DATADIR and stopping the etcd server. |
| 61 | + trap 'stop_and_cleanup_etcd' EXIT |
39 | 62 |
|
40 | | -trap 'log_warning -e "Stopping etcd server - PID $etcd_pid"; |
41 | | - kill $etcd_pid 2>/dev/null; |
42 | | - rm -rf $DATA_DIR; |
43 | | - log_success "Deleted the contents from $DATA_DIR related to benchmark test"' EXIT |
| 63 | + # Poll until etcd is healthy |
| 64 | + until curl -fs http://127.0.0.1:2379/health | grep -q '"health":"true"'; do |
| 65 | + RETRY=$((RETRY + 1)) |
| 66 | + if [[ $RETRY -gt $MAX_RETRY_ATTEMPTS ]]; then |
| 67 | + log_error -e "Setup: Failed to confirm etcd health after $MAX_RETRY_ATTEMPTS attempts." |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + log_warning -e "Setup: Waiting for etcd to be healthy... (retry: $RETRY/$MAX_RETRY_ATTEMPTS)" |
| 71 | + sleep 1 |
| 72 | + done |
| 73 | + log_success -e "Setup: etcd is healthy and running on pid $ETCD_PID" |
| 74 | +} |
44 | 75 |
|
45 | | -# Wait until etcd becomes healthy |
46 | | -for retry in {1..10}; do |
47 | | - if ./bin/etcdctl endpoint health --cluster> /dev/null 2>&1; then |
48 | | - log_success -e "\\netcd is healthy" |
49 | | - break |
50 | | - fi |
51 | | - log_warning -e "\\nWaiting for etcd to be healthy..." |
52 | | - sleep 1 |
53 | | - if [[ $retry -eq 10 ]]; then |
54 | | - log_error -e "\\nFailed to confirm etcd health after $retry attempts. Check /tmp/etcd.log for more information" |
55 | | - exit 1 |
| 76 | +stop_and_cleanup_etcd() { |
| 77 | + trap - EXIT |
| 78 | + log_warning -e "Cleanup: Stopping etcd server - PID $ETCD_PID" |
| 79 | + kill "$ETCD_PID" 2>/dev/null || true |
| 80 | + rm -rf "$DATA_DIR" |
| 81 | + log_success "Cleanup: Deleted the DATA_DIR contents from $DATA_DIR related to benchmark test" |
| 82 | +} |
| 83 | + |
| 84 | +run_benchmark() { |
| 85 | + local bench=$1 |
| 86 | + local args="${BENCHMARK_FLAGS[$bench]:-}" |
| 87 | + |
| 88 | + if [[ -z "$args" ]]; then |
| 89 | + log_callout -e "\\nPerforming benchmark $bench with default arguments" |
| 90 | + benchmark "$bench" "${COMMON_BENCHMARK_FLAGS[@]}" |
| 91 | + else |
| 92 | + log_callout -e "\\nPerforming benchmark $bench with arguments: $args" |
| 93 | + read -r -a TESTER_OPTIONS <<< "$args" |
| 94 | + |
| 95 | + printf "Running: benchmark %s %s %s\n" \ |
| 96 | + "$bench" "${TESTER_OPTIONS[*]}" "${COMMON_BENCHMARK_FLAGS[*]}" |
| 97 | + |
| 98 | + benchmark "$bench" "${TESTER_OPTIONS[@]}" "${COMMON_BENCHMARK_FLAGS[@]}" |
56 | 99 | fi |
57 | | -done |
| 100 | +} |
| 101 | + |
| 102 | +main() { |
| 103 | + parse_args "$@" |
58 | 104 |
|
59 | | -log_success -e "etcd process is running with PID $etcd_pid" |
| 105 | + for bench in "${BENCHMARKS_TO_RUN[@]}"; do |
| 106 | + start_etcd |
| 107 | + run_benchmark "$bench" |
| 108 | + stop_and_cleanup_etcd |
| 109 | + done |
| 110 | +} |
60 | 111 |
|
61 | | -log_callout -e "\\nPerforming benchmark $BENCHMARK_NAME with arguments: $ARGS" |
62 | | -read -r -a TESTER_OPTIONS <<< "$ARGS" |
63 | | -log_callout "Running: benchmark $BENCHMARK_NAME ${TESTER_OPTIONS[*]} $COMMON_BENCHMARK_FLAGS" |
64 | | -benchmark "$BENCHMARK_NAME" "${TESTER_OPTIONS[@]}" $COMMON_BENCHMARK_FLAGS |
65 | | -log_callout "Completed: benchmark $BENCHMARK_NAME ${TESTER_OPTIONS[*]} $COMMON_BENCHMARK_FLAGS" |
| 112 | +main "$@" |
0 commit comments