| 1 |
package dnsperf |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"regexp" |
| 12 |
"runtime" |
| 13 |
"strconv" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
) |
| 17 |
|
| 18 |
// Run runs dnsperf which read the queries from the io.Reader and benchmarks the server running on addr. The |
| 19 |
// test runs for duration. The thread count is set the runtime.NumCPU/2, as both the server and requestor are |
| 20 |
// running on the same machine. I.e. |
| 21 |
// |
| 22 |
// Run(q, "127.0.0.1:8053", udp, 2*time.Second, 10) |
| 23 |
// |
| 24 |
// runs: "dnsperf -s 127.0.0.1 -p 8053 -l 2 -c 2 -T 2", 10 times (if runtime.NumCPU returns 4). |
| 25 |
// |
| 26 |
// See dnsperf(1) on how to create queries. The queries io.Reader is drained and placed in a file. |
| 27 |
// |
| 28 |
// The output is simular to running Go benchmark tests and can be used in benchstat. See |
| 29 |
// https://pkg.go.dev/golang.org/x/perf/cmd/benchstat |
| 30 |
func Run(t *testing.T, queries io.Reader, addr, network string, duration time.Duration, count int) error { |
| 31 |
p, err := io.ReadAll(queries) |
| 32 |
if err != nil { |
| 33 |
return err |
| 34 |
} |
| 35 |
dir := t.TempDir() |
| 36 |
if err := os.WriteFile(dir+"/queries.txt", p, 0600); err != nil { |
| 37 |
return err |
| 38 |
} |
| 39 |
|
| 40 |
host, port, err := net.SplitHostPort(addr) |
| 41 |
if err != nil { |
| 42 |
return err |
| 43 |
} |
| 44 |
args := []string{ |
| 45 |
"-s", host, |
| 46 |
"-p", port, |
| 47 |
"-t", "7", // timeout, see some tcp queries lagging |
| 48 |
"-d", dir + "/queries.txt", |
| 49 |
"-l", fmt.Sprintf("%d", int(duration.Seconds())), |
| 50 |
"-c", strconv.Itoa(runtime.NumCPU() / 2), // clients |
| 51 |
"-T", strconv.Itoa(runtime.NumCPU() / 2), // threads |
| 52 |
} |
| 53 |
if network == "tcp" { |
| 54 |
args = append(args, "-m", network) |
| 55 |
} |
| 56 |
|
| 57 |
for i := range count { |
| 58 |
cmd := exec.Command("dnsperf", args...) |
| 59 |
if p, err = cmd.CombinedOutput(); err != nil { |
| 60 |
t.Logf("%s", p) |
| 61 |
return err |
| 62 |
} |
| 63 |
|
| 64 |
qps, lost := queriesPerSecond(bytes.NewReader(p)) |
| 65 |
if qps == 0 { |
| 66 |
return fmt.Errorf("could not determine queries per second") |
| 67 |
} |
| 68 |
if lost > 0 { |
| 69 |
return fmt.Errorf("seen %f lost queries, inconclusive test", lost) |
| 70 |
} |
| 71 |
// fake this a bit: |
| 72 |
// goos: linux |
| 73 |
// goarch: arm64 |
| 74 |
// pkg: codeberg.org/miekg/dns |
| 75 |
if i == 0 { |
| 76 |
fmt.Printf(`goos: %s |
| 77 |
goarch: %s |
| 78 |
pkg: codeberg.org/miekg/dns |
| 79 |
`, runtime.GOOS, runtime.GOARCH) |
| 80 |
} |
| 81 |
|
| 82 |
nsPerOp := 1e9 / qps |
| 83 |
fmt.Printf("%s%s-%d\t%d\t\t\t%.1f ns/op\n", name, network, runtime.NumCPU(), int(qps), nsPerOp) |
| 84 |
} |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
const name = "BenchmarkDNSPerf" |
| 89 |
|
| 90 |
var ( |
| 91 |
qpsregex = regexp.MustCompile(`Queries per second:\s+([\d.]+)`) |
| 92 |
lostregex = regexp.MustCompile(`Queries lost:\s+([\d.]+)`) |
| 93 |
) |
| 94 |
|
| 95 |
func queriesPerSecond(r io.Reader) (qps, lost float64) { |
| 96 |
scanner := bufio.NewScanner(r) |
| 97 |
|
| 98 |
for scanner.Scan() { |
| 99 |
line := scanner.Text() |
| 100 |
if matches := qpsregex.FindStringSubmatch(line); len(matches) > 1 { |
| 101 |
qps, _ = strconv.ParseFloat(matches[1], 64) |
| 102 |
} |
| 103 |
if matches := lostregex.FindStringSubmatch(line); len(matches) > 1 { |
| 104 |
lost, _ = strconv.ParseFloat(matches[1], 64) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return qps, lost |
| 109 |
} |