Skip to content

Commit

Permalink
improve running speed
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Mar 18, 2024
1 parent d4c6787 commit 3309924
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
11 changes: 0 additions & 11 deletions constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package main

import "github.com/CursedHardware/go-ipv6-test/ipv6test"

var knownHosts = []string{
"jp2.test-ipv6.com",
"ams2.test-ipv6.com",
Expand Down Expand Up @@ -34,12 +32,3 @@ var knownHosts = []string{
"testipv6.cn",
"testipv6.de",
}

var tasks = []ipv6test.Task{
ipv6test.RecordIPv4,
ipv6test.RecordIPv6,
ipv6test.RecordDualStack,
ipv6test.RecordDualStackMTU,
ipv6test.RecordIPv6MTU,
ipv6test.RecordIPv6NS,
}
10 changes: 4 additions & 6 deletions ipv6test/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ func (t Task) Name() string {

func (t Task) Prefix() string {
switch t {
case RecordIPv4:
case RecordIPv4, RecordASN4:
return "ipv4."
case RecordIPv6:
case RecordIPv6, RecordASN6:
return "ipv6."
case RecordDualStack, RecordDualStackMTU:
return "ds."
case RecordIPv6MTU:
return "mtu1280."
case RecordIPv6NS:
return "ds.v6ns."
default:
return ""
}
return ""
}

func (t Task) Match(proto string) bool {
Expand All @@ -58,7 +57,6 @@ func (t Task) Match(proto string) bool {
return t >= RecordIPv4 && t <= RecordASN4
case "ipv6":
return t >= RecordIPv6 && t <= RecordASN6
default:
return false
}
return false
}
68 changes: 48 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
_ "embed"
"flag"
"fmt"
"github.com/CursedHardware/go-ipv6-test/ipv6test"
. "github.com/CursedHardware/go-ipv6-test/ipv6test"
"github.com/fatih/color"
"net/http"
"strings"
Expand All @@ -23,43 +23,71 @@ func init() {
}

func main() {
tester := &ipv6test.Tester{
tester := &Tester{
Client: http.DefaultClient,
MTU: 1600,
}
switch {
case listAll:
fmt.Println(strings.Join(knownHosts, "\n"))
case testAll:
var wg sync.WaitGroup
var mux sync.Mutex
for _, knownHost := range knownHosts {
wg.Add(1)
go func(host string) {
report := tester.Run(ipv6test.RecordIPv6, host)
mux.Lock()
fmt.Printf("Test for %q\n", host)
emitReport(report)
mux.Unlock()
batchTasks(tester, []Task{RecordIPv6}, knownHosts)
default:
fullTask(tester, host)
}
}

func invoke(tester *Tester, requests map[string][]Task) <-chan *Report {
var wg sync.WaitGroup
reports := make(chan *Report)
for taskHost, tasks := range requests {
wg.Add(len(tasks))
for _, task := range tasks {
go func(task Task, taskHost string) {
if task == RecordASN4 || task == RecordASN6 {
taskHost = "lookup.test-ipv6.com"
}
reports <- tester.Run(task, taskHost)
wg.Done()
}(knownHost)
}(task, taskHost)
}
}
go func() {
wg.Wait()
default:
for _, taskType := range tasks {
emitReport(tester.Run(taskType, host))
}
emitReport(tester.Run(ipv6test.RecordASN4, "ipv4.lookup.test-ipv6.com"))
emitReport(tester.Run(ipv6test.RecordASN6, "ipv6.lookup.test-ipv6.com"))
close(reports)
}()
return reports
}

func fullTask(tester *Tester, host string) {
requests := make(map[string][]Task)
requests[host] = []Task{
RecordIPv4, RecordIPv6, RecordDualStack, RecordDualStackMTU,
RecordIPv6MTU, RecordIPv6NS, RecordASN4, RecordASN6,
}
for report := range invoke(tester, requests) {
emitReport(report)
}
}

func batchTasks(tester *Tester, tasks []Task, hosts []string) {
requests := make(map[string][]Task)
for _, testHost := range hosts {
requests[testHost] = tasks
}
for report := range invoke(tester, requests) {
fmt.Printf("Test for %q\n", report.Host)
emitReport(report)
}
}

func emitReport(r *ipv6test.Report) {
func emitReport(r *Report) {
var ok = color.GreenString("ok")
var bad = color.BlueString("bad")
fmt.Println(r.Task.Name())
if r.Failed {
fmt.Printf("%s (%s)\n", bad, r.Elapsed)
fmt.Println()
return
}
status := bad
Expand Down

0 comments on commit 3309924

Please sign in to comment.