mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 06:25:44 +03:00
d3862614e5
Updates #6220.
Squashed commit of the following:
commit 2ad87979b4f8ade2cad8a1b86b8e0663dd53b098
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Nov 9 13:55:09 2023 +0300
all: upd go
commit 387200c3de899e6f59e8d2455cb2aa2b7194b311
Merge: 37f2855ed f8fe9bfc8
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Nov 9 13:49:59 2023 +0300
Merge branch 'master' into 6220-average-processing-time
commit 37f2855ed104c20faa3f4d57d48bd221a59cacb3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Nov 8 17:08:13 2023 +0300
client: upd locales
commit a09be4183811cef29b594f9916dc76b87f89d304
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Nov 7 20:46:24 2023 +0300
all: imp docs
commit f0b85ac1fef366da37b996d53e29d76d6279691f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Fri Nov 3 14:17:32 2023 +0300
all: add todo
commit 48a5879865a1625410787edef6d78b309056af7b
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Fri Nov 3 13:38:14 2023 +0300
all: add upstream time
commit 4d7431c00e24f8a9d3e86160851b5ef3bd9d03fa
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Oct 31 17:27:19 2023 +0300
all: upd chlog
commit 040b6b9cdebea2c9789d4d38f2a0a40ef4cb26d7
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Oct 31 15:42:04 2023 +0300
all: imp average processing time
272 lines
7 KiB
Go
272 lines
7 KiB
Go
package stats_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/miekg/dns"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
// constUnitID is the UnitIDGenFunc which always return 0.
|
|
func constUnitID() (id uint32) { return 0 }
|
|
|
|
func assertSuccessAndUnmarshal(t *testing.T, to any, handler http.Handler, req *http.Request) {
|
|
t.Helper()
|
|
|
|
require.NotNil(t, handler)
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
require.Equal(t, http.StatusOK, rw.Code)
|
|
|
|
data := rw.Body.Bytes()
|
|
if to == nil {
|
|
assert.Empty(t, data)
|
|
|
|
return
|
|
}
|
|
|
|
err := json.Unmarshal(data, to)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestStats(t *testing.T) {
|
|
cliIP := netutil.IPv4Localhost()
|
|
cliIPStr := cliIP.String()
|
|
|
|
handlers := map[string]http.Handler{}
|
|
conf := stats.Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Enabled: true,
|
|
UnitID: constUnitID,
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) {
|
|
handlers[url] = handler
|
|
},
|
|
}
|
|
|
|
s, err := stats.New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
t.Run("data", func(t *testing.T) {
|
|
const reqDomain = "domain"
|
|
const respUpstream = "upstream"
|
|
|
|
entries := []*stats.Entry{{
|
|
Domain: reqDomain,
|
|
Client: cliIPStr,
|
|
Result: stats.RFiltered,
|
|
ProcessingTime: time.Microsecond * 123456,
|
|
Upstream: respUpstream,
|
|
UpstreamTime: time.Microsecond * 222222,
|
|
}, {
|
|
Domain: reqDomain,
|
|
Client: cliIPStr,
|
|
Result: stats.RNotFiltered,
|
|
ProcessingTime: time.Microsecond * 123456,
|
|
Upstream: respUpstream,
|
|
UpstreamTime: time.Microsecond * 222222,
|
|
}}
|
|
|
|
wantData := &stats.StatsResp{
|
|
TimeUnits: "hours",
|
|
TopQueried: []map[string]uint64{0: {reqDomain: 1}},
|
|
TopClients: []map[string]uint64{0: {cliIPStr: 2}},
|
|
TopBlocked: []map[string]uint64{0: {reqDomain: 1}},
|
|
TopUpstreamsResponses: []map[string]uint64{0: {respUpstream: 2}},
|
|
TopUpstreamsAvgTime: []map[string]float64{0: {respUpstream: 0.222222}},
|
|
DNSQueries: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
|
|
},
|
|
BlockedFiltering: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
},
|
|
ReplacedSafebrowsing: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
},
|
|
ReplacedParental: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
},
|
|
NumDNSQueries: 2,
|
|
NumBlockedFiltering: 1,
|
|
NumReplacedSafebrowsing: 0,
|
|
NumReplacedSafesearch: 0,
|
|
NumReplacedParental: 0,
|
|
AvgProcessingTime: 0.123456,
|
|
}
|
|
|
|
for _, e := range entries {
|
|
s.Update(e)
|
|
}
|
|
|
|
data := &stats.StatsResp{}
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
|
|
assert.Equal(t, wantData, data)
|
|
})
|
|
|
|
t.Run("tops", func(t *testing.T) {
|
|
topClients := s.TopClientsIP(2)
|
|
require.NotEmpty(t, topClients)
|
|
|
|
assert.Equal(t, cliIP, topClients[0])
|
|
})
|
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/control/stats_reset", nil)
|
|
assertSuccessAndUnmarshal(t, nil, handlers["/control/stats_reset"], req)
|
|
|
|
_24zeroes := [24]uint64{}
|
|
emptyData := &stats.StatsResp{
|
|
TimeUnits: "hours",
|
|
TopQueried: []map[string]uint64{},
|
|
TopClients: []map[string]uint64{},
|
|
TopBlocked: []map[string]uint64{},
|
|
TopUpstreamsResponses: []map[string]uint64{},
|
|
TopUpstreamsAvgTime: []map[string]float64{},
|
|
DNSQueries: _24zeroes[:],
|
|
BlockedFiltering: _24zeroes[:],
|
|
ReplacedSafebrowsing: _24zeroes[:],
|
|
ReplacedParental: _24zeroes[:],
|
|
}
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
data := &stats.StatsResp{}
|
|
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
assert.Equal(t, emptyData, data)
|
|
})
|
|
}
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
|
var curHour uint32 = 1
|
|
handlers := map[string]http.Handler{}
|
|
|
|
conf := stats.Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Enabled: true,
|
|
UnitID: func() (id uint32) { return atomic.LoadUint32(&curHour) },
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) { handlers[url] = handler },
|
|
}
|
|
|
|
s, err := stats.New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
const (
|
|
hoursNum = 12
|
|
cliNumPerHour = 1000
|
|
)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
|
|
for h := 0; h < hoursNum; h++ {
|
|
atomic.AddUint32(&curHour, 1)
|
|
|
|
for i := 0; i < cliNumPerHour; i++ {
|
|
ip := net.IP{127, 0, byte((i & 0xff00) >> 8), byte(i & 0xff)}
|
|
e := &stats.Entry{
|
|
Domain: fmt.Sprintf("domain%d.hour%d", i, h),
|
|
Client: ip.String(),
|
|
Result: stats.RNotFiltered,
|
|
ProcessingTime: 123456,
|
|
}
|
|
s.Update(e)
|
|
}
|
|
}
|
|
|
|
data := &stats.StatsResp{}
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
assert.Equal(t, hoursNum*cliNumPerHour, int(data.NumDNSQueries))
|
|
}
|
|
|
|
func TestShouldCount(t *testing.T) {
|
|
const (
|
|
ignored1 = "ignor.ed"
|
|
ignored2 = "ignored.to"
|
|
)
|
|
ignored := []string{ignored1, ignored2}
|
|
engine, err := aghnet.NewIgnoreEngine(ignored)
|
|
require.NoError(t, err)
|
|
|
|
s, err := stats.New(stats.Config{
|
|
Enabled: true,
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Ignored: engine,
|
|
ShouldCountClient: func(ids []string) (a bool) {
|
|
return ids[0] != "no_count"
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
testCases := []struct {
|
|
wantCount assert.BoolAssertionFunc
|
|
name string
|
|
host string
|
|
ids []string
|
|
}{{
|
|
name: "count",
|
|
host: "example.com",
|
|
ids: []string{"whatever"},
|
|
wantCount: assert.True,
|
|
}, {
|
|
name: "no_count_ignored_1",
|
|
host: ignored1,
|
|
ids: []string{"whatever"},
|
|
wantCount: assert.False,
|
|
}, {
|
|
name: "no_count_ignored_2",
|
|
host: ignored2,
|
|
ids: []string{"whatever"},
|
|
wantCount: assert.False,
|
|
}, {
|
|
name: "no_count_client_ignore",
|
|
host: "example.com",
|
|
ids: []string{"no_count"},
|
|
wantCount: assert.False,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
res := s.ShouldCount(tc.host, dns.TypeA, dns.ClassINET, tc.ids)
|
|
|
|
tc.wantCount(t, res)
|
|
})
|
|
}
|
|
}
|