github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/table_counter_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package kvstore_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/utils/kvstore"
     7  	"runtime"
     8  	"testing"
     9  )
    10  
    11  func TestCounterTable_Increase(t *testing.T) {
    12  	store, err := kvstore.OpenStore("test")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	defer func() {
    17  		_ = store.Close()
    18  	}()
    19  
    20  	db, err := store.NewDB("TEST_DB")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	table, err := kvstore.NewCounterTable[uint64]("users_counter")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	db.AddTable(table)
    31  
    32  	count, err := table.Increase("counter", 1)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	t.Log(count)
    37  }
    38  
    39  func BenchmarkCounterTable_Increase(b *testing.B) {
    40  	runtime.GOMAXPROCS(1)
    41  
    42  	store, err := kvstore.OpenStore("test")
    43  	if err != nil {
    44  		b.Fatal(err)
    45  	}
    46  	defer func() {
    47  		_ = store.Close()
    48  	}()
    49  
    50  	db, err := store.NewDB("TEST_DB")
    51  	if err != nil {
    52  		b.Fatal(err)
    53  	}
    54  
    55  	table, err := kvstore.NewCounterTable[uint64]("users_counter")
    56  	if err != nil {
    57  		b.Fatal(err)
    58  	}
    59  
    60  	db.AddTable(table)
    61  
    62  	defer func() {
    63  		count, incrErr := table.Increase("counter", 1)
    64  		if incrErr != nil {
    65  			b.Fatal(incrErr)
    66  		}
    67  		b.Log(count)
    68  	}()
    69  
    70  	b.ResetTimer()
    71  
    72  	b.RunParallel(func(pb *testing.PB) {
    73  		for pb.Next() {
    74  			_, incrErr := table.Increase("counter", 1)
    75  			if incrErr != nil {
    76  				b.Fatal(incrErr)
    77  			}
    78  		}
    79  	})
    80  }