mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
f9fe3172c4
Merge in DNS/adguard-home from 4299-querylog-stats-clients to master Squashed commit of the following: commit 33b80b67224f7c1a15bee8e6a23d9d5bab6ac629 Merge: 61964fdd5d5a7295
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:43:22 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 61964fdd02221abbddedf2d6d02bb0bce6845362 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:42:01 2023 +0300 dnsforward: imp code commit 7382168500bab6ca7494d39aabfc2d7bfceb5d24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 11:13:07 2023 +0300 all: imp code, chlog commit c7852902f635af6c296dcb6735f7b0bfb83f4e87 Merge: aa4dc0a5a55cbbe7
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 14:34:24 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit aa4dc0a54e95bc5b24718ec158340b631a822801 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 12:54:02 2023 +0300 all: imp code commit dd541f0cd7ecbf0afcf10ccbd130fd1d1fa4c1c4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 13:01:53 2023 +0300 querylog: fix typo commit d2c8fdb35b04d27c8957fa027882fde704cc07be Merge: 83d0baa12eb3bf6e
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:36:49 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 83d0baa1f1202f9c62d4be2041d7aed12ee9ab2c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:35:15 2023 +0300 all: add tests commit a459f19f25cf9646d145813fe7834b2d9979c516 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 29 16:51:53 2023 +0300 all: add clients querylog stats ignore
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
package stats
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TODO(e.burkov): Use more realistic data.
|
|
func TestStatsCollector(t *testing.T) {
|
|
ng := func(_ *unitDB) uint64 { return 0 }
|
|
units := make([]*unitDB, 720)
|
|
|
|
t.Run("hours", func(t *testing.T) {
|
|
statsData := statsCollector(units, 0, Hours, ng)
|
|
assert.Len(t, statsData, 720)
|
|
})
|
|
|
|
t.Run("days", func(t *testing.T) {
|
|
for i := 0; i != 25; i++ {
|
|
statsData := statsCollector(units, uint32(i), Days, ng)
|
|
require.Lenf(t, statsData, 30, "i=%d", i)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestStats_races(t *testing.T) {
|
|
var r uint32
|
|
idGen := func() (id uint32) { return atomic.LoadUint32(&r) }
|
|
conf := Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
UnitID: idGen,
|
|
Filename: filepath.Join(t.TempDir(), "./stats.db"),
|
|
Limit: timeutil.Day,
|
|
}
|
|
|
|
s, err := New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
startTime := time.Now()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
writeFunc := func(start, fin *sync.WaitGroup, waitCh <-chan unit, i int) {
|
|
e := Entry{
|
|
Domain: fmt.Sprintf("example-%d.org", i),
|
|
Client: fmt.Sprintf("client_%d", i),
|
|
Result: Result(i)%(resultLast-1) + 1,
|
|
Time: uint32(time.Since(startTime).Milliseconds()),
|
|
}
|
|
|
|
start.Done()
|
|
defer fin.Done()
|
|
|
|
<-waitCh
|
|
|
|
s.Update(e)
|
|
}
|
|
readFunc := func(start, fin *sync.WaitGroup, waitCh <-chan unit) {
|
|
start.Done()
|
|
defer fin.Done()
|
|
|
|
<-waitCh
|
|
|
|
_, _ = s.getData(24)
|
|
}
|
|
|
|
const (
|
|
roundsNum = 3
|
|
|
|
writersNum = 10
|
|
readersNum = 5
|
|
)
|
|
|
|
for round := 0; round < roundsNum; round++ {
|
|
atomic.StoreUint32(&r, uint32(round))
|
|
|
|
startWG, finWG := &sync.WaitGroup{}, &sync.WaitGroup{}
|
|
waitCh := make(chan unit)
|
|
|
|
for i := 0; i < writersNum; i++ {
|
|
startWG.Add(1)
|
|
finWG.Add(1)
|
|
go writeFunc(startWG, finWG, waitCh, i)
|
|
}
|
|
|
|
for i := 0; i < readersNum; i++ {
|
|
startWG.Add(1)
|
|
finWG.Add(1)
|
|
go readFunc(startWG, finWG, waitCh)
|
|
}
|
|
|
|
startWG.Wait()
|
|
close(waitCh)
|
|
finWG.Wait()
|
|
}
|
|
}
|