From 7893f1258b13c51e16666c5127548d8068f350e0 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 26 Jul 2023 11:22:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A1=AC=E7=9B=98=E9=80=9F?= =?UTF-8?q?=E5=BA=A6=E6=A3=80=E6=B5=8B=E5=91=BD=E4=BB=A4=EF=BC=9Aedge-node?= =?UTF-8?q?=20disk=20speed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/edge-node/main.go | 24 ++++++++++ internal/nodes/node.go | 27 +++++------ internal/utils/fs/disk.go | 69 +++++++++++++++++++++++++++++ internal/utils/fs/disk_test_test.go | 16 +++++++ 4 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 internal/utils/fs/disk.go create mode 100644 internal/utils/fs/disk_test_test.go diff --git a/cmd/edge-node/main.go b/cmd/edge-node/main.go index 2143457..0a1e94c 100644 --- a/cmd/edge-node/main.go +++ b/cmd/edge-node/main.go @@ -7,6 +7,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/apps" teaconst "github.com/TeaOSLab/EdgeNode/internal/const" "github.com/TeaOSLab/EdgeNode/internal/nodes" + fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs" _ "github.com/iwind/TeaGo/bootstrap" "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/maps" @@ -365,6 +366,29 @@ func main() { } fmt.Println(string(statsJSON)) }) + app.On("disk", func() { + var args = os.Args[2:] + if len(args) > 0 { + switch args[0] { + case "speed": + speedMB, isFast, err := fsutils.CheckDiskIsFast() + if err != nil { + fmt.Println("[ERROR]" + err.Error()) + } else { + fmt.Printf("Speed: %.2fMB/s\n", speedMB) + if isFast { + fmt.Println("IsFast: true") + } else { + fmt.Println("IsFast: false") + } + } + default: + fmt.Println("Usage: edge-node disk [speed]") + } + } else { + fmt.Println("Usage: edge-node disk [speed]") + } + }) app.Run(func() { var node = nodes.NewNode() node.Start() diff --git a/internal/nodes/node.go b/internal/nodes/node.go index f7b3605..75e0dc0 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "github.com/TeaOSLab/EdgeCommon/pkg/configutils" iplib "github.com/TeaOSLab/EdgeCommon/pkg/iplibrary" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" @@ -26,6 +27,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils" _ "github.com/TeaOSLab/EdgeNode/internal/utils/agents" // 引入Agent管理器 _ "github.com/TeaOSLab/EdgeNode/internal/utils/clock" // 触发时钟更新 + fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs" "github.com/TeaOSLab/EdgeNode/internal/utils/jsonutils" "github.com/TeaOSLab/EdgeNode/internal/waf" "github.com/andybalholm/brotli" @@ -141,7 +143,7 @@ func (this *Node) Start() { // 调整系统参数 this.checkSystem() - // 检查硬盘类型 + // 检查硬盘 this.checkDisk() // 启动事件 @@ -1109,23 +1111,16 @@ func (this *Node) checkSystem() { // 检查硬盘 func (this *Node) checkDisk() { - if runtime.GOOS != "linux" { + speedMB, isFast, err := fsutils.CheckDiskIsFast() + if err != nil { + remotelogs.Error("NODE", "check disk speed failed: "+err.Error()) return } - for n := 'a'; n <= 'z'; n++ { - for _, path := range []string{ - "/sys/block/vd" + string(n) + "/queue/rotational", - "/sys/block/sd" + string(n) + "/queue/rotational", - } { - data, err := os.ReadFile(path) - if err != nil { - continue - } - if string(data) == "0" { - teaconst.DiskIsFast = true - } - return - } + teaconst.DiskIsFast = isFast + if isFast { + remotelogs.Println("NODE", "disk is fast, writing test speed: "+fmt.Sprintf("%.2fMB/s", speedMB)) + } else { + remotelogs.Println("NODE", "disk is slow, writing test speed: "+fmt.Sprintf("%.2fMB/s", speedMB)) } } diff --git a/internal/utils/fs/disk.go b/internal/utils/fs/disk.go new file mode 100644 index 0000000..2ac98b6 --- /dev/null +++ b/internal/utils/fs/disk.go @@ -0,0 +1,69 @@ +// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package fsutils + +import ( + "bytes" + "os" + "time" +) + +// CheckDiskWritingSpeed test disk writing speed +func CheckDiskWritingSpeed() (speedMB float64, err error) { + var tempDir = os.TempDir() + if len(tempDir) == 0 { + tempDir = "/tmp" + } + + const filename = "edge-disk-writing-test.data" + var path = tempDir + "/" + filename + _ = os.Remove(path) // always try to delete the file + + fp, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + return 0, err + } + + var isClosed bool + defer func() { + if !isClosed { + _ = fp.Close() + } + + _ = os.Remove(path) + }() + + var data = bytes.Repeat([]byte{'A'}, 16<<20) + var before = time.Now() + _, err = fp.Write(data) + if err != nil { + return 0, err + } + + err = fp.Sync() + if err != nil { + return 0, err + } + + err = fp.Close() + if err != nil { + return 0, err + } + + var costSeconds = time.Since(before).Seconds() + speedMB = float64(len(data)) / (1 << 20) / costSeconds + + isClosed = true + + return +} + +// CheckDiskIsFast check disk is 'fast' disk to write +func CheckDiskIsFast() (speedMB float64, isFast bool, err error) { + speedMB, err = CheckDiskWritingSpeed() + if err != nil { + return + } + isFast = speedMB > 120 + return +} diff --git a/internal/utils/fs/disk_test_test.go b/internal/utils/fs/disk_test_test.go new file mode 100644 index 0000000..c20fd7a --- /dev/null +++ b/internal/utils/fs/disk_test_test.go @@ -0,0 +1,16 @@ +// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package fsutils_test + +import ( + fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs" + "testing" +) + +func TestCheckDiskWritingSpeed(t *testing.T) { + t.Log(fsutils.CheckDiskWritingSpeed()) +} + +func TestCheckDiskIsFast(t *testing.T) { + t.Log(fsutils.CheckDiskIsFast()) +} \ No newline at end of file