github.com/TeaOSLab/EdgeNode@v1.3.8/internal/metrics/task_kv_test.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package metrics_test
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
     8  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
     9  	"github.com/TeaOSLab/EdgeNode/internal/metrics"
    10  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
    11  	_ "github.com/iwind/TeaGo/bootstrap"
    12  	"github.com/iwind/TeaGo/rands"
    13  	"log"
    14  	"runtime"
    15  	"sync"
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  type testObj struct {
    21  	ip string
    22  }
    23  
    24  func (this *testObj) MetricKey(key string) string {
    25  	return this.ip
    26  }
    27  
    28  func (this *testObj) MetricValue(value string) (int64, bool) {
    29  	return 1, true
    30  }
    31  
    32  func (this *testObj) MetricServerId() int64 {
    33  	return int64(rands.Int(1, 100))
    34  }
    35  
    36  func (this *testObj) MetricCategory() string {
    37  	return "http"
    38  }
    39  
    40  func TestKVTask_Init(t *testing.T) {
    41  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
    42  		Id:         1,
    43  		IsOn:       false,
    44  		Category:   "",
    45  		Period:     0,
    46  		PeriodUnit: "",
    47  		Keys:       nil,
    48  		Value:      "",
    49  	})
    50  	err := task.Init()
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	defer func() {
    55  		_ = task.Stop()
    56  	}()
    57  	t.Log("ok")
    58  }
    59  
    60  func TestKVTask_Add(t *testing.T) {
    61  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
    62  		Id:         1,
    63  		IsOn:       false,
    64  		Category:   "",
    65  		Period:     1,
    66  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
    67  		Keys:       []string{"${remoteAddr}"},
    68  		Value:      "${countRequest}",
    69  	})
    70  	err := task.Init()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	err = task.Start()
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	defer func() {
    79  		_ = task.Stop()
    80  	}()
    81  
    82  	task.Add(&testObj{ip: "127.0.0.2"})
    83  
    84  	if testutils.IsSingleTesting() {
    85  		time.Sleep(1 * time.Second) // waiting for inserting
    86  	}
    87  }
    88  
    89  func TestKVTask_Add_Many(t *testing.T) {
    90  	if !testutils.IsSingleTesting() {
    91  		return
    92  	}
    93  
    94  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
    95  		Id:         1,
    96  		IsOn:       false,
    97  		Category:   "",
    98  		Period:     1,
    99  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   100  		Keys:       []string{"${remoteAddr}"},
   101  		Value:      "${countRequest}",
   102  		Version:    1,
   103  	})
   104  	err := task.Init()
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	err = task.Start()
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	defer func() {
   113  		_ = task.Stop()
   114  	}()
   115  
   116  	for i := 0; i < 4_000_000; i++ {
   117  		task.Add(&testObj{
   118  			ip: fmt.Sprintf("%d.%d.%d.%d", rands.Int(0, 255), rands.Int(0, 255), rands.Int(0, 255), rands.Int(0, 255)),
   119  		})
   120  		if i%10000 == 0 {
   121  			time.Sleep(1 * time.Second)
   122  		}
   123  	}
   124  }
   125  
   126  func TestKVTask_InsertStat(t *testing.T) {
   127  	var item = &serverconfigs.MetricItemConfig{
   128  		Id:         1,
   129  		IsOn:       false,
   130  		Category:   "",
   131  		Period:     1,
   132  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   133  		Keys:       []string{"${remoteAddr}"},
   134  		Value:      "${countRequest}",
   135  		Version:    1,
   136  	}
   137  	var task = metrics.NewKVTask(item)
   138  	err := task.Init()
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	defer func() {
   144  		err = task.Flush()
   145  		if err != nil {
   146  			t.Fatal(err)
   147  		}
   148  	}()
   149  
   150  	err = task.Start()
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	defer func() {
   155  		_ = task.Stop()
   156  	}()
   157  
   158  	{
   159  		err = task.InsertStat(&metrics.Stat{
   160  			ServerId: 1,
   161  			Keys:     []string{"127.0.0.1"},
   162  			Hash:     "",
   163  			Value:    1,
   164  			Time:     item.CurrentTime(),
   165  		})
   166  		if err != nil {
   167  			t.Fatal(err)
   168  		}
   169  	}
   170  
   171  	{
   172  		err = task.InsertStat(&metrics.Stat{
   173  			ServerId: 2,
   174  			Keys:     []string{"127.0.0.2"},
   175  			Hash:     "",
   176  			Value:    3,
   177  			Time:     item.CurrentTime(),
   178  		})
   179  		if err != nil {
   180  			t.Fatal(err)
   181  		}
   182  	}
   183  
   184  	{
   185  		err = task.InsertStat(&metrics.Stat{
   186  			ServerId: 1,
   187  			Keys:     []string{"127.0.0.3"},
   188  			Hash:     "",
   189  			Value:    2,
   190  			Time:     item.CurrentTime(),
   191  		})
   192  		if err != nil {
   193  			t.Fatal(err)
   194  		}
   195  	}
   196  
   197  	TestKVTask_TestInspect(t)
   198  }
   199  
   200  func TestKVTask_CleanExpired(t *testing.T) {
   201  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
   202  		Id:         1,
   203  		IsOn:       false,
   204  		Category:   "",
   205  		Period:     1,
   206  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   207  		Keys:       []string{"${remoteAddr}"},
   208  		Value:      "${countRequest}",
   209  		Version:    1,
   210  	})
   211  	err := task.Init()
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  	err = task.Start()
   216  	if err != nil {
   217  		t.Fatal(err)
   218  	}
   219  	defer func() {
   220  		_ = task.Stop()
   221  	}()
   222  
   223  	err = task.CleanExpired()
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  
   228  	defer func() {
   229  		_ = task.Flush()
   230  	}()
   231  
   232  	t.Log("=== inspect ===")
   233  	task.TestInspect(t)
   234  }
   235  
   236  func TestKVTask_Upload(t *testing.T) {
   237  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
   238  		Id:         31,
   239  		IsOn:       false,
   240  		Category:   "",
   241  		Period:     1,
   242  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   243  		Keys:       []string{"${remoteAddr}"},
   244  		Value:      "${countRequest}",
   245  		Version:    1,
   246  	})
   247  	err := task.Init()
   248  	if err != nil {
   249  		t.Fatal(err)
   250  	}
   251  
   252  	err = task.Start()
   253  	if err != nil {
   254  		t.Fatal(err)
   255  	}
   256  	defer func() {
   257  		_ = task.Stop()
   258  	}()
   259  
   260  	err = task.Upload(0)
   261  	if err != nil {
   262  		t.Fatal(err)
   263  	}
   264  	t.Log("ok")
   265  }
   266  
   267  func TestKVTask_TestInspect(t *testing.T) {
   268  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
   269  		Id:         1,
   270  		IsOn:       false,
   271  		Category:   "",
   272  		Period:     1,
   273  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   274  		Keys:       []string{"${remoteAddr}"},
   275  		Value:      "${countRequest}",
   276  		Version:    1,
   277  	})
   278  	err := task.Init()
   279  	if err != nil {
   280  		t.Fatal(err)
   281  	}
   282  
   283  	task.TestInspect(t)
   284  }
   285  
   286  func TestKVTask_Truncate(t *testing.T) {
   287  	var task = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
   288  		Id:         1,
   289  		IsOn:       false,
   290  		Category:   "",
   291  		Period:     1,
   292  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   293  		Keys:       []string{"${remoteAddr}"},
   294  		Value:      "${countRequest}",
   295  		Version:    1,
   296  	})
   297  	err := task.Init()
   298  	if err != nil {
   299  		t.Fatal(err)
   300  	}
   301  
   302  	if testutils.IsSingleTesting() {
   303  		_ = task.Truncate()
   304  	}
   305  }
   306  
   307  var testingTask metrics.Task
   308  var testingTaskInitOnce = &sync.Once{}
   309  
   310  func initTestingTask() {
   311  	testingTask = metrics.NewKVTask(&serverconfigs.MetricItemConfig{
   312  		Id:         1,
   313  		IsOn:       false,
   314  		Category:   "tcp",
   315  		Period:     1,
   316  		PeriodUnit: serverconfigs.MetricItemPeriodUnitDay,
   317  		Keys:       []string{"${remoteAddr}"},
   318  		Value:      "${countRequest}",
   319  	})
   320  
   321  	err := testingTask.Init()
   322  	if err != nil {
   323  		log.Fatal(err)
   324  	}
   325  
   326  	err = testingTask.Start()
   327  	if err != nil {
   328  		log.Fatal(err)
   329  	}
   330  }
   331  
   332  func BenchmarkKVTask_Add(b *testing.B) {
   333  	runtime.GOMAXPROCS(1)
   334  
   335  	testingTaskInitOnce.Do(func() {
   336  		initTestingTask()
   337  	})
   338  
   339  	b.ResetTimer()
   340  
   341  	b.RunParallel(func(pb *testing.PB) {
   342  		for pb.Next() {
   343  			testingTask.Add(&taskRequest{})
   344  		}
   345  	})
   346  }
   347  
   348  type taskRequest struct {
   349  }
   350  
   351  func (this *taskRequest) MetricKey(key string) string {
   352  	return configutils.ParseVariables(key, func(varName string) (value string) {
   353  		return "1.2.3.4"
   354  	})
   355  }
   356  
   357  func (this *taskRequest) MetricValue(value string) (result int64, ok bool) {
   358  	return 1, true
   359  }
   360  
   361  func (this *taskRequest) MetricServerId() int64 {
   362  	return 1
   363  }
   364  
   365  func (this *taskRequest) MetricCategory() string {
   366  	return "http"
   367  }