Skip to content

Instantly share code, notes, and snippets.

@yomaokobiah
Created March 10, 2025 14:31
Show Gist options
  • Select an option

  • Save yomaokobiah/5da837f38fe9d1d9423ce55d34a034f9 to your computer and use it in GitHub Desktop.

Select an option

Save yomaokobiah/5da837f38fe9d1d9423ce55d34a034f9 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
read -p "Enter the project name: " project_name
cudoctl projects create $project_name
CONFIGS=(
"benchmarkvm1 us-santaclara-1 intel-broadwell ubuntu-2204-nvidia-535-docker-v20241017 1 1 10"
"benchmarkvm2 us-santaclara-1 intel-broadwell ubuntu-2204-nvidia-535-docker-v20241017 2 2 15"
"benchmarkvm3 us-santaclara-1 intel-broadwell ubuntu-2204-nvidia-535-docker-v20241017 4 4 20"
)
RESULTS_FILE="benchmark_results.txt"
echo "VM Name, VCPU, Memory, Disk_Size, CPU Events/sec" > $RESULTS_FILE
create_vm() {
local vm_name=$1
local data_center=$2
local machine_type=$3
local image=$4
local vcpu=$5
local memory=$6
local disk_size=$7
echo "Creating VM: $vm_name ($vcpu CPUs, $memory RAM, $disk_size Disk) in $data_center..."
VM_ID=$(cudoctl vm create -project "$project_name" -id "$vm_name" -data-center "$data_center" -machine-type "$machine_type" \
-image "$image" -vcpus "$vcpu" -memory "$memory" -boot-disk-size "$disk_size")
if [[ $VM_ID -gt 0 ]]; then
echo "Failed to create VM: $vm_name"
exit 1
fi
echo "creating vm"
echo $vm_name
}
run_benchmark() {
local vm_id=$1
local vcpu=$2
local memory=$3
local disk_size=$4
echo "Running benchmark on (VM ID: $vm_id)..."
IPS=($(cudoctl vm list | grep -A 2 "$vm_id" | grep externalIP | awk '{print $2}'))
if [[ ${#IPS[@]} -eq 0 ]]; then
echo "No IPs found for VM $vm_id, waiting 30 seconds and trying again..."
sleep 30
IPS=($(cudoctl vm list | grep -A 2 "$vm_id" | grep externalIP | awk '{print $2}'))
if [[ ${#IPS[@]} -eq 0 ]]; then
echo "Still no IPs found for VM $vm_id"
return 1
fi
fi
echo "Found ${#IPS[@]} IP(s) for VM $vm_id"
for ip in "${IPS[@]}"; do
echo "Attempting to connect to IP: $ip"
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o BatchMode=yes root@$ip echo "SSH Ready" &>/dev/null; then
echo "SSH connection to $ip successful"
OUTPUT=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=30 root@$ip -v << "EOF"
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update -y
sudo apt-get install -y sysbench
sysbench cpu --threads=4 run
EOF
) || { echo "Benchmark failed on $ip"; continue; }
CPU_EVENTS=$(echo "$OUTPUT" | grep "events per second" | awk '{print $4}')
if [[ -n "$CPU_EVENTS" ]]; then
echo "$vm_id, $vcpu, $memory, $disk_size, $CPU_EVENTS" >> $RESULTS_FILE
echo "Benchmark completed successfully: $CPU_EVENTS events/sec"
return 0
else
echo "Failed to extract benchmark results from $ip"
fi
else
echo "SSH connection to $ip failed, trying next IP if available..."
fi
done
echo "Failed to run benchmark on any available IP for VM $vm_id"
return 1
}
destroy_vm() {
local vm_id=$1
echo "Destroying VM: (VM ID: $vm_id)..."
cudoctl vm delete "$vm_id"
}
for CONFIG in "${CONFIGS[@]}"; do
set -- $CONFIG
VM_NAME=$1
DATA_CENTER=$2
MACHINE_TYPE=$3
IMAGE=$4
VCPU=$5
MEMORY=$6
DISK_SIZE=$7
create_vm "$VM_NAME" "$DATA_CENTER" "$MACHINE_TYPE" "$IMAGE" "$VCPU" "$MEMORY" "$DISK_SIZE"
echo "Waiting for VM to initialize..."
echo "---------------------------------------"
done
sleep 60
echo '' > ~/.ssh/known_hosts
for CONFIG in "${CONFIGS[@]}"; do
set -- $CONFIG
VM_NAME=$1
VCPU=$5
MEMORY=$6
DISK_SIZE=$7
run_benchmark "$VM_NAME" "$VCPU" "$MEMORY" "$DISK_SIZE"
destroy_vm "$VM_NAME"
done
echo "Benchmarking complete. Results saved in $RESULTS_FILE and VMs deleted."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment