Skip to content

Commit 3309924

Browse files
committed
improve running speed
1 parent d4c6787 commit 3309924

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

constants.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package main
22

3-
import "github.com/CursedHardware/go-ipv6-test/ipv6test"
4-
53
var knownHosts = []string{
64
"jp2.test-ipv6.com",
75
"ams2.test-ipv6.com",
@@ -34,12 +32,3 @@ var knownHosts = []string{
3432
"testipv6.cn",
3533
"testipv6.de",
3634
}
37-
38-
var tasks = []ipv6test.Task{
39-
ipv6test.RecordIPv4,
40-
ipv6test.RecordIPv6,
41-
ipv6test.RecordDualStack,
42-
ipv6test.RecordDualStackMTU,
43-
ipv6test.RecordIPv6MTU,
44-
ipv6test.RecordIPv6NS,
45-
}

ipv6test/task.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@ func (t Task) Name() string {
3737

3838
func (t Task) Prefix() string {
3939
switch t {
40-
case RecordIPv4:
40+
case RecordIPv4, RecordASN4:
4141
return "ipv4."
42-
case RecordIPv6:
42+
case RecordIPv6, RecordASN6:
4343
return "ipv6."
4444
case RecordDualStack, RecordDualStackMTU:
4545
return "ds."
4646
case RecordIPv6MTU:
4747
return "mtu1280."
4848
case RecordIPv6NS:
4949
return "ds.v6ns."
50-
default:
51-
return ""
5250
}
51+
return ""
5352
}
5453

5554
func (t Task) Match(proto string) bool {
@@ -58,7 +57,6 @@ func (t Task) Match(proto string) bool {
5857
return t >= RecordIPv4 && t <= RecordASN4
5958
case "ipv6":
6059
return t >= RecordIPv6 && t <= RecordASN6
61-
default:
62-
return false
6360
}
61+
return false
6462
}

main.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
_ "embed"
55
"flag"
66
"fmt"
7-
"github.com/CursedHardware/go-ipv6-test/ipv6test"
7+
. "github.com/CursedHardware/go-ipv6-test/ipv6test"
88
"github.com/fatih/color"
99
"net/http"
1010
"strings"
@@ -23,43 +23,71 @@ func init() {
2323
}
2424

2525
func main() {
26-
tester := &ipv6test.Tester{
26+
tester := &Tester{
2727
Client: http.DefaultClient,
2828
MTU: 1600,
2929
}
3030
switch {
3131
case listAll:
3232
fmt.Println(strings.Join(knownHosts, "\n"))
3333
case testAll:
34-
var wg sync.WaitGroup
35-
var mux sync.Mutex
36-
for _, knownHost := range knownHosts {
37-
wg.Add(1)
38-
go func(host string) {
39-
report := tester.Run(ipv6test.RecordIPv6, host)
40-
mux.Lock()
41-
fmt.Printf("Test for %q\n", host)
42-
emitReport(report)
43-
mux.Unlock()
34+
batchTasks(tester, []Task{RecordIPv6}, knownHosts)
35+
default:
36+
fullTask(tester, host)
37+
}
38+
}
39+
40+
func invoke(tester *Tester, requests map[string][]Task) <-chan *Report {
41+
var wg sync.WaitGroup
42+
reports := make(chan *Report)
43+
for taskHost, tasks := range requests {
44+
wg.Add(len(tasks))
45+
for _, task := range tasks {
46+
go func(task Task, taskHost string) {
47+
if task == RecordASN4 || task == RecordASN6 {
48+
taskHost = "lookup.test-ipv6.com"
49+
}
50+
reports <- tester.Run(task, taskHost)
4451
wg.Done()
45-
}(knownHost)
52+
}(task, taskHost)
4653
}
54+
}
55+
go func() {
4756
wg.Wait()
48-
default:
49-
for _, taskType := range tasks {
50-
emitReport(tester.Run(taskType, host))
51-
}
52-
emitReport(tester.Run(ipv6test.RecordASN4, "ipv4.lookup.test-ipv6.com"))
53-
emitReport(tester.Run(ipv6test.RecordASN6, "ipv6.lookup.test-ipv6.com"))
57+
close(reports)
58+
}()
59+
return reports
60+
}
61+
62+
func fullTask(tester *Tester, host string) {
63+
requests := make(map[string][]Task)
64+
requests[host] = []Task{
65+
RecordIPv4, RecordIPv6, RecordDualStack, RecordDualStackMTU,
66+
RecordIPv6MTU, RecordIPv6NS, RecordASN4, RecordASN6,
67+
}
68+
for report := range invoke(tester, requests) {
69+
emitReport(report)
70+
}
71+
}
72+
73+
func batchTasks(tester *Tester, tasks []Task, hosts []string) {
74+
requests := make(map[string][]Task)
75+
for _, testHost := range hosts {
76+
requests[testHost] = tasks
77+
}
78+
for report := range invoke(tester, requests) {
79+
fmt.Printf("Test for %q\n", report.Host)
80+
emitReport(report)
5481
}
5582
}
5683

57-
func emitReport(r *ipv6test.Report) {
84+
func emitReport(r *Report) {
5885
var ok = color.GreenString("ok")
5986
var bad = color.BlueString("bad")
6087
fmt.Println(r.Task.Name())
6188
if r.Failed {
6289
fmt.Printf("%s (%s)\n", bad, r.Elapsed)
90+
fmt.Println()
6391
return
6492
}
6593
status := bad

0 commit comments

Comments
 (0)