github.com/TeaOSLab/EdgeNode@v1.3.8/internal/stats/dau_manager_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package stats_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/stats"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     8  	"github.com/iwind/TeaGo/rands"
     9  	"runtime"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestDAUManager_AddIP(t *testing.T) {
    15  	var manager = stats.NewDAUManager()
    16  	err := manager.Init()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	manager.AddIP(1, "127.0.0.1")
    22  	manager.AddIP(1, "127.0.0.2")
    23  	manager.AddIP(1, "127.0.0.3")
    24  	manager.AddIP(1, "127.0.0.4")
    25  	manager.AddIP(1, "127.0.0.2")
    26  	manager.AddIP(1, "127.0.0.3")
    27  
    28  	time.Sleep(1 * time.Second)
    29  
    30  	err = manager.Close()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	t.Log("======")
    36  	manager.TestInspect(t)
    37  }
    38  
    39  func TestDAUManager_AddIP_Many(t *testing.T) {
    40  	var manager = stats.NewDAUManager()
    41  	err := manager.Init()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	var before = time.Now()
    47  	defer func() {
    48  		t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
    49  	}()
    50  
    51  	var count = 1
    52  
    53  	if testutils.IsSingleTesting() {
    54  		count = 10_000
    55  	}
    56  
    57  	for i := 0; i < count; i++ {
    58  		manager.AddIP(int64(rands.Int(1, 10)), testutils.RandIP())
    59  	}
    60  }
    61  
    62  func TestDAUManager_CleanStats(t *testing.T) {
    63  	var manager = stats.NewDAUManager()
    64  	err := manager.Init()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	var before = time.Now()
    70  	defer func() {
    71  		t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
    72  	}()
    73  
    74  	defer func() {
    75  		_ = manager.Flush()
    76  	}()
    77  
    78  	err = manager.CleanStats()
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  }
    83  
    84  func TestDAUManager_TestInspect(t *testing.T) {
    85  	var manager = stats.NewDAUManager()
    86  	err := manager.Init()
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	manager.TestInspect(t)
    92  }
    93  
    94  func TestDAUManager_Truncate(t *testing.T) {
    95  	var manager = stats.NewDAUManager()
    96  	err := manager.Init()
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	err = manager.Truncate()
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	err = manager.Flush()
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  }
   111  
   112  func BenchmarkDAUManager_AddIP_Cache(b *testing.B) {
   113  	runtime.GOMAXPROCS(1)
   114  
   115  	var cachedIPs []stats.IPInfo
   116  	var maxIPs = 128
   117  	b.Log("maxIPs:", maxIPs)
   118  	for i := 0; i < maxIPs; i++ {
   119  		cachedIPs = append(cachedIPs, stats.IPInfo{
   120  			IP:       testutils.RandIP(),
   121  			ServerId: 1,
   122  		})
   123  	}
   124  
   125  	b.ResetTimer()
   126  
   127  	for i := 0; i < b.N; i++ {
   128  		var ip = "1.2.3.4"
   129  		for _, cacheIP := range cachedIPs {
   130  			if cacheIP.IP == ip && cacheIP.ServerId == 1 {
   131  				break
   132  			}
   133  		}
   134  	}
   135  }