-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (172 loc) · 3.88 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
)
var rxCount uint64
var txCount uint64
const (
DEFAULT_WORKER_NUM = 64
DEFAULT_TARGET_URL = "https://db.laomoe.com/data-waster-dummy"
)
var (
workerNum = flag.Int("j", DEFAULT_WORKER_NUM, "worker num")
url = flag.String("u", DEFAULT_TARGET_URL, "target url")
verbose = flag.Bool("v", false, "print more info")
)
func main() {
// unit is MB
flag.Usage = func() {
println("usage: xhll [...opts] [n MB]")
flag.PrintDefaults()
}
flag.Parse()
stopCount := 10
// fmt.Println(flag.Args())
if flag.NArg() > 0 {
n, err := strconv.Atoi(flag.Arg(0))
if err != nil {
flag.Usage()
return
}
stopCount = n
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
c := uint64(stopCount) * 1024 * 1024
startTime := time.Now()
txSpeedCalcer := newSpeedCalcer(startTime, 0)
rxSpeedCalcer := newSpeedCalcer(startTime, 0)
for {
rx := atomic.LoadUint64(&rxCount)
tx := atomic.LoadUint64(&txCount)
rxtx := rx + tx
rxSpeed := B2M(rxSpeedCalcer.Calc(int64(rx)))
txSpeed := B2M(txSpeedCalcer.Calc(int64(tx)))
eplasedTime := time.Since(startTime)
cleanLine()
fmt.Printf("elpased: %s rxtx: %.2fm ↑: %.2fm/s ↓: %.2fm/s", eplasedTime, B2M(float64(rxtx)), txSpeed, rxSpeed)
if rxtx > c {
// calc avg speed
txSpeedCalcer = newSpeedCalcer(startTime, 0)
rxSpeedCalcer = newSpeedCalcer(startTime, 0)
rxSpeed = B2M(rxSpeedCalcer.Calc(int64(rx)))
txSpeed = B2M(txSpeedCalcer.Calc(int64(tx)))
cleanLine()
fmt.Printf("elpased: %s rxtx: %.2fm ↑: %.2fm/s ↓: %.2fm/s", eplasedTime, B2M(float64(rxtx)), txSpeed, rxSpeed)
println("")
println("done!")
cancel()
return
}
time.Sleep(time.Millisecond * 500)
}
}()
ch := make(chan func(), *workerNum)
for i := 0; i < *workerNum; i++ {
go func() {
for {
select {
case <-ctx.Done():
return
case task := <-ch:
task()
}
}
}()
}
go func() {
for {
select {
case <-ctx.Done():
return
default:
ch <- func() {
err := download(ctx, *url)
if err != nil {
if *verbose {
println(err.Error())
}
time.Sleep(time.Second)
}
}
}
}
}()
<-ctx.Done()
}
var bufPool = sync.Pool{
New: func() any {
buf := make([]byte, 1024)
return &buf
},
}
func download(ctx context.Context, url string) error {
tr := http.DefaultTransport.(*http.Transport).Clone()
client := http.Client{Transport: tr}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Close = true
req.Header.Set("User-Agent", DEFAULT_UA)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
type writer interface {
Write(w io.Writer) error
}
buf := bufPool.Get().(*[]byte)
defer bufPool.Put(buf)
count := func(addr *uint64, w writer) {
pipr, pipw := io.Pipe()
go func() {
w.Write(pipw)
pipw.Close()
}()
for {
n, err := pipr.Read(*buf)
if err != nil {
pipr.Close()
return
}
atomic.AddUint64(addr, uint64(n))
}
}
count(&txCount, req)
count(&rxCount, resp)
return nil
}
func cleanLine() {
fmt.Printf("\033[2K\r")
}
type SpeedCalcer struct {
preTime time.Time
preValue int64
}
func newSpeedCalcer(t time.Time, v int64) *SpeedCalcer {
return &SpeedCalcer{preTime: t, preValue: v}
}
// no support concurrent
func (c *SpeedCalcer) Calc(v int64) float64 {
delta := v - c.preValue
since := time.Since(c.preTime)
r := float64(delta) / since.Seconds()
c.preTime = time.Now()
c.preValue = v
return r
}
// byte to mb
func B2M(v float64) float64 {
return v / 1024 / 1024
}
const DEFAULT_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"