mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +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
170 lines
3.6 KiB
Go
170 lines
3.6 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"
|
|
)
|
|
|
|
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,
|
|
ProcessingTime: time.Since(startTime),
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
func TestStatsCtx_FillCollectedStats_daily(t *testing.T) {
|
|
const (
|
|
daysCount = 10
|
|
|
|
timeUnits = "days"
|
|
)
|
|
|
|
s, err := New(Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "./stats.db"),
|
|
Limit: time.Hour,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
sum := make([][]uint64, resultLast)
|
|
sum[RFiltered] = make([]uint64, daysCount)
|
|
sum[RSafeBrowsing] = make([]uint64, daysCount)
|
|
sum[RParental] = make([]uint64, daysCount)
|
|
|
|
total := make([]uint64, daysCount)
|
|
|
|
dailyData := []*unitDB{}
|
|
|
|
for i := 0; i < daysCount*24; i++ {
|
|
n := uint64(i)
|
|
nResult := make([]uint64, resultLast)
|
|
nResult[RFiltered] = n
|
|
nResult[RSafeBrowsing] = n
|
|
nResult[RParental] = n
|
|
|
|
day := i / 24
|
|
sum[RFiltered][day] += n
|
|
sum[RSafeBrowsing][day] += n
|
|
sum[RParental][day] += n
|
|
|
|
t := n * 3
|
|
|
|
total[day] += t
|
|
|
|
dailyData = append(dailyData, &unitDB{
|
|
NTotal: t,
|
|
NResult: nResult,
|
|
})
|
|
}
|
|
|
|
data := &StatsResp{}
|
|
|
|
// In this way we will not skip first hours.
|
|
curID := uint32(daysCount * 24)
|
|
|
|
s.fillCollectedStats(data, dailyData, curID)
|
|
|
|
assert.Equal(t, timeUnits, data.TimeUnits)
|
|
assert.Equal(t, sum[RFiltered], data.BlockedFiltering)
|
|
assert.Equal(t, sum[RSafeBrowsing], data.ReplacedSafebrowsing)
|
|
assert.Equal(t, sum[RParental], data.ReplacedParental)
|
|
assert.Equal(t, total, data.DNSQueries)
|
|
}
|
|
|
|
func TestStatsCtx_DataFromUnits_month(t *testing.T) {
|
|
const hoursInMonth = 720
|
|
|
|
s, err := New(Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "./stats.db"),
|
|
Limit: time.Hour,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
units, curID := s.loadUnits(hoursInMonth)
|
|
require.Len(t, units, hoursInMonth)
|
|
|
|
var h uint32
|
|
for h = 1; h <= hoursInMonth; h++ {
|
|
data := s.dataFromUnits(units[:h], curID)
|
|
require.NotNil(t, data)
|
|
}
|
|
}
|