#!/bin/bash
DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
cd "$DIR/.."
set -x
SSH_KEY=~/.ssh/id_ed25519

# Create server.

# We must use us-central1 to have access to N2D, with the new AMD CPUs.
gcloud compute instances create shishua-amd \
  --machine-type=n2d-standard-2 \
  --maintenance-policy=TERMINATE \
  --zone=us-central1-f \
  --image-project=ubuntu-os-cloud --image-family=ubuntu-2004-lts
tar cJf shishua.tar.xz $(git ls-files)
while ! gcloud compute ssh shishua-amd --ssh-key-file="$SSH_KEY" \
    --zone=us-central1-f -- 'echo sshd started.'; do
  echo Awaiting sshd…
  sleep 10
done

# Run benchmark.

gcloud compute scp ./shishua.tar.xz shishua-amd:~ --ssh-key-file="$SSH_KEY" --zone=us-central1-f
gcloud compute ssh shishua-amd --ssh-key-file="$SSH_KEY" --zone=us-central1-f -- 'tar xJf shishua.tar.xz && ./bin/perf-cloud-ubuntu-gcc.sh'
gcloud compute scp shishua-amd:~/test/benchmark-perf test/benchmark-perf-amd --ssh-key-file="$SSH_KEY" --zone=us-central1-f

# Cleaning things up.

rm shishua.tar.xz
gcloud compute instances delete shishua-amd --zone=us-central1-f