-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed.go
54 lines (43 loc) · 1.07 KB
/
speed.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
package main
import (
"fmt"
"github.com/miekg/dns"
"github.com/urfave/cli/v2"
"time"
)
func show(msg *dns.Msg, t time.Duration) {
for _, v := range msg.Answer {
record, ok := v.(*dns.A)
if ok {
fmt.Printf("domain:%s, server:%s, delay:%dms\n", msg.Question[0].Name, record.A, t.Milliseconds())
break
}
}
}
func Speed(ctx *cli.Context) error {
domain := ctx.String("domain")
count := ctx.Int("count")
server := ctx.String("server")
every := ctx.Int("time")
var sucTime, sunCount = 0, 0
for i := 0; i < count; i++ {
r, t, err := calcTime(domain, server, dns.TypeA)
time.Sleep(time.Duration(every) * time.Millisecond)
if err != nil {
fmt.Printf("Timeout:domain:%s\n", domain)
continue
}
if len(r.Answer) <= 0 {
continue
}
sucTime += int(t.Milliseconds())
sunCount += 1
show(r, t)
}
if sunCount > 0 {
fmt.Printf("dnsTools Total:%d, Succ:%d, Error:%d, averageTime:%dms\n", count, sunCount, count-sunCount, sucTime/sunCount)
} else {
fmt.Printf("dnsTools Total:%d, Succ:%d, Error:%d\n", count, sunCount, count-sunCount)
}
return nil
}