Skip to content

Commit

Permalink
v0.0.5 - 增加日志记录参数
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Jun 30, 2024
1 parent 060a1c0 commit 0b64045
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com'
TAG="v0.0.4-$(date +'%Y%m%d%H%M%S')"
TAG="v0.0.5-$(date +'%Y%m%d%H%M%S')"
git tag $TAG
git push origin $TAG
env:
Expand Down
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"

. "github.com/oneclickvirt/defaultset"
"github.com/oneclickvirt/speedtest/model"
"github.com/oneclickvirt/speedtest/sp"
)
Expand All @@ -14,13 +15,14 @@ func main() {
go func() {
http.Get("https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Foneclickvirt%2Fspeedtest&count_bg=%2323E01C&title_bg=%23555555&icon=sonarcloud.svg&icon_color=%23E7E7E7&title=hits&edge_flat=false")
}()
fmt.Println("项目地址:", "https://github.com/oneclickvirt/speedtest")
fmt.Println("项目地址:", Blue("https://github.com/oneclickvirt/speedtest"))
var showVersion, nearByServer, showHead bool
var language, operator, platform string
var num int
flag.BoolVar(&showVersion, "v", false, "Show version information")
flag.BoolVar(&nearByServer, "nearby", false, "Test only nearby servers")
flag.BoolVar(&showHead, "s", true, "Show head")
flag.BoolVar(&model.EnableLoger, "e", false, "Enable logging")
flag.StringVar(&language, "l", "zh", "Language parameter (options: en, zh)")
flag.StringVar(&platform, "pf", "net", "Platform parameter (options: net, cn)")
flag.StringVar(&operator, "opt", "global", "Operator parameter (options: cmcc, cu, ct, sg, tw, jp, hk, global)")
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/oneclickvirt/defaultset v0.0.2-20240624082446 // indirect
github.com/onsi/ginkgo/v2 v2.16.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.41.0 // indirect
github.com/refraction-networking/utls v1.6.3 // indirect
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.16.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/imroc/req/v3 v3.43.7 h1:dOcNb9n0X83N5/5/AOkiU+cLhzx8QFXjv5MhikazzQA=
github.com/imroc/req/v3 v3.43.7/go.mod h1:SQIz5iYop16MJxbo8ib+4LnostGCok8NQf8ToyQc2xA=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/oneclickvirt/defaultset v0.0.2-20240624082446 h1:5Pg3mK/u/vQvSz7anu0nxzrNdELi/AcDAU1mMsmPzyc=
github.com/oneclickvirt/defaultset v0.0.2-20240624082446/go.mod h1:e9Jt4tf2sbemCtc84/XgKcHy9EZ2jkc5x2sW1NiJS+E=
github.com/onsi/ginkgo/v2 v2.16.0 h1:7q1w9frJDzninhXxjZd+Y/x54XNjG/UlRLIYPZafsPM=
github.com/onsi/ginkgo/v2 v2.16.0/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
Expand All @@ -43,6 +45,10 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
Expand Down
3 changes: 2 additions & 1 deletion model/model.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package model

const SpeedTestVersion = "v0.0.4"
const SpeedTestVersion = "v0.0.5"

var EnableLoger = false
var (
NetCMCC = "https://raw.githubusercontent.com/spiritLHLS/speedtest.net-CN-ID/main/CN_Mobile.csv"
NetCT = "https://raw.githubusercontent.com/spiritLHLS/speedtest.net-CN-ID/main/CN_Telecom.csv"
Expand Down
92 changes: 72 additions & 20 deletions sp/sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"encoding/csv"
"fmt"
"io"
"log"
"sort"
"strings"
"time"
"unicode/utf8"

"github.com/imroc/req/v3"
. "github.com/oneclickvirt/defaultset"
"github.com/oneclickvirt/speedtest/model"
"github.com/showwin/speedtest-go/speedtest"
"github.com/showwin/speedtest-go/speedtest/transport"
Expand All @@ -23,19 +23,17 @@ var speedtestClient = speedtest.New(speedtest.WithUserConfig(
MaxConnections: 8,
}))

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}

func getData(endpoint string) string {
client := req.C()
client.SetTimeout(10 * time.Second)
client.R().
SetRetryCount(2).
SetRetryBackoffInterval(1*time.Second, 5*time.Second).
SetRetryFixedInterval(2 * time.Second)
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
for _, baseUrl := range model.CdnList {
url := baseUrl + endpoint
resp, err := client.R().Get(url)
Expand All @@ -46,11 +44,18 @@ func getData(endpoint string) string {
return string(b)
}
}
if model.EnableLoger {
Logger.Info(err.Error())
}
}
return ""
}

func parseDataFromURL(data, url string) speedtest.Servers {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
var targets speedtest.Servers
reader := csv.NewReader(strings.NewReader(data))
reader.Comma = ','
Expand All @@ -63,6 +68,9 @@ func parseDataFromURL(data, url string) speedtest.Servers {
customURL := record[5]
target, errFetch := speedtestClient.CustomServer(customURL)
if errFetch != nil {
if model.EnableLoger {
Logger.Info(err.Error())
}
continue
}
target.Name = record[10] + record[7] + record[8]
Expand All @@ -73,6 +81,10 @@ func parseDataFromURL(data, url string) speedtest.Servers {
}

func parseDataFromID(data, url string) speedtest.Servers {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
var targets speedtest.Servers
reader := csv.NewReader(strings.NewReader(data))
reader.Comma = ','
Expand All @@ -85,6 +97,9 @@ func parseDataFromID(data, url string) speedtest.Servers {
id := record[0]
serverPtr, errFetch := speedtestClient.FetchServerByID(id)
if errFetch != nil {
if model.EnableLoger {
Logger.Info(err.Error())
}
continue
}
if strings.Contains(url, "Mobile") {
Expand Down Expand Up @@ -144,6 +159,10 @@ func ShowHead(language string) {
}

func NearbySpeedTest() {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
serverList, _ := speedtestClient.FetchServers()
targets, _ := serverList.FindServer([]int{})
analyzer := speedtest.NewPacketLossAnalyzer(nil)
Expand All @@ -166,18 +185,25 @@ func NearbySpeedTest() {
err := analyzer.Run(NearbyServer.Host, func(packetLoss *transport.PLoss) {
PacketLoss = strings.ReplaceAll(packetLoss.String(), "Packet Loss: ", "")
})
checkError(err)
fmt.Print(formatString("Speedtest.net", 16))
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", NearbyServer.ULSpeed.Mbps())+" Mbps"), 16))
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", NearbyServer.DLSpeed.Mbps())+" Mbps"), 16))
fmt.Print(formatString(fmt.Sprintf("%s", NearbyServer.Latency), 16))
fmt.Print(formatString(PacketLoss, 16))
fmt.Println()
NearbyServer.Context.Reset()
if err == nil {
fmt.Print(formatString("Speedtest.net", 16))
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", NearbyServer.ULSpeed.Mbps())+" Mbps"), 16))
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", NearbyServer.DLSpeed.Mbps())+" Mbps"), 16))
fmt.Print(formatString(fmt.Sprintf("%s", NearbyServer.Latency), 16))
fmt.Print(formatString(PacketLoss, 16))
fmt.Println()
NearbyServer.Context.Reset()
} else if model.EnableLoger {
Logger.Info(err.Error())
}
}
}

func CustomSpeedTest(url, byWhat string, num int) {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
data := getData(url)
var targets speedtest.Servers
if byWhat == "id" {
Expand All @@ -186,12 +212,15 @@ func CustomSpeedTest(url, byWhat string, num int) {
targets = parseDataFromURL(data, url)
}
var pingList []time.Duration
var err error
var err, err1, err2, err3 error
serverMap := make(map[time.Duration]*speedtest.Server)
for _, server := range targets {
err = server.PingTest(nil)
if err != nil {
server.Latency = 1000 * time.Millisecond
if model.EnableLoger {
Logger.Info(err.Error())
}
}
pingList = append(pingList, server.Latency)
serverMap[server.Latency] = server
Expand All @@ -205,19 +234,42 @@ func CustomSpeedTest(url, byWhat string, num int) {
num = len(pingList)
} else if len(pingList) == 0 {
fmt.Println("No match servers")
if model.EnableLoger {
Logger.Info("No match servers")
}
return
}
for i := 0; i < len(pingList); i++ {
server := serverMap[pingList[i]]
if i < num {
server.DownloadTest()
server.UploadTest()
err = analyzer.Run(server.Host, func(packetLoss *transport.PLoss) {
err1 = server.DownloadTest()
err2 = server.UploadTest()
err3 = analyzer.Run(server.Host, func(packetLoss *transport.PLoss) {
PacketLoss = strings.ReplaceAll(packetLoss.String(), "Packet Loss: ", "")
})
if err != nil {
if err3 != nil {
if model.EnableLoger {
Logger.Info(server.ID)
Logger.Info(err3.Error())
}
PacketLoss = "N/A"
}
if err1 != nil {
if model.EnableLoger {
Logger.Info(server.ID)
Logger.Info(err1.Error())
}
server.Context.Reset()
continue
}
if err2 != nil {
if model.EnableLoger {
Logger.Info(server.ID)
Logger.Info(err2.Error())
}
server.Context.Reset()
continue
}
fmt.Print(formatString(server.Name, 16))
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", server.ULSpeed.Mbps())+" Mbps"), 16))
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", server.DLSpeed.Mbps())+" Mbps"), 16))
Expand Down

0 comments on commit 0b64045

Please sign in to comment.