#!/usr/bin/env bash
# 10.77.8.4/stress_test/heartbeat

JP='192.168.168.106'
MAN_IP=$(ip -o r get $JP|sed -n 's/.*src \([0-9.]*\) .*/\1/p')

if lspci -n |grep -q 10de: ; then
    curl -s -XPUT http://10.179.234.108:8500/v1/agent/check/pass/service:$MAN_IP
    wget -qN 10.77.8.4/stress_test/scripts/modules/gpu-temp-pwr.sh -P /scripts/modules/
    wget -qN 10.77.8.4/stress_test/scripts/modules/loop-test-throughput.py -P /scripts/gpu-burn/
fi


wget -qN 10.77.8.4/stress_test/scripts/modules/lib.sh -P /scripts/modules/

pangu_key=$(curl -sSLf softlib.service.ucloud.cn/usa/usage_key/pangu.pub)
[[ $pangu_key ]] && ! grep -q "$pangu_key" /root/.ssh/authorized_keys && echo "$pangu_key" >> /root/.ssh/authorized_keys
unset pangu_key

source /scripts/modules/lib.sh
source /tmp/UCLOUD_GPU_DEMAG
source /tmp/UCLOUD_TEST_SEL

if [[ $UCLOUD_GPU_DEMAG != 0 ]] ||  [[ $UCLOUD_TEST_SEL != 0 ]];then
    echo UCLOUD_GPU_DEMAG: $UCLOUD_GPU_DEMAG
    echo UCLOUD_TEST_SEL: $UCLOUD_TEST_SEL
    UPLOAD_LOG error
elif ! GPU_TEMP_PWR ;then
    UPLOAD_LOG error
else
    UPLOAD_LOG testing
fi


grep '=0' /tmp/UCLOUD_GPU_DEMAG && {
    sed -i '/UCLOUD_GPU_DEMAG/d' /mnt/ucloud/exports.ini
    cat /tmp/UCLOUD_GPU_DEMAG >> /mnt/ucloud/exports.ini
}
grep '=0' /tmp/UCLOUD_TEST_SEL && {
    sed -i '/UCLOUD_TEST_SEL/d' /mnt/ucloud/exports.ini
    cat /tmp/UCLOUD_TEST_SEL >> /mnt/ucloud/exports.ini
}


### 压测完成
test_process=$(ps -eo lstart,etime,cmd |grep -v grep|grep -E 'vllm.entrypoints.openai.api_server|gpu-burn|all_reduce_perf|mxvs.x86_64.*appimage|stressapptest' )
if [[ $test_process ]];then
    UPLOAD_LOG testing
else
    UPLOAD_LOG end
fi

