github.com/weaviate/weaviate@v1.24.6/test/benchmark/remote/run.sh (about)

     1  #!/bin/bash
     2  
     3  PROJECT=semi-automated-benchmarking
     4  ZONE=us-central1-a
     5  INSTANCE=automated-loadtest
     6  GOVERSION=https://go.dev/dl/go1.18.4.linux-amd64.tar.gz
     7  FILE_PREFIX=${FILE_PREFIX:-""}
     8  
     9  set -eou pipefail
    10  
    11  # change to script directory
    12  cd "${0%/*}" || exit
    13  
    14  function main() {
    15    while [[ "$#" -gt 0 ]]; do
    16        case $1 in
    17            --all) run_all; exit 0 ;;
    18            --create_machine) create_machine; exit 0 ;;
    19            --clone_repository) clone_repository; exit 0;;
    20            --delete_machine) delete_machine; exit 0;;
    21            --install_dependencies) install_dependencies; exit 0;;
    22            --benchmark) benchmark; exit 0;;
    23            --prepare) prepare; exit 0;;
    24            --checkout) checkout "$2" ; exit 0;;
    25            --ssh) interactive_ssh; exit 0;;
    26            *) echo "Unknown parameter passed: $1"; exit 1 ;;
    27        esac
    28        shift
    29    done
    30  
    31    print_help
    32  }
    33  
    34  function print_help() {
    35    echo "Valid arguments include:"
    36    echo ""
    37    echo "  --all               Run everything, including machine creation & destruction"
    38    echo "  --prepare           Create Machine & run all the steps prior to benchmark execution"
    39    echo "  --create_machine    Only create machine"
    40    echo "  --delete_machine    Stop & Delete running machine"
    41    echo "  --clone_repository  Clone and checkout Weaviate repo at specified commit"
    42    echo "  --checkout          Checkout arbitrary branch or commit"
    43    echo "  --ssh               Interactive SSH session"
    44  }
    45  
    46  function run_all() {
    47    trap delete_machine EXIT
    48  
    49    prepare
    50    benchmark
    51  }
    52  
    53  function prepare() {
    54    check
    55    create_machine
    56    install_dependencies
    57    clone_repository
    58  }
    59  
    60  function check() {
    61    echo_green "Checking required dependencies"
    62    if ! command -v gcloud &> /dev/null
    63    then
    64        echo_red "Missing gcloud binary"
    65        return 1
    66    fi
    67  
    68    if ! command -v terraform &> /dev/null
    69    then
    70        echo_red "Missing terraform binary"
    71        return 1
    72    fi
    73  
    74    echo "Ready to go!"
    75  }
    76  
    77  function create_machine() {
    78    (cd terraform && terraform init)
    79    (cd terraform && terraform apply -auto-approve)
    80    echo "Sleeping for 10s, so first ssh doesn't fail. This should be improved through polling"
    81    sleep 10
    82  }
    83  
    84  function delete_machine() {
    85    (cd terraform && terraform destroy -auto-approve)
    86  }
    87  
    88  function install_dependencies() {
    89    ssh_command "sudo apt-get update && sudo apt-get install -y git git-lfs curl"
    90    ssh_command "ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts"
    91    install_go
    92    install_docker
    93  }
    94  
    95  function install_go {
    96    ssh_command "curl -Lo go.tar.gz $GOVERSION"
    97    ssh_command "sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go.tar.gz"
    98    ssh_command 'echo '"'"'PATH=$PATH:/usr/local/go/bin'"'"' >> ~/.profile'
    99    ssh_command "go version"
   100  }
   101  
   102  function install_docker() {
   103    ssh_command "if ! command -v docker &> /dev/null; then curl -fsSL https://get.docker.com -o get-docker.sh && sh ./get-docker.sh; fi"
   104    ssh_command "sudo groupadd docker || true"
   105    ssh_command "sudo usermod -aG docker $USER"
   106  }
   107  
   108  function clone_repository() {
   109    ref=$(git rev-parse --abbrev-ref HEAD)
   110    echo_green "Cloning weaviate repo to branch $ref"
   111    ssh_command "cd; [ ! -d weaviate ] && git clone --depth 1 --branch $ref https://github.com/weaviate/weaviate.git weaviate || true"
   112    ssh_command "cd weaviate; git-lfs install; git-lfs pull"
   113  }
   114  
   115  function checkout() {
   116    ref="$1"
   117    ssh_command "cd weaviate; git checkout $ref"
   118  }
   119  
   120  function benchmark() {
   121    echo_green "Run benchmarks on remote machine"
   122    ssh_command "echo "stop all running docker containers"; docker rm -f $(docker ps -q) || true"
   123    ssh_command "cd ~/weaviate; rm test/benchmark/benchmark_results.json || true"
   124    ssh_command "cd ~/weaviate; test/benchmark/run_performance_tracker.sh"
   125    echo_green "Copy results file to local machine"
   126    filename="${FILE_PREFIX}benchmark_results_$(date +%s).json"
   127    scp_command "$INSTANCE:~/weaviate/test/benchmark/benchmark_results.json" "$filename"
   128    echo "Results file succesfully copied to ${PWD}/$filename"
   129  }
   130  
   131  
   132  function echo_green() {
   133    green='\033[0;32m'
   134    nc='\033[0m' 
   135    echo -e "${green}${*}${nc}"
   136  }
   137  
   138  function echo_red() {
   139    red='\033[0;31m'
   140    nc='\033[0m' 
   141    echo -e "${red}${*}${nc}"
   142  }
   143  
   144  function ssh_command() {
   145    gcloud beta compute ssh --project=$PROJECT --zone=$ZONE  "$INSTANCE" --command="source ~/.profile; $1"
   146  }
   147  
   148  function scp_command() {
   149    gcloud beta compute scp --project=$PROJECT --zone=$ZONE  "$@"
   150  }
   151  
   152  function interactive_ssh() {
   153    gcloud beta compute ssh --project=$PROJECT --zone=$ZONE  "$INSTANCE"
   154  }
   155  
   156  main "$@"