Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tscs37 committed Apr 1, 2018
0 parents commit e92972e
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dnsbench
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Tim Schuster

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# dnsbench

Simple DNS Benchmarking Tool

## Usage

```
$ ./dnsbench --help
usage: dnsbench [<flags>] [<dns servers>...]
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
-i, --i=1000 Number of Requests per DNS server
-e, --e="google.com" DNS Wildcard Endpoint to benchmark against
--wait-request=100 Time to wait between requests in milliseconds
--anti-cache Prepend randomized subdomains to query to prevent some caching. THIS
REQUIRES A WILDCARD DNS ENTRY!
Args:
[<dns servers>] DNS Servers to ping
```

It is recommended to change the benchmark endpoint.

## License

This tool is licensed under MIT License. See LICENSE for details
114 changes: 114 additions & 0 deletions bench.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"fmt"
"math"
"sort"
"strings"
"time"

"github.com/miekg/dns"
"github.com/pkg/errors"
)

type benchResult struct {
DNSSECSupport bool
TimeResults TimeResults
}

type TimeResults struct {
P0Dot5 time.Duration
P5 time.Duration
P25 time.Duration
P50 time.Duration
P75 time.Duration
P95 time.Duration
P99Dot5 time.Duration
Average time.Duration
}

type ProgressCallback func(i, n uint16)

func bench(dnsServer, target string, measurements uint16, cb ProgressCallback) (*benchResult, error) {
if !strings.Contains(dnsServer, ":") {
dnsServer += ":53"
}
c := new(dns.Client)
c.SingleInflight = true

result := &benchResult{
DNSSECSupport: false,
}

fmt.Println("Checking DNSSEC...")
// verify DNSSEC
m := new(dns.Msg)
m.SetEdns0(4096, true) // Set DNSSEC OK
m.SetQuestion("www.dnssec-failed.org.", dns.TypeA)
r, _, err := c.Exchange(m, dnsServer)
if err != nil {
return nil, errors.Wrap(err, "Could not check DNSSEC")
}
if r.Rcode == dns.RcodeServerFailure {
result.DNSSECSupport = true
}

// execute measurements
var ttls = make([]time.Duration, 0)
for i := measurements; i > 0; i-- {
cb(measurements-i, measurements)
time.Sleep(time.Duration(*sleepTimeout) * time.Millisecond)
q := new(dns.Msg)
if *antiCache {
q.SetQuestion(RandStringRunes(4)+"."+target+".", dns.TypeA)
} else {
q.SetQuestion(target+".", dns.TypeA)
}
r, ttl, err := c.Exchange(q, dnsServer)
if err != nil {
return nil, errors.Wrap(err, "Bench Question failed")
}
if len(r.Answer) != 1 {
return nil, errors.New("DNS has no response Answers")
}
ttls = append(ttls, ttl)
}

sort.Slice(ttls, func(i, j int) bool {
// sort worst first
return ttls[i].Nanoseconds() > ttls[j].Nanoseconds()
})

var avg int64
for k := range ttls {
avg += ttls[k].Nanoseconds()
}
avg /= int64(len(ttls))

result.TimeResults.Average = time.Duration(avg) * time.Nanosecond

result.TimeResults.P0Dot5 = Px(ttls, 0.005)
result.TimeResults.P5 = Px(ttls, 0.05)
result.TimeResults.P25 = Px(ttls, 0.25)
result.TimeResults.P50 = Px(ttls, 0.50)
result.TimeResults.P75 = Px(ttls, 0.75)
result.TimeResults.P95 = Px(ttls, 0.95)
result.TimeResults.P99Dot5 = Px(ttls, 0.995)

return result, nil
}

func Px(sl []time.Duration, p float64) time.Duration {
var res int64
numR := int64(
math.Min(
math.Ceil(p*float64(len(sl))),
float64(len(sl)),
),
)
for i := int64(0); i < numR && i < int64(len(sl)); i++ {
res += sl[i].Nanoseconds()
}
res /= numR
return time.Duration(res) * time.Nanosecond
}
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"

"github.com/schollz/progressbar"

"gopkg.in/alecthomas/kingpin.v2"
)

var (
numRequests = kingpin.Flag("i", "Number of Requests per DNS server").Default("1000").Short('i').Uint16()
wildcardEndpoint = kingpin.Flag("e", "DNS Wildcard Endpoint to benchmark against").Default("google.com").Short('e').String()
dnsServers = kingpin.Arg("dns servers", "DNS Servers to ping").Default("127.0.0.1", "8.8.8.8").Strings()
sleepTimeout = kingpin.Flag("wait-request", "Time to wait between requests in milliseconds").Default("100").Uint()
antiCache = kingpin.Flag("anti-cache", "Prepend randomized subdomains to query to prevent some caching. THIS REQUIRES A WILDCARD DNS ENTRY!").Default("false").Bool()
)

func main() {
kingpin.Parse()
for k := range *dnsServers {
fmt.Printf("Testing %s...\n", (*dnsServers)[k])
bar := progressbar.New(int(*numRequests))
res, err := bench((*dnsServers)[k], *wildcardEndpoint, *numRequests, func(i, _ uint16) {
bar.Set(int(i))
})
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
fmt.Printf("\n"+
"\tP00.5 = % 6.3fms\n"+
"\tP05.0 = % 6.3fms\n"+
"\tP25.0 = % 6.3fms\n"+
"\tP50.0 = % 6.3fms\n"+
"\tP75.0 = % 6.3fms\n"+
"\tP95.0 = % 6.3fms\n"+
"\tP99.5 = % 6.3fms\n"+
"\tAVG = % 6.3fms\n"+
"\tDNSSEC = % 6t\n",
res.TimeResults.P0Dot5.Seconds()*1000,
res.TimeResults.P5.Seconds()*1000,
res.TimeResults.P25.Seconds()*1000,
res.TimeResults.P50.Seconds()*1000,
res.TimeResults.P75.Seconds()*1000,
res.TimeResults.P95.Seconds()*1000,
res.TimeResults.P99Dot5.Seconds()*1000,
res.TimeResults.Average.Seconds()*1000,
res.DNSSECSupport,
)
fmt.Println("")
}
}
20 changes: 20 additions & 0 deletions rnd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")

func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

0 comments on commit e92972e

Please sign in to comment.