From 197ccf5e7d30d400b7c7ad02260d5ce9436830bf Mon Sep 17 00:00:00 2001 From: "zun.yang" Date: Thu, 27 Jul 2023 23:12:35 +0800 Subject: [PATCH] update --- benchmark/geekbench.license | 1 - benchmark/run.sh | 32 ++++++++------- config.json | 5 ++- go.mod | 4 +- go.sum | 20 ++++++++++ main.go | 77 +++++++++++++++++++++++++------------ 6 files changed, 96 insertions(+), 43 deletions(-) delete mode 100644 benchmark/geekbench.license diff --git a/benchmark/geekbench.license b/benchmark/geekbench.license deleted file mode 100644 index d941ab0..0000000 --- a/benchmark/geekbench.license +++ /dev/null @@ -1 +0,0 @@ -1934531619@qq.com JDN2X-DHS55-OAYJX-JNR32-BFQXZ-ASDRJ-QYANY-CC7NX-JV3QW \ No newline at end of file diff --git a/benchmark/run.sh b/benchmark/run.sh index e4a6d77..8da65d4 100755 --- a/benchmark/run.sh +++ b/benchmark/run.sh @@ -1,13 +1,14 @@ #!/bin/bash export PATH=$PATH:$(pwd)/tools -YYDS_VERSION="v2022-11-24" +MAIN_VERSION="V2022-11-24" BENCHMARK_HOST="${1}" +GEEKBENCH_LICENSE="${2}" echo -e '# ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## #' -echo -e '# RUN-Bench-Script #' -echo -e '# '$YYDS_VERSION' #' -echo -e '# https://gitlab-ee.treesir.pub/devops/benchmark #' +echo -e '# RUN-BenchMark-Script #' +echo -e '# '$MAIN_VERSION' #' +echo -e '# https://gitlab-ee.treesir.pub/devops/benchmark #' echo -e '# ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## #' echo -e @@ -235,7 +236,7 @@ if [ ! -z $JSON ]; then IPV6=$([ ! -z $IPV6_CHECK ] && echo "true" || echo "false") AES=$([[ "$CPU_AES" = *Enabled* ]] && echo "true" || echo "false") VIRT=$([[ "$CPU_VIRT" = *Enabled* ]] && echo "true" || echo "false") - JSON_RESULT='{"version":"'$YYDS_VERSION'","time":"'$TIME_START'","os":{"arch":"'$ARCH'","distro":"'$DISTRO'","kernel":"'$KERNEL'",' + JSON_RESULT='{"version":"'$MAIN_VERSION'","time":"'$TIME_START'","os":{"arch":"'$ARCH'","distro":"'$DISTRO'","kernel":"'$KERNEL'",' JSON_RESULT+='"uptime":'$UPTIME_S'},"net":{"ipv4":'$IPV4',"ipv6":'$IPV6'},"cpu":{"model":"'$CPU_PROC'","cores":'$CPU_CORES',' JSON_RESULT+='"freq":"'$CPU_FREQ'","aes":'$AES',"virt":'$VIRT'},"mem":{"ram":'$TOTAL_RAM_RAW',"swap":'$TOTAL_SWAP_RAW',"disk":'$TOTAL_DISK_RAW'}' fi @@ -767,17 +768,19 @@ function launch_geekbench { if [[ "$ARCH" == *"x86"* ]]; then # check if geekbench file exists - if test -f "geekbench.license"; then - $GEEKBENCH_PATH/geekbench_x86_32 --unlock `cat geekbench.license` > /dev/null 2>&1 - fi + # if test -f "geekbench.license"; then + # $GEEKBENCH_PATH/geekbench_x86_32 --unlock `cat geekbench.license` > /dev/null 2>&1 + # fi + $GEEKBENCH_PATH/geekbench_x86_32 --unlock ${GEEKBENCH_LICENSE} > /dev/null 2>&1 # run the Geekbench 4 test and grep the test results URL given at the end of the test GEEKBENCH_TEST=$($GEEKBENCH_PATH/geekbench_x86_32 --no-upload 2>/dev/null | grep "https://browser") else # check if geekbench file exists - if test -f "geekbench.license"; then - $GEEKBENCH_PATH/geekbench4 --unlock `cat geekbench.license` > /dev/null 2>&1 - fi + # if test -f "geekbench.license"; then + # $GEEKBENCH_PATH/geekbench4 --unlock `cat geekbench.license` > /dev/null 2>&1 + # fi + $GEEKBENCH_PATH/geekbench4 --unlock ${GEEKBENCH_LICENSE} > /dev/null 2>&1 # run the Geekbench 4 test and grep the test results URL given at the end of the test GEEKBENCH_TEST=$($GEEKBENCH_PATH/geekbench4 --upload 2>/dev/null | grep "https://browser") @@ -800,9 +803,10 @@ function launch_geekbench { # fi # check if geekbench file exists - if test -f "geekbench.license"; then - $GEEKBENCH_PATH/geekbench5 --unlock `cat geekbench.license` > /dev/null 2>&1 - fi + # if test -f "geekbench.license"; then + # $GEEKBENCH_PATH/geekbench5 --unlock `cat geekbench.license` > /dev/null 2>&1 + # fi + $GEEKBENCH_PATH/geekbench5 --unlock ${GEEKBENCH_LICENSE} > /dev/null 2>&1 GEEKBENCH_TEST=$($GEEKBENCH_PATH/geekbench5 --no-upload 2>/dev/null) fi diff --git a/config.json b/config.json index fe21510..ca76e6a 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,7 @@ { - "Host": "192.168.8.135,192.168.8.134", + "Host": "192.168.8.188", "Port": 22, "Username": "root", - "Password": "nuli123456" + "Password": "123456", + "Geekbench_License": "1934531619@qq.com JDN2X-DHS55-OAYJX-JNR32-BFQXZ-ASDRJ-QYANY-CC7NX-JV3QW" } \ No newline at end of file diff --git a/go.mod b/go.mod index 1b3b980..0250a58 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,14 @@ module gitlab-ee.treesir.pub/go-projects/ssh-benchmark go 1.20 require ( + github.com/bitfield/script v0.22.0 github.com/bramvdbogaerde/go-scp v1.2.1 + github.com/elulcao/progress-bar v0.1.1 + github.com/sirupsen/logrus v1.9.3 golang.org/x/crypto v0.9.0 ) require ( - github.com/bitfield/script v0.22.0 // indirect github.com/itchyny/gojq v0.12.12 // indirect github.com/itchyny/timefmt-go v0.1.5 // indirect golang.org/x/sys v0.8.0 // indirect diff --git a/go.sum b/go.sum index caebe9e..b83bf0f 100644 --- a/go.sum +++ b/go.sum @@ -4,23 +4,40 @@ github.com/bramvdbogaerde/go-scp v1.2.1 h1:BKTqrqXiQYovrDlfuVFaEGz0r4Ou6EED8L7jC github.com/bramvdbogaerde/go-scp v1.2.1/go.mod h1:s4ZldBoRAOgUg8IrRP2Urmq5qqd2yPXQTPshACY8vQ0= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/elulcao/progress-bar v0.1.1 h1:yLVafDn4pDitkpqomzF5zixDRl0TySLX6unEUuYJ5wE= +github.com/elulcao/progress-bar v0.1.1/go.mod h1:ejyDVliG6R+ekjJY8BHPHFNJiIQV00zyWT7920D/Nls= +github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/renameio/v2 v2.0.0/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4= github.com/itchyny/gojq v0.12.12 h1:x+xGI9BXqKoJQZkr95ibpe3cdrTbY8D9lonrK433rcA= github.com/itchyny/gojq v0.12.12/go.mod h1:j+3sVkjxwd7A7Z5jrbKibgOLn0ZfLWkV+Awxr/pyzJE= github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE= github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= @@ -28,6 +45,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -40,6 +58,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= mvdan.cc/editorconfig v0.2.0/go.mod h1:lvnnD3BNdBYkhq+B4uBuFFKatfp02eB6HixDvEz91C0= mvdan.cc/sh/v3 v3.6.0 h1:gtva4EXJ0dFNvl5bHjcUEvws+KRcDslT8VKheTYkbGU= diff --git a/main.go b/main.go index 3d2c5e2..db72b30 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "log" "math/rand" "os" "os/exec" @@ -14,14 +13,17 @@ import ( "github.com/bitfield/script" "github.com/bramvdbogaerde/go-scp" + pcmd "github.com/elulcao/progress-bar/cmd" + "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" ) type envConfig struct { - Host string `json:"Host"` - Port int `json:"Port"` - Username string `json:"Username"` - Password string `json:"Password"` + Host string `json:"Host"` + Port int `json:"Port"` + Username string `json:"Username"` + Password string `json:"Password"` + Geekbench_License string `json:"Geekbench_License"` } // 在本地启动一个 iperf3 服务端 @@ -30,6 +32,21 @@ func iperfServer() { cmd.Run() } +func pBar(hsotNum int) { + pb := pcmd.NewPBar() + pb.SignalHandler() + pb.Total = uint16(150 * hsotNum) + + // pb.RenderPBar(i) + for i := 1; uint16(i) <= pb.Total; i++ { + pb.RenderPBar(i) + if uint16(i) == pb.Total-1 { + i -= 2 + } + time.Sleep(1 * time.Second) + } +} + func randomString(length int) string { const charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" @@ -46,21 +63,21 @@ func randomString(length int) string { // 封装 远程执行 shell 命令 func remoteShellExec(rClient ssh.Client, commands []string, logOut bool) error { - outputStr := "" for _, command := range commands { session, err := rClient.NewSession() if err != nil { - log.Fatal("Failed to create session: ", err) + logrus.Errorf("Failed to create session: %s", err.Error()) } defer session.Close() output, err := session.CombinedOutput(command) if err != nil { - log.Fatal("Failed to run: ", err) + logrus.Errorf("Failed to run: %s", err.Error()) } outputStr = string(output) if logOut { - log.Println(outputStr) + fmt.Println(" ") + fmt.Println(outputStr) } } return nil @@ -71,7 +88,7 @@ func testSSH(rClient ssh.Client) bool { session, err := rClient.NewSession() if err != nil { - log.Fatal("Failed to dial: ", err) + logrus.Error("Failed to dial: ", err) return false } defer session.Close() @@ -81,7 +98,7 @@ func testSSH(rClient ssh.Client) bool { func main() { file, err := os.Open("config.json") if err != nil { - fmt.Println("Error opening file:", err) + logrus.Error("Error opening file:", err) return } defer file.Close() @@ -92,15 +109,11 @@ func main() { fmt.Println("Error decoding file:", err) return } - - srcDir := "benchmark" - dstDir := "/tmp/" + randomString(10) + "/" - // 读取本地 config.json 文件, 注入成变量 - username := envConfig.Username password := envConfig.Password port := envConfig.Port + geekbenchLicense := envConfig.Geekbench_License HostExecOut, _ := script.Exec("hostname -I").String() hostIPS := strings.Split(HostExecOut, " ") @@ -112,6 +125,17 @@ func main() { HostKeyCallback: ssh.InsecureIgnoreHostKey(), } + // 打印开始 motto + logrus.Info("Benchmarking...") + // 获取当前时间 + start := time.Now() // 获取当前时间 + + // 初始化 bar cmd + go pBar(len(strings.Split(envConfig.Host, ","))) + + srcDir := "benchmark" + dstDir := "/tmp/" + randomString(10) + "/" + // 创建 iperfServer 协程 go iperfServer() @@ -120,7 +144,7 @@ func main() { cmd := exec.Command("killall", "iperf3") _, err := cmd.Output() if err != nil { - log.Fatal(err) + logrus.Error("Failed to kill iperf3") } }() @@ -129,12 +153,12 @@ func main() { serverURL := fmt.Sprintf("%s:%d", host, port) client, err := ssh.Dial("tcp", serverURL, config) if err != nil { - log.Fatal("Failed to dial: ", err) + logrus.Error("Failed to dial: ", err) } if testSSH(*client) { - log.Printf("Host: %s , SSH 连接成功", host) + logrus.Infof("Host: %s , SSH 连接成功", host) } else { - log.Printf("Host: %s , SSH 连接失败", host) + logrus.Errorf("Host: %s , SSH 连接失败", host) } } @@ -144,7 +168,7 @@ func main() { client, err := ssh.Dial("tcp", serverURL, config) if err != nil { - log.Fatal("Failed to dial: ", err) + logrus.Error("Failed to dial: ", err) } _ = remoteShellExec(*client, []string{"mkdir -p " + dstDir}, false) @@ -161,11 +185,11 @@ func main() { file, err := os.Open(path) defer file.Close() if err != nil { - log.Fatal("Failed to open file: ", err) + logrus.Error("Failed to open file: ", err) } err = scpClient.CopyFile(context.Background(), file, dstDir+path, "0655") if err != nil { - log.Fatal("Failed to copy file: ", err) + logrus.Error("Failed to copy file: ", err) } scpClient.Close() @@ -174,13 +198,16 @@ func main() { }) // 执行 远端 脚本 - log.Printf("Host: %s , 正在执行远端脚本...", host) + logrus.Infof("Host: %s , 正在执行远端脚本中...", host) _ = remoteShellExec(*client, []string{ - "cd " + dstDir + srcDir + " && " + "./run.sh " + hostIPS[0], + "cd " + dstDir + srcDir + " && " + "./run.sh " + hostIPS[0] + " " + "'" + geekbenchLicense + "'", }, true) // 删除远端文件 remoteShellExec(*client, []string{"rm -rf " + dstDir}, false) } + + // 打印执行时间 + logrus.Infof("本次总计执行时间: %s", time.Since(start)) }