Browse Source

Initial Commit

tags/v1
Tim Schuster 1 year ago
commit
e92972ed85
Signed by: Tim Schuster <mail@timschuster.info> GPG Key ID: F9E27097EFB77F61
6 changed files with 222 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 7
    0
      LICENSE
  3. 27
    0
      README.md
  4. 114
    0
      bench.go
  5. 53
    0
      main.go
  6. 20
    0
      rnd.go

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+/dnsbench

+ 7
- 0
LICENSE View File

@@ -0,0 +1,7 @@
1
+Copyright 2018 Tim Schuster
2
+
3
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 27
- 0
README.md View File

@@ -0,0 +1,27 @@
1
+# dnsbench
2
+
3
+Simple DNS Benchmarking Tool
4
+
5
+## Usage
6
+
7
+```
8
+$ ./dnsbench --help
9
+usage: dnsbench [<flags>] [<dns servers>...]
10
+
11
+Flags:
12
+      --help              Show context-sensitive help (also try --help-long and --help-man).
13
+  -i, --i=1000            Number of Requests per DNS server
14
+  -e, --e="google.com"    DNS Wildcard Endpoint to benchmark against
15
+      --wait-request=100  Time to wait between requests in milliseconds
16
+      --anti-cache        Prepend randomized subdomains to query to prevent some caching. THIS
17
+                          REQUIRES A WILDCARD DNS ENTRY!
18
+
19
+Args:
20
+  [<dns servers>]  DNS Servers to ping
21
+```
22
+
23
+It is recommended to change the benchmark endpoint.
24
+
25
+## License
26
+
27
+This tool is licensed under MIT License. See LICENSE for details

+ 114
- 0
bench.go View File

@@ -0,0 +1,114 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"math"
6
+	"sort"
7
+	"strings"
8
+	"time"
9
+
10
+	"github.com/miekg/dns"
11
+	"github.com/pkg/errors"
12
+)
13
+
14
+type benchResult struct {
15
+	DNSSECSupport bool
16
+	TimeResults   TimeResults
17
+}
18
+
19
+type TimeResults struct {
20
+	P0Dot5  time.Duration
21
+	P5      time.Duration
22
+	P25     time.Duration
23
+	P50     time.Duration
24
+	P75     time.Duration
25
+	P95     time.Duration
26
+	P99Dot5 time.Duration
27
+	Average time.Duration
28
+}
29
+
30
+type ProgressCallback func(i, n uint16)
31
+
32
+func bench(dnsServer, target string, measurements uint16, cb ProgressCallback) (*benchResult, error) {
33
+	if !strings.Contains(dnsServer, ":") {
34
+		dnsServer += ":53"
35
+	}
36
+	c := new(dns.Client)
37
+	c.SingleInflight = true
38
+
39
+	result := &benchResult{
40
+		DNSSECSupport: false,
41
+	}
42
+
43
+	fmt.Println("Checking DNSSEC...")
44
+	// verify DNSSEC
45
+	m := new(dns.Msg)
46
+	m.SetEdns0(4096, true) // Set DNSSEC OK
47
+	m.SetQuestion("www.dnssec-failed.org.", dns.TypeA)
48
+	r, _, err := c.Exchange(m, dnsServer)
49
+	if err != nil {
50
+		return nil, errors.Wrap(err, "Could not check DNSSEC")
51
+	}
52
+	if r.Rcode == dns.RcodeServerFailure {
53
+		result.DNSSECSupport = true
54
+	}
55
+
56
+	// execute measurements
57
+	var ttls = make([]time.Duration, 0)
58
+	for i := measurements; i > 0; i-- {
59
+		cb(measurements-i, measurements)
60
+		time.Sleep(time.Duration(*sleepTimeout) * time.Millisecond)
61
+		q := new(dns.Msg)
62
+		if *antiCache {
63
+			q.SetQuestion(RandStringRunes(4)+"."+target+".", dns.TypeA)
64
+		} else {
65
+			q.SetQuestion(target+".", dns.TypeA)
66
+		}
67
+		r, ttl, err := c.Exchange(q, dnsServer)
68
+		if err != nil {
69
+			return nil, errors.Wrap(err, "Bench Question failed")
70
+		}
71
+		if len(r.Answer) != 1 {
72
+			return nil, errors.New("DNS has no response Answers")
73
+		}
74
+		ttls = append(ttls, ttl)
75
+	}
76
+
77
+	sort.Slice(ttls, func(i, j int) bool {
78
+		// sort worst first
79
+		return ttls[i].Nanoseconds() > ttls[j].Nanoseconds()
80
+	})
81
+
82
+	var avg int64
83
+	for k := range ttls {
84
+		avg += ttls[k].Nanoseconds()
85
+	}
86
+	avg /= int64(len(ttls))
87
+
88
+	result.TimeResults.Average = time.Duration(avg) * time.Nanosecond
89
+
90
+	result.TimeResults.P0Dot5 = Px(ttls, 0.005)
91
+	result.TimeResults.P5 = Px(ttls, 0.05)
92
+	result.TimeResults.P25 = Px(ttls, 0.25)
93
+	result.TimeResults.P50 = Px(ttls, 0.50)
94
+	result.TimeResults.P75 = Px(ttls, 0.75)
95
+	result.TimeResults.P95 = Px(ttls, 0.95)
96
+	result.TimeResults.P99Dot5 = Px(ttls, 0.995)
97
+
98
+	return result, nil
99
+}
100
+
101
+func Px(sl []time.Duration, p float64) time.Duration {
102
+	var res int64
103
+	numR := int64(
104
+		math.Min(
105
+			math.Ceil(p*float64(len(sl))),
106
+			float64(len(sl)),
107
+		),
108
+	)
109
+	for i := int64(0); i < numR && i < int64(len(sl)); i++ {
110
+		res += sl[i].Nanoseconds()
111
+	}
112
+	res /= numR
113
+	return time.Duration(res) * time.Nanosecond
114
+}

+ 53
- 0
main.go View File

@@ -0,0 +1,53 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+
6
+	"github.com/schollz/progressbar"
7
+
8
+	"gopkg.in/alecthomas/kingpin.v2"
9
+)
10
+
11
+var (
12
+	numRequests      = kingpin.Flag("i", "Number of Requests per DNS server").Default("1000").Short('i').Uint16()
13
+	wildcardEndpoint = kingpin.Flag("e", "DNS Wildcard Endpoint to benchmark against").Default("google.com").Short('e').String()
14
+	dnsServers       = kingpin.Arg("dns servers", "DNS Servers to ping").Default("127.0.0.1", "8.8.8.8").Strings()
15
+	sleepTimeout     = kingpin.Flag("wait-request", "Time to wait between requests in milliseconds").Default("100").Uint()
16
+	antiCache        = kingpin.Flag("anti-cache", "Prepend randomized subdomains to query to prevent some caching. THIS REQUIRES A WILDCARD DNS ENTRY!").Default("false").Bool()
17
+)
18
+
19
+func main() {
20
+	kingpin.Parse()
21
+	for k := range *dnsServers {
22
+		fmt.Printf("Testing %s...\n", (*dnsServers)[k])
23
+		bar := progressbar.New(int(*numRequests))
24
+		res, err := bench((*dnsServers)[k], *wildcardEndpoint, *numRequests, func(i, _ uint16) {
25
+			bar.Set(int(i))
26
+		})
27
+		if err != nil {
28
+			fmt.Printf("Error: %s\n", err)
29
+			return
30
+		}
31
+		fmt.Printf("\n"+
32
+			"\tP00.5  = % 6.3fms\n"+
33
+			"\tP05.0  = % 6.3fms\n"+
34
+			"\tP25.0  = % 6.3fms\n"+
35
+			"\tP50.0  = % 6.3fms\n"+
36
+			"\tP75.0  = % 6.3fms\n"+
37
+			"\tP95.0  = % 6.3fms\n"+
38
+			"\tP99.5  = % 6.3fms\n"+
39
+			"\tAVG    = % 6.3fms\n"+
40
+			"\tDNSSEC = % 6t\n",
41
+			res.TimeResults.P0Dot5.Seconds()*1000,
42
+			res.TimeResults.P5.Seconds()*1000,
43
+			res.TimeResults.P25.Seconds()*1000,
44
+			res.TimeResults.P50.Seconds()*1000,
45
+			res.TimeResults.P75.Seconds()*1000,
46
+			res.TimeResults.P95.Seconds()*1000,
47
+			res.TimeResults.P99Dot5.Seconds()*1000,
48
+			res.TimeResults.Average.Seconds()*1000,
49
+			res.DNSSECSupport,
50
+		)
51
+		fmt.Println("")
52
+	}
53
+}

+ 20
- 0
rnd.go View File

@@ -0,0 +1,20 @@
1
+package main
2
+
3
+import (
4
+	"math/rand"
5
+	"time"
6
+)
7
+
8
+func init() {
9
+	rand.Seed(time.Now().UnixNano())
10
+}
11
+
12
+var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
13
+
14
+func RandStringRunes(n int) string {
15
+	b := make([]rune, n)
16
+	for i := range b {
17
+		b[i] = letterRunes[rand.Intn(len(letterRunes))]
18
+	}
19
+	return string(b)
20
+}

Loading…
Cancel
Save