簡易測網(wǎng)速工具

工具原理, 通過循環(huán)訪問指定的鏈接來達(dá)到測網(wǎng)速的功能

使用方式:
gif4.gif
源碼:
package main

import (
    "bufio"
    "fmt"
    "io"
    "log"
    "net/http"
    "os"
    "strconv"
    "sync"
    "time"
)

var (
    threadCount int
    url         string
    mu          sync.Mutex
    count       int
    total       int64
    oldTime     time.Time
)

func main() {
    log.SetFlags(log.LstdFlags | log.Lshortfile)
    reader := bufio.NewReader(os.Stdin)
    for {
        println("請輸入測試的線程數(shù):")
        lineData, _, err := reader.ReadLine()
        if len(lineData) == 0 {
            continue
        }
        if err != nil {
            log.Println(err)
            continue
        }
        value, err := strconv.ParseInt(string(lineData), 10, 64)
        if err != nil {
            log.Println(err)
            continue
        }
        threadCount = int(value)
        break
    }
    for {
        println("請輸入用于測試的地址:")
        lineData, _, err := reader.ReadLine()
        if len(lineData) == 0 {
            continue
        }
        if err != nil {
            log.Println(err)
            continue
        }
        url = string(lineData)
        break
    }

    count = 0
    oldTime = time.Now()

    for i := 0; i < threadCount; i++ {
        go startDownload(url, i)
    }

    for {
        time.Sleep(time.Second * 1)
        printSpeed()
    }
}

func startDownload(url string, index int) bool {
    print(index)
    tomeOut := true
    count := 0
    for {
        if tomeOut {
            count++
            go func(oldCount int) {
                for {
                    if oldCount != count {
                        break
                    }
                    resp, err := http.Get(url)
                    tomeOut = false
                    if err != nil {
                        log.Println(err)
                        continue
                    }
                    defer resp.Body.Close()
                    data := make([]byte, 4096)
                    for {
                        if oldCount != count {
                            break
                        }
                        n, err := resp.Body.Read(data)
                        if n > 0 {
                            tomeOut = false
                            addSize(n)
                        }
                        if err == io.EOF {
                            break
                        }
                        if err != nil {
                            log.Println(err)
                            break
                        }
                    }
                }
            }(count)
        } else {
            tomeOut = true
        }
        time.Sleep(time.Second * 5)
    }
}

func addSize(length int) {
    mu.Lock()
    defer mu.Unlock()
    count += length
    total += int64(length)
}

func printSpeed() {
    mu.Lock()
    defer mu.Unlock()
    nowTime := time.Now()
    dTime := float64(nowTime.Sub(oldTime).Milliseconds() * 1024 * 1024)
    fmt.Printf("速度: %.2fMB/S   帶寬: %.2fMbps   已讀取大小: %.2fMB    線程數(shù): %d    時間: %s\n",
        float64(count*1000)/dTime, float64(count*8*1000)/dTime, float64(total)/(1024*1024),
        threadCount, nowTime.Format("2006-01-02 15:04:05"))
    count = 0
    oldTime = nowTime
}

編譯后的產(chǎn)物:

https://download.csdn.net/download/qq_37873556/88739159
提供如下系統(tǒng)產(chǎn)物
go env -w GOOS=linux GOARCH=amd64
go install
go env -w GOOS=darwin GOARCH=amd64
go install
go env -w GOOS=darwin GOARCH=arm64
go install
go env -w GOOS=linux GOARCH=arm GOARM=5
go install
go env -w GOOS=windows GOARCH=amd64
go install

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容