Skip to content

Commit

Permalink
v0.0.2 - 初始化整个项目
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Jun 29, 2024
1 parent 8c08bc0 commit 03f2b43
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 76 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 '[email protected]'
TAG="v0.0.1-$(date +'%Y%m%d%H%M%S')"
TAG="v0.0.2-$(date +'%Y%m%d%H%M%S')"
git tag $TAG
git push origin $TAG
env:
Expand Down
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
# speedtest
# speedtest

[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Foneclickvirt%2Fspeedtest&count_bg=%232EFFF8&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false)](https://hits.seeyoufarm.com)

就近节点测速模块

## 说明

- [x] 基于[speedtest.net-爬虫](https://github.com/spiritLHLS/speedtest.net-CN-ID)[speedtest.cn-爬虫](https://github.com/spiritLHLS/speedtest.cn-CN-ID)的数据
- [x] 基于[speedtest-go](https://github.com/showwin/speedtest-go)二次开发
- [x] 主体逻辑借鉴了[ecsspeed](https://github.com/spiritLHLS/ecsspeed)

## 使用

下载及安装

```
curl https://raw.githubusercontent.com/oneclickvirt/speedtest/main/spt_install.sh -sSf | bash
```


```
curl https://cdn.spiritlhl.net/https://raw.githubusercontent.com/oneclickvirt/speedtest/main/spt_install.sh -sSf | bash
```

使用

```
spt
```


```
./spt
```

进行测试

无环境依赖,理论上适配所有系统和主流架构,更多架构请查看 https://github.com/oneclickvirt/speedtest/releases/tag/output

```
```

## 卸载

```
rm -rf /root/spt
rm -rf /usr/bin/spt
```

## 在Golang中使用

```
go get github.com/oneclickvirt/speedtest@latest
```
69 changes: 66 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
package main

import "github.com/oneclickvirt/speedtest/sp"
import (
"flag"
"fmt"
"net/http"
"strings"

"github.com/oneclickvirt/speedtest/model"
"github.com/oneclickvirt/speedtest/sp"
)

func main() {
sp.NearbySpeedTest("en")
sp.CustomSpeedTest("https://raw.githubusercontent.com/spiritLHLS/speedtest.net-CN-ID/main/CN_Telecom.csv", 2)
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")
var showVersion, nearByServer 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.StringVar(&language, "l", "zh", "Language parameter (options: en, zh)")
flag.StringVar(&platform, "pf", "net", "Platform parameter (options: net, cn)")
flag.StringVar(&operator, "opt", "", "Operator parameter (options: cmcc, cu, ct, sg, tw, jp, hk, global)")
flag.IntVar(&num, "num", -1, "Number of test servers")
flag.Parse()
if showVersion {
fmt.Println(model.SpeedTestVersion)
return
}
if nearByServer {
sp.NearbySpeedTest(language)
return
}
var url string
if strings.ToLower(platform) == "net" {
if strings.ToLower(operator) == "cmcc" {
url = model.CnCMCC
} else if strings.ToLower(operator) == "cu" {
url = model.CnCU
} else if strings.ToLower(operator) == "ct" {
url = model.CnCT
} else if strings.ToLower(operator) == "hk" {
url = model.CnHK
} else if strings.ToLower(operator) == "tw" {
url = model.CnTW
} else if strings.ToLower(operator) == "jp" {
url = model.CnJP
} else if strings.ToLower(operator) == "sg" {
url = model.CnSG
}
} else if strings.ToLower(platform) == "cn" {
if strings.ToLower(operator) == "cmcc" {
url = model.NetCMCC
} else if strings.ToLower(operator) == "cu" {
url = model.NetCU
} else if strings.ToLower(operator) == "ct" {
url = model.NetCT
} else if strings.ToLower(operator) == "hk" {
url = model.NetHK
} else if strings.ToLower(operator) == "tw" {
url = model.NetTW
} else if strings.ToLower(operator) == "jp" {
url = model.NetJP
} else if strings.ToLower(operator) == "sg" {
url = model.NetSG
}
}
sp.CustomSpeedTest(url, "id", num)
}
3 changes: 2 additions & 1 deletion cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import "testing"

func Test(t *testing.T) {
main()
} // /usr/local/go/bin/go test -timeout 360s -run ^Test$ github.com/oneclickvirt/speedtest/cmd
}
// /usr/local/go/bin/go test -timeout 360s -run ^Test$ github.com/oneclickvirt/speedtest/cmd
6 changes: 4 additions & 2 deletions model/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package model

const SpeedTestVersion = "v0.0.2"

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 All @@ -11,7 +13,7 @@ var (
NetGlobal = "https://raw.githubusercontent.com/spiritLHLS/speedtest.net-CN-ID/main/ls_sg_hk_jp.csv"

CnCMCC = "https://raw.githubusercontent.com/spiritLHLS/speedtest.cn-CN-ID/main/mobile.csv"
CntCT = "https://raw.githubusercontent.com/spiritLHLS/speedtest.cn-CN-ID/main/telecom.csv"
CnCT = "https://raw.githubusercontent.com/spiritLHLS/speedtest.cn-CN-ID/main/telecom.csv"
CnCU = "https://raw.githubusercontent.com/spiritLHLS/speedtest.cn-CN-ID/main/unicom.csv"
CnHK = "https://raw.githubusercontent.com/spiritLHLS/speedtest.cn-CN-ID/main/HK.csv"
CnJP = "https://raw.githubusercontent.com/spiritLHLS/speedtest.cn-CN-ID/main/JP.csv"
Expand All @@ -25,4 +27,4 @@ var (
"http://cdn3.spiritlhl.net/",
"http://cdn2.spiritlhl.net/",
}
)
)
148 changes: 80 additions & 68 deletions sp/sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,6 @@ func checkError(err error) {
}
}

func NearbySpeedTest(language string) {
if language == "zh" {
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
"位置", "上传速度", "下载速度", "延迟", "丢包率")
} else if language == "en" {
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
"Location", "Upload Speed", "Download Speed", "Latency", "PacketLoss")
}
var speedtestClient = speedtest.New()
serverList, _ := speedtestClient.FetchServers()
targets, _ := serverList.FindServer([]int{})
analyzer := speedtest.NewPacketLossAnalyzer(nil)
var LowestLatency time.Duration
var NearbyServer *speedtest.Server
var PacketLoss string
for _, server := range targets {
server.PingTest(nil)
if LowestLatency == 0 && NearbyServer == nil {
LowestLatency = server.Latency
NearbyServer = server
} else if server.Latency < LowestLatency && NearbyServer != nil {
NearbyServer = server
}
server.Context.Reset()
}
if NearbyServer != nil {
NearbyServer.DownloadTest()
NearbyServer.UploadTest()
err := analyzer.Run(NearbyServer.Host, func(packetLoss *transport.PLoss) {
PacketLoss = strings.ReplaceAll(packetLoss.String(), "Packet Loss: ", "")
})
checkError(err)
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
//NearbyServer.Name,
"Speedtest.net",
fmt.Sprintf("%.2f Mbps", NearbyServer.ULSpeed.Mbps()),
fmt.Sprintf("%.2f Mbps", NearbyServer.DLSpeed.Mbps()),
NearbyServer.Latency,
PacketLoss)
NearbyServer.Context.Reset()
}
}

func getData(endpoint string) string {
client := req.C()
client.SetTimeout(10 * time.Second)
Expand All @@ -87,28 +44,28 @@ func getData(endpoint string) string {
return ""
}

// func parseData(data string) speedtest.Servers {
// var targets speedtest.Servers
// reader := csv.NewReader(strings.NewReader(data))
// reader.Comma = ','
// records, err := reader.ReadAll()
// if err != nil {
// log.Fatal(err)
// }
// speedtestClient := speedtest.New()
// for _, record := range records {
// customURL := record[5] + ":" + record[6]
// target, errFetch := speedtestClient.CustomServer(customURL)
// if errFetch != nil {
// continue
// }
// target.Name = record[3]
// targets = append(targets, target)
// }
// return targets
// }
func parseDataFromURL(data string) speedtest.Servers {
var targets speedtest.Servers
reader := csv.NewReader(strings.NewReader(data))
reader.Comma = ','
records, err := reader.ReadAll()
if err != nil {
log.Fatal(err)
}
speedtestClient := speedtest.New()
for _, record := range records {
customURL := record[5] + ":" + record[6]
target, errFetch := speedtestClient.CustomServer(customURL)
if errFetch != nil {
continue
}
target.Name = record[3]
targets = append(targets, target)
}
return targets
}

func parseData(data string) speedtest.Servers {
func parseDataFromID(data string) speedtest.Servers {
var targets speedtest.Servers
reader := csv.NewReader(strings.NewReader(data))
reader.Comma = ','
Expand All @@ -129,15 +86,65 @@ func parseData(data string) speedtest.Servers {
return targets
}

func CustomSpeedTest(url string, num int) {
func NearbySpeedTest(language string) {
if language == "zh" {
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
"位置", "上传速度", "下载速度", "延迟", "丢包率")
} else if language == "en" {
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
"Location", "Upload Speed", "Download Speed", "Latency", "PacketLoss")
}
var speedtestClient = speedtest.New()
serverList, _ := speedtestClient.FetchServers()
targets, _ := serverList.FindServer([]int{})
analyzer := speedtest.NewPacketLossAnalyzer(nil)
var LowestLatency time.Duration
var NearbyServer *speedtest.Server
var PacketLoss string
for _, server := range targets {
server.PingTest(nil)
if LowestLatency == 0 && NearbyServer == nil {
LowestLatency = server.Latency
NearbyServer = server
} else if server.Latency < LowestLatency && NearbyServer != nil {
NearbyServer = server
}
server.Context.Reset()
}
if NearbyServer != nil {
NearbyServer.DownloadTest()
NearbyServer.UploadTest()
err := analyzer.Run(NearbyServer.Host, func(packetLoss *transport.PLoss) {
PacketLoss = strings.ReplaceAll(packetLoss.String(), "Packet Loss: ", "")
})
checkError(err)
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
//NearbyServer.Name,
"Speedtest.net",
fmt.Sprintf("%.2f Mbps", NearbyServer.ULSpeed.Mbps()),
fmt.Sprintf("%.2f Mbps", NearbyServer.DLSpeed.Mbps()),
NearbyServer.Latency,
PacketLoss)
NearbyServer.Context.Reset()
}
}

func CustomSpeedTest(url, byWhat string, num int) {
data := getData(url)
targets := parseData(data)
var targets speedtest.Servers
if byWhat == "id" {
targets = parseDataFromID(data)
} else if byWhat == "url" {
targets = parseDataFromURL(data)
}
var pingList []time.Duration
var err error
serverMap := make(map[time.Duration]*speedtest.Server)
for _, server := range targets {
err = server.PingTest(nil)
checkError(err)
if err != nil {
server.Latency = 1000 * time.Millisecond
}
pingList = append(pingList, server.Latency)
serverMap[server.Latency] = server
server.Context.Reset()
Expand All @@ -147,14 +154,19 @@ func CustomSpeedTest(url string, num int) {
})
analyzer := speedtest.NewPacketLossAnalyzer(nil)
var PacketLoss string
if num == -1 && num >= len(pingList) {
num = len(pingList)
}
for i := 0; i < num && i < len(pingList); i++ {
server := serverMap[pingList[i]]
server.DownloadTest()
server.UploadTest()
err = analyzer.Run(server.Host, func(packetLoss *transport.PLoss) {
PacketLoss = strings.ReplaceAll(packetLoss.String(), "Packet Loss: ", "")
})
checkError(err)
if err != nil {
PacketLoss = "N/A"
}
fmt.Printf("%-12s\t %-11s\t %-11s\t %-11s\t %-12s\n",
server.Name,
fmt.Sprintf("%.2f Mbps", server.ULSpeed.Mbps()),
Expand Down
Loading

0 comments on commit 03f2b43

Please sign in to comment.