-
Notifications
You must be signed in to change notification settings - Fork 2
/
cf-speedtester.go
106 lines (99 loc) · 2.85 KB
/
cf-speedtester.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cf_speedtester
import (
"errors"
"github.com/fireinrain/cf-speedtester/config"
"github.com/fireinrain/cf-speedtester/entity"
"github.com/fireinrain/cf-speedtester/geoip"
"github.com/fireinrain/cf-speedtester/handler"
"github.com/fireinrain/cf-speedtester/task"
"log"
"sync"
"time"
)
type CFSpeedTester struct {
Mux sync.RWMutex
TestOpts entity.TestOptions
SpeedResults []entity.SpeedResult
}
func NewCFSpeedTestClient(testOpts ...config.TestOptionFunc) *CFSpeedTester {
options := config.NewTestOptions(testOpts...)
speedTester := CFSpeedTester{
Mux: sync.RWMutex{},
TestOpts: entity.TestOptions{},
SpeedResults: nil,
}
speedTester.Mux.RLock()
speedTester.TestOpts = options
speedTester.Mux.RUnlock()
return &speedTester
}
// DoSpeedTest
//
// @Description: 执行cloudflare ip 速度测试
// @receiver s
func (s *CFSpeedTester) DoSpeedTest() {
//延迟关闭geoip db
defer geoip.GlobalGeoIPClient.GeoIPDb.Close()
// 开始延迟测速 + 过滤延迟/丢包
pingData := task.NewPing(&s.TestOpts).Run().
FilterWantedISOIP(&s.TestOpts).
FilterDelay(&s.TestOpts).
FilterLossRate(&s.TestOpts).
FilterIPBan(&s.TestOpts)
// 开始下载测速
speedTestData, speedData := task.TestDownloadSpeed(pingData, &s.TestOpts)
//格式化输出结果
log.Println("Test ip download speed info details: ")
speedTestData.PrettyPrint()
log.Println("DownloadSpeed filtered details: ")
speedData.PrettyPrint()
var speedResults []entity.SpeedResult
for _, data := range speedData {
result := entity.SpeedResult{
IPAddress: data.IP.String(),
Sent: data.Sended,
Received: data.Received,
PacketLossRate: data.GetLossRate(),
AvgLatency: data.Delay.Seconds() * 1000,
DownloadSpeed: data.DownloadSpeed,
}
speedResults = append(speedResults, result)
}
s.SpeedResults = speedResults
}
// DoSpeedTestForResult
//
// @Description: 执行ip优选测速
// @receiver s
// @return []SpeedResult
func (s *CFSpeedTester) DoSpeedTestForResult() []entity.SpeedResult {
s.DoSpeedTest()
return s.SpeedResults
}
// ExportToCSV
//
// @Description: 导出为csv文件
// @receiver s
// @param filePath
func (s *CFSpeedTester) ExportToCSV(filePath string) error {
if s.SpeedResults == nil || len(s.SpeedResults) <= 0 {
return errors.New("当前未进行ip测速,暂无结果导出")
}
var cloudflareIPDatas []handler.CloudflareIPData
for _, result := range s.SpeedResults {
addr := handler.IPStrToIPAddr(result.IPAddress)
pingData := &handler.PingData{
IP: addr,
Sended: result.Sent,
Received: result.Received,
Delay: time.Duration(result.AvgLatency / 1000),
}
data := handler.CloudflareIPData{
PingData: pingData,
DownloadSpeed: result.DownloadSpeed,
}
cloudflareIPDatas = append(cloudflareIPDatas, data)
}
handler.ExportCSV(cloudflareIPDatas, filePath)
return nil
}