fa33568fab
Merge in DNS/adguard-home from imp-errcheck to master Squashed commit of the following: commit ed046b8ef59a092a27c623cd14b3fc2ef305fc3d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 15:00:04 2021 +0300 stats: imp cleanup more commit e53a9240d3e3eec2417c768b98c267a8cd54d992 Merge: da734317676f8c76
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:59:40 2021 +0300 Merge branch 'master' into imp-errcheck commit da734317035543b52e5a9030813084bdc92ba90a Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:37:26 2021 +0300 stats: imp cleanup commit 8b4ad150129111a09be6fa2944a21bd06ab8e5a1 Merge: 385c8a6c5081ead0
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:34:26 2021 +0300 Merge branch 'master' into imp-errcheck commit 385c8a6c91e3bf07a457da370c8cc77820b91600 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Jan 29 20:41:57 2021 +0300 all: imp errcheck in tests
167 lines
3.5 KiB
Go
167 lines
3.5 KiB
Go
package stats
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
func UIntArrayEquals(a, b []uint64) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func TestStats(t *testing.T) {
|
|
conf := Config{
|
|
Filename: "./stats.db",
|
|
LimitDays: 1,
|
|
}
|
|
t.Cleanup(func() {
|
|
assert.Nil(t, os.Remove(conf.Filename))
|
|
})
|
|
|
|
s, _ := createObject(conf)
|
|
|
|
e := Entry{}
|
|
|
|
e.Domain = "domain"
|
|
e.Client = "127.0.0.1"
|
|
e.Result = RFiltered
|
|
e.Time = 123456
|
|
s.Update(e)
|
|
|
|
e.Domain = "domain"
|
|
e.Client = "127.0.0.1"
|
|
e.Result = RNotFiltered
|
|
e.Time = 123456
|
|
s.Update(e)
|
|
|
|
d, ok := s.getData()
|
|
assert.True(t, ok)
|
|
|
|
a := []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}
|
|
assert.True(t, UIntArrayEquals(d.DNSQueries, a))
|
|
|
|
a = []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}
|
|
assert.True(t, UIntArrayEquals(d.BlockedFiltering, a))
|
|
|
|
a = []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}
|
|
assert.True(t, UIntArrayEquals(d.ReplacedSafebrowsing, a))
|
|
|
|
a = []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}
|
|
assert.True(t, UIntArrayEquals(d.ReplacedParental, a))
|
|
|
|
m := d.TopQueried
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
|
|
|
m = d.TopBlocked
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
|
|
|
m = d.TopClients
|
|
assert.EqualValues(t, 2, m[0]["127.0.0.1"])
|
|
|
|
assert.EqualValues(t, 2, d.NumDNSQueries)
|
|
assert.EqualValues(t, 1, d.NumBlockedFiltering)
|
|
assert.EqualValues(t, 0, d.NumReplacedSafebrowsing)
|
|
assert.EqualValues(t, 0, d.NumReplacedSafesearch)
|
|
assert.EqualValues(t, 0, d.NumReplacedParental)
|
|
assert.EqualValues(t, 0.123456, d.AvgProcessingTime)
|
|
|
|
topClients := s.GetTopClientsIP(2)
|
|
assert.True(t, net.IP{127, 0, 0, 1}.Equal(topClients[0]))
|
|
|
|
s.clear()
|
|
s.Close()
|
|
}
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
|
var hour int32 = 1
|
|
newID := func() uint32 {
|
|
// use "atomic" to make Go race detector happy
|
|
return uint32(atomic.LoadInt32(&hour))
|
|
}
|
|
|
|
// log.SetLevel(log.DEBUG)
|
|
conf := Config{
|
|
Filename: "./stats.db",
|
|
LimitDays: 1,
|
|
UnitID: newID,
|
|
}
|
|
t.Cleanup(func() {
|
|
assert.Nil(t, os.Remove(conf.Filename))
|
|
})
|
|
|
|
s, _ := createObject(conf)
|
|
e := Entry{}
|
|
|
|
n := 1000 // number of distinct clients and domains every hour
|
|
for h := 0; h != 12; h++ {
|
|
if h != 0 {
|
|
atomic.AddInt32(&hour, 1)
|
|
}
|
|
for i := 0; i != n; i++ {
|
|
e.Domain = fmt.Sprintf("domain%d", i)
|
|
ip := net.IP{127, 0, 0, 1}
|
|
ip[2] = byte((i & 0xff00) >> 8)
|
|
ip[3] = byte(i & 0xff)
|
|
e.Client = ip.String()
|
|
e.Result = RNotFiltered
|
|
e.Time = 123456
|
|
s.Update(e)
|
|
}
|
|
}
|
|
|
|
d, ok := s.getData()
|
|
assert.True(t, ok)
|
|
assert.EqualValues(t, int(hour)*n, d.NumDNSQueries)
|
|
|
|
s.Close()
|
|
}
|
|
|
|
// this code is a chunk copied from getData() that generates aggregate data per day
|
|
func aggregateDataPerDay(firstID uint32) int {
|
|
firstDayID := (firstID + 24 - 1) / 24 * 24 // align_ceil(24)
|
|
a := []uint64{}
|
|
var sum uint64
|
|
id := firstDayID
|
|
nextDayID := firstDayID + 24
|
|
for i := firstDayID - firstID; int(i) != 720; i++ {
|
|
sum++
|
|
if id == nextDayID {
|
|
a = append(a, sum)
|
|
sum = 0
|
|
nextDayID += 24
|
|
}
|
|
id++
|
|
}
|
|
if id <= nextDayID {
|
|
a = append(a, sum)
|
|
}
|
|
return len(a)
|
|
}
|
|
|
|
func TestAggregateDataPerTimeUnit(t *testing.T) {
|
|
for i := 0; i != 25; i++ {
|
|
alen := aggregateDataPerDay(uint32(i))
|
|
assert.Equalf(t, 30, alen, "i=%d", i)
|
|
}
|
|
}
|