/
dnscache_test.go
175 lines (154 loc) · 4 KB
/
dnscache_test.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package dnscache
import (
"fmt"
"math/rand"
"net"
"testing"
"time"
"github.com/miekg/dns"
)
// TODO: Write some tests
type samplePool struct {
queries []dns.Question
answers map[dns.Question][]dns.RR
}
type generator struct {
rand.Source
}
func newGenerator(seed int64) *generator {
return &generator{rand.NewSource(seed)}
}
func (g *generator) RandString(n int) string {
// See http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, g.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = g.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func (g *generator) RandFQDN(i int) string {
output := g.RandString(i % 23)
if i%3 == 0 {
output += "." + g.RandString(i%19)
}
if i%7 == 0 {
output += "." + g.RandString(i%29)
}
return output
}
func (g *generator) DeterministicIPv4(i int) net.IP {
a := byte((i + 31) % 255)
b := byte((i + 67) % 255)
c := byte((i + 97) % 255)
d := byte((i + 157) % 255)
return net.IPv4(a, b, c, d)
}
func generateSamplePool(size int, seed int64) *samplePool {
g := newGenerator(seed)
sample := &samplePool{
queries: make([]dns.Question, 0, size),
answers: make(map[dns.Question][]dns.RR),
}
for i := 0; i < size; i++ {
var qtype uint16
switch i % 2 {
case 0:
qtype = dns.TypeTXT
case 1:
qtype = dns.TypeA
}
question := dns.Question{
Name: g.RandFQDN(i),
Qtype: qtype,
Qclass: dns.ClassINET,
}
sample.queries = append(sample.queries, question)
var answer dns.RR
switch question.Qtype {
case dns.TypeTXT:
answer = answerTXT(question, g.RandString(i%31))
case dns.TypeA:
answer = answerA(question, g.DeterministicIPv4(i))
}
sample.answers[question] = append([]dns.RR(nil), answer)
}
return sample
}
func (sp *samplePool) LookupFunc() func(dns.Question) []dns.RR {
return func(q dns.Question) []dns.RR {
return sp.answers[q]
}
}
func answerTXT(q dns.Question, value string) dns.RR {
answer := new(dns.TXT)
answer.Header().Name = q.Name
answer.Header().Rrtype = dns.TypeTXT
answer.Header().Class = dns.ClassINET
answer.Txt = []string{value}
return answer
}
func answerA(q dns.Question, value net.IP) dns.RR {
answer := new(dns.A)
answer.Header().Name = q.Name
answer.Header().Rrtype = dns.TypeA
answer.Header().Class = dns.ClassINET
answer.A = value
return answer
}
func newSampleCache(poolSize, bufferSize int, seed int64, maxTTL, missTTL time.Duration) (*samplePool, *Cache) {
sample := generateSamplePool(poolSize, seed)
cache := New(bufferSize, maxTTL, missTTL, sample.LookupFunc())
return sample, cache
}
func printRecords(rr []dns.RR) {
if len(rr) == 1 {
fmt.Printf("%v\n", rr[0])
} else {
fmt.Printf("%v\n", rr)
}
}
var tempRR []dns.RR
func benchmark(b *testing.B, poolSize, bufferSize int, seed int64) {
sample, cache := newSampleCache(poolSize, bufferSize, seed, time.Minute*5, time.Second*30)
//total := b.N
total := b.N
requests := make([]Request, total)
k := 0
for k < total {
for _, q := range sample.queries {
requests[k] = Request{
Question: q,
ResponseChan: make(chan []dns.RR),
}
k++
if k >= total {
break
}
}
}
b.ResetTimer()
for i := range requests {
cache.Lookup(requests[i])
}
for i := range requests {
tempRR = <-requests[i].ResponseChan
}
cache.Stop()
}
func BenchmarkRandPool100(b *testing.B) { benchmark(b, 100, 0, 78897) }
func BenchmarkRandPool1000(b *testing.B) { benchmark(b, 1000, 0, 215487113) }