forked from google/namebench
-
Notifications
You must be signed in to change notification settings - Fork 37
/
loader.go
81 lines (74 loc) · 2.13 KB
/
loader.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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"math/rand"
"os"
"time"
)
func prepareBenchmarkNameservers(nsStore *nsInfoMap) {
if appConfiguration.nameserver == "" {
// read global nameservers from given file
fmt.Println("trying to load nameservers from nameserver-globals")
readNameserversFromFile(nsStore, "datasrc/nameserver-globals.csv") // TODO: Split read and Load
} else {
loadNameserver(nsStore, appConfiguration.nameserver, "givenByParameter")
}
}
func prepareBenchmarkDomains(dStore *dInfoMap) {
var domains []string
// read domains from given file
fmt.Println("trying to load domains from alexa-top-2000-domains")
allDomains, err := readLoadDomainsFromFile("datasrc/alexa-top-2000-domains.txt")
if err != nil {
fmt.Println("File not found")
return
}
_ = err // TODO: Exception handling in case that the files do not exist
// randomize domains from file to avoid cached results
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allDomains), func(i, j int) { allDomains[i], allDomains[j] = allDomains[j], allDomains[i] })
// take care only for the domain-tests we were looking for
domains = allDomains[0:appConfiguration.numberOfDomains]
dStoreAddFQDN(dStore, domains)
}
// load nameservers
func loadNameserver(nsStore *nsInfoMap, ip string, name string) {
nsStoreAddNS(nsStore, ip, name, "LOCAL")
}
// load nameservers
func readNameserversFromFile(nsStore *nsInfoMap, filename string) {
csvFile, _ := os.Open(filename)
nameserverReader := csv.NewReader(bufio.NewReader(csvFile))
for {
line, err := nameserverReader.Read()
if err == io.EOF {
break
}
// fmt.Println(line)
nsStoreAddNS(nsStore, line[0], line[1], line[2])
_ = err
}
}
// readDomainsFromFile reads a whole file into memory
// and returns a slice of its lines.
func readLoadDomainsFromFile(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
panic(err)
}
}(file)
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}