简易测网速工具

2024-01-13  本文已影响0人  今天i你好吗

工具原理, 通过循环访问指定的链接来达到测网速的功能

使用方式:
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("请输入测试的线程数:")
        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    线程数: %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
}

编译后的产物:

https://download.csdn.net/download/qq_37873556/88739159
提供如下系统产物
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

上一篇 下一篇

猜你喜欢

热点阅读