git » dnss » commit 05af0de

dnstox: Add a simple benchmark for the caching resolver

author Alberto Bertogli
2016-05-30 10:24:17 UTC
committer Alberto Bertogli
2016-05-30 10:25:13 UTC
parent 4094141112cb2292fa989001c364a005a0086bb3

dnstox: Add a simple benchmark for the caching resolver

internal/dnstox/caching_test.go +27 -2
testing/util/util.go +11 -0

diff --git a/internal/dnstox/caching_test.go b/internal/dnstox/caching_test.go
index f798f2b..15200f7 100644
--- a/internal/dnstox/caching_test.go
+++ b/internal/dnstox/caching_test.go
@@ -243,6 +243,31 @@ func TestZeroSize(t *testing.T) {
 	}
 }
 
+//
+// === Benchmarks ===
+//
+
+func BenchmarkCacheSimple(b *testing.B) {
+	var err error
+
+	r := NewTestResolver()
+	r.response = newReply(mustNewRR(b, "test. A 1.2.3.4"))
+
+	c := NewCachingResolver(r)
+	c.Init()
+
+	tr := &util.NullTrace{}
+	req := newQuery("test.", dns.TypeA)
+
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		_, err = c.Query(req, tr)
+		if err != nil {
+			b.Errorf("query failed: %v", err)
+		}
+	}
+}
+
 //
 // === Helpers ===
 //
@@ -313,10 +338,10 @@ func queryFail(t *testing.T, c *cachingResolver) *dns.Msg {
 	return resp
 }
 
-func mustNewRR(t *testing.T, s string) dns.RR {
+func mustNewRR(tb testing.TB, s string) dns.RR {
 	rr, err := dns.NewRR(s)
 	if err != nil {
-		t.Fatalf("invalid RR %q: %v", s, err)
+		tb.Fatalf("invalid RR %q: %v", s, err)
 	}
 	return rr
 }
diff --git a/testing/util/util.go b/testing/util/util.go
index 1f046a2..975cef6 100644
--- a/testing/util/util.go
+++ b/testing/util/util.go
@@ -64,3 +64,14 @@ func (t *TestTrace) SetRecycler(f func(interface{}))     {}
 func (t *TestTrace) SetTraceInfo(traceID, spanID uint64) {}
 func (t *TestTrace) SetMaxEvents(m int)                  {}
 func (t *TestTrace) Finish()                             {}
+
+// NullTrace implements the tracer.Trace interface, but discards everything.
+type NullTrace struct{}
+
+func (t *NullTrace) LazyLog(x fmt.Stringer, sensitive bool)     {}
+func (t *NullTrace) LazyPrintf(format string, a ...interface{}) {}
+func (t *NullTrace) SetError()                                  {}
+func (t *NullTrace) SetRecycler(f func(interface{}))            {}
+func (t *NullTrace) SetTraceInfo(traceID, spanID uint64)        {}
+func (t *NullTrace) SetMaxEvents(m int)                         {}
+func (t *NullTrace) Finish()                                    {}