mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 08:44:17 -04:00
Fix log plugin benchmark and slightly improve performance (#3004)
* log: use ioutil.Discard as write buffer in benchmark Using a buffer gives unrealistic stats and consumes a large amount of memory. * log: lazily check if a msg should be logged * log: improve variable name Change 'ok' to the more descriptive 'shouldLog'. * log: code comments: don't reuse variable
This commit is contained in:
committed by
Miek Gieben
parent
767d399877
commit
21e9c6047b
@@ -26,20 +26,24 @@ type Logger struct {
|
|||||||
// ServeDNS implements the plugin.Handler interface.
|
// ServeDNS implements the plugin.Handler interface.
|
||||||
func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||||
state := request.Request{W: w, Req: r}
|
state := request.Request{W: w, Req: r}
|
||||||
|
name := state.Name()
|
||||||
for _, rule := range l.Rules {
|
for _, rule := range l.Rules {
|
||||||
if !plugin.Name(rule.NameScope).Matches(state.Name()) {
|
if !plugin.Name(rule.NameScope).Matches(name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
rrw := dnstest.NewRecorder(w)
|
rrw := dnstest.NewRecorder(w)
|
||||||
rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)
|
rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)
|
||||||
|
|
||||||
tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
|
|
||||||
class := response.Classify(tpe)
|
|
||||||
// If we don't set up a class in config, the default "all" will be added
|
// If we don't set up a class in config, the default "all" will be added
|
||||||
// and we shouldn't have an empty rule.Class.
|
// and we shouldn't have an empty rule.Class.
|
||||||
_, ok := rule.Class[response.All]
|
_, ok := rule.Class[response.All]
|
||||||
_, ok1 := rule.Class[class]
|
var ok1 bool
|
||||||
|
if !ok {
|
||||||
|
tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
|
||||||
|
class := response.Classify(tpe)
|
||||||
|
_, ok1 = rule.Class[class]
|
||||||
|
}
|
||||||
if ok || ok1 {
|
if ok || ok1 {
|
||||||
logstr := l.repl.Replace(ctx, state, rrw, rule.Format)
|
logstr := l.repl.Replace(ctx, state, rrw, rule.Format)
|
||||||
clog.Infof(logstr)
|
clog.Infof(logstr)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package log
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -239,8 +240,7 @@ func TestLogged(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkLogged(b *testing.B) {
|
func BenchmarkLogged(b *testing.B) {
|
||||||
var f bytes.Buffer
|
log.SetOutput(ioutil.Discard)
|
||||||
log.SetOutput(&f)
|
|
||||||
|
|
||||||
rule := Rule{
|
rule := Rule{
|
||||||
NameScope: ".",
|
NameScope: ".",
|
||||||
|
|||||||
Reference in New Issue
Block a user