2022-08-17 14:09:13 +03:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
2022-09-05 16:53:00 +03:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-08-17 14:09:13 +03:00
|
|
|
"testing"
|
2022-09-05 16:53:00 +03:00
|
|
|
"time"
|
2022-08-17 14:09:13 +03:00
|
|
|
|
2024-09-09 13:31:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2022-09-05 16:53:00 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2023-03-23 13:46:57 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2022-08-17 14:09:13 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-09-05 16:53:00 +03:00
|
|
|
func TestStats_races(t *testing.T) {
|
|
|
|
var r uint32
|
|
|
|
idGen := func() (id uint32) { return atomic.LoadUint32(&r) }
|
|
|
|
conf := Config{
|
2024-09-09 13:31:54 +03:00
|
|
|
Logger: slogutil.NewDiscardLogger(),
|
2023-04-07 13:17:40 +03:00
|
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
|
|
UnitID: idGen,
|
|
|
|
Filename: filepath.Join(t.TempDir(), "./stats.db"),
|
|
|
|
Limit: timeutil.Day,
|
2022-09-05 16:53:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-08-09 14:33:52 +03:00
|
|
|
e := &Entry{
|
2023-11-09 16:14:05 +03:00
|
|
|
Domain: fmt.Sprintf("example-%d.org", i),
|
|
|
|
Client: fmt.Sprintf("client_%d", i),
|
|
|
|
Result: Result(i)%(resultLast-1) + 1,
|
|
|
|
ProcessingTime: time.Since(startTime),
|
2022-09-05 16:53:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
for i := range writersNum {
|
2022-09-05 16:53:00 +03:00
|
|
|
startWG.Add(1)
|
|
|
|
finWG.Add(1)
|
|
|
|
go writeFunc(startWG, finWG, waitCh, i)
|
|
|
|
}
|
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
for range readersNum {
|
2022-09-05 16:53:00 +03:00
|
|
|
startWG.Add(1)
|
|
|
|
finWG.Add(1)
|
|
|
|
go readFunc(startWG, finWG, waitCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
startWG.Wait()
|
|
|
|
close(waitCh)
|
|
|
|
finWG.Wait()
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 17:09:42 +03:00
|
|
|
|
|
|
|
func TestStatsCtx_FillCollectedStats_daily(t *testing.T) {
|
|
|
|
const (
|
|
|
|
daysCount = 10
|
|
|
|
|
|
|
|
timeUnits = "days"
|
|
|
|
)
|
|
|
|
|
|
|
|
s, err := New(Config{
|
2024-09-09 13:31:54 +03:00
|
|
|
Logger: slogutil.NewDiscardLogger(),
|
2023-08-23 17:09:42 +03:00
|
|
|
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{}
|
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
for i := range daysCount * 24 {
|
2023-08-23 17:09:42 +03:00
|
|
|
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{
|
2024-09-09 13:31:54 +03:00
|
|
|
Logger: slogutil.NewDiscardLogger(),
|
2023-08-23 17:09:42 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|