mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	Cache elements of State
Cache the size and the do bit whenever someone asked for it. We can probably add more: PASS BenchmarkStateDo-4 100000000 11.9 ns/op BenchmarkStateSize-4 5000000 265 ns/op ok github.com/miekg/coredns/middleware 2.828s PASS BenchmarkStateDo-4 1000000000 2.86 ns/op BenchmarkStateSize-4 500000000 3.10 ns/op ok github.com/miekg/coredns/middleware 5.032s This PR also includes some testing cleanups as well.
This commit is contained in:
		| @@ -1,5 +1,46 @@ | ||||
| package middleware | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	coretest "github.com/miekg/coredns/middleware/testing" | ||||
|  | ||||
| 	"github.com/miekg/dns" | ||||
| ) | ||||
|  | ||||
| func TestStateDo(t *testing.T) { | ||||
| 	st := testState() | ||||
|  | ||||
| 	st.Do() | ||||
| 	if st.do == 0 { | ||||
| 		t.Fatalf("expected st.do to be set") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func BenchmarkStateDo(b *testing.B) { | ||||
| 	st := testState() | ||||
|  | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		st.Do() | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func BenchmarkStateSize(b *testing.B) { | ||||
| 	st := testState() | ||||
|  | ||||
| 	for i := 0; i < b.N; i++ { | ||||
| 		st.Size() | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func testState() State { | ||||
| 	m := new(dns.Msg) | ||||
| 	m.SetQuestion("example.com.", dns.TypeA) | ||||
| 	m.SetEdns0(4097, true) | ||||
|  | ||||
| 	return State{W: &coretest.ResponseWriter{}, Req: m} | ||||
| } | ||||
|  | ||||
| /* | ||||
| func TestHeader(t *testing.T) { | ||||
| 	state := getContextOrFail(t) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user