"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/stats/stats_test.go" (19 Nov 2020, 3691 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "stats_test.go": 0.104.1_vs_0.104.3.

    1 package stats
    2 
    3 import (
    4     "fmt"
    5     "net"
    6     "os"
    7     "sync/atomic"
    8     "testing"
    9 
   10     "github.com/AdguardTeam/AdGuardHome/internal/testutil"
   11     "github.com/stretchr/testify/assert"
   12 )
   13 
   14 func TestMain(m *testing.M) {
   15     testutil.DiscardLogOutput(m)
   16 }
   17 
   18 func UIntArrayEquals(a, b []uint64) bool {
   19     if len(a) != len(b) {
   20         return false
   21     }
   22 
   23     for i := range a {
   24         if a[i] != b[i] {
   25             return false
   26         }
   27     }
   28 
   29     return true
   30 }
   31 
   32 func TestStats(t *testing.T) {
   33     conf := Config{
   34         Filename:  "./stats.db",
   35         LimitDays: 1,
   36     }
   37     s, _ := createObject(conf)
   38 
   39     e := Entry{}
   40 
   41     e.Domain = "domain"
   42     e.Client = net.ParseIP("127.0.0.1")
   43     e.Result = RFiltered
   44     e.Time = 123456
   45     s.Update(e)
   46 
   47     e.Domain = "domain"
   48     e.Client = net.ParseIP("127.0.0.1")
   49     e.Result = RNotFiltered
   50     e.Time = 123456
   51     s.Update(e)
   52 
   53     d := s.getData()
   54     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}
   55     assert.True(t, UIntArrayEquals(d["dns_queries"].([]uint64), a))
   56 
   57     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}
   58     assert.True(t, UIntArrayEquals(d["blocked_filtering"].([]uint64), a))
   59 
   60     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}
   61     assert.True(t, UIntArrayEquals(d["replaced_safebrowsing"].([]uint64), a))
   62 
   63     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}
   64     assert.True(t, UIntArrayEquals(d["replaced_parental"].([]uint64), a))
   65 
   66     m := d["top_queried_domains"].([]map[string]uint64)
   67     assert.True(t, m[0]["domain"] == 1)
   68 
   69     m = d["top_blocked_domains"].([]map[string]uint64)
   70     assert.True(t, m[0]["domain"] == 1)
   71 
   72     m = d["top_clients"].([]map[string]uint64)
   73     assert.True(t, m[0]["127.0.0.1"] == 2)
   74 
   75     assert.True(t, d["num_dns_queries"].(uint64) == 2)
   76     assert.True(t, d["num_blocked_filtering"].(uint64) == 1)
   77     assert.True(t, d["num_replaced_safebrowsing"].(uint64) == 0)
   78     assert.True(t, d["num_replaced_safesearch"].(uint64) == 0)
   79     assert.True(t, d["num_replaced_parental"].(uint64) == 0)
   80     assert.True(t, d["avg_processing_time"].(float64) == 0.123456)
   81 
   82     topClients := s.GetTopClientsIP(2)
   83     assert.True(t, topClients[0] == "127.0.0.1")
   84 
   85     s.clear()
   86     s.Close()
   87     os.Remove(conf.Filename)
   88 }
   89 
   90 func TestLargeNumbers(t *testing.T) {
   91     var hour int32 = 1
   92     newID := func() uint32 {
   93         // use "atomic" to make Go race detector happy
   94         return uint32(atomic.LoadInt32(&hour))
   95     }
   96 
   97     // log.SetLevel(log.DEBUG)
   98     conf := Config{
   99         Filename:  "./stats.db",
  100         LimitDays: 1,
  101         UnitID:    newID,
  102     }
  103     os.Remove(conf.Filename)
  104     s, _ := createObject(conf)
  105     e := Entry{}
  106 
  107     n := 1000 // number of distinct clients and domains every hour
  108     for h := 0; h != 12; h++ {
  109         if h != 0 {
  110             atomic.AddInt32(&hour, 1)
  111         }
  112         for i := 0; i != n; i++ {
  113             e.Domain = fmt.Sprintf("domain%d", i)
  114             e.Client = net.ParseIP("127.0.0.1")
  115             e.Client[2] = byte((i & 0xff00) >> 8)
  116             e.Client[3] = byte(i & 0xff)
  117             e.Result = RNotFiltered
  118             e.Time = 123456
  119             s.Update(e)
  120         }
  121     }
  122 
  123     d := s.getData()
  124     assert.True(t, d["num_dns_queries"].(uint64) == uint64(int(hour)*n))
  125 
  126     s.Close()
  127     os.Remove(conf.Filename)
  128 }
  129 
  130 // this code is a chunk copied from getData() that generates aggregate data per day
  131 func aggregateDataPerDay(firstID uint32) int {
  132     firstDayID := (firstID + 24 - 1) / 24 * 24 // align_ceil(24)
  133     a := []uint64{}
  134     var sum uint64
  135     id := firstDayID
  136     nextDayID := firstDayID + 24
  137     for i := firstDayID - firstID; int(i) != 720; i++ {
  138         sum++
  139         if id == nextDayID {
  140             a = append(a, sum)
  141             sum = 0
  142             nextDayID += 24
  143         }
  144         id++
  145     }
  146     if id <= nextDayID {
  147         a = append(a, sum)
  148     }
  149     return len(a)
  150 }
  151 
  152 func TestAggregateDataPerTimeUnit(t *testing.T) {
  153     for i := 0; i != 25; i++ {
  154         alen := aggregateDataPerDay(uint32(i))
  155         assert.True(t, alen == 30, "i=%d", i)
  156     }
  157 }