github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/cache/store/memcache_bench_test.go (about)

     1  package store
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/bradfitz/gomemcache/memcache"
    10  )
    11  
    12  func BenchmarkMemcacheSet(b *testing.B) {
    13  	store := NewMemcache(
    14  		memcache.New("127.0.0.1:11211"),
    15  		&Options{
    16  			Expiration: 100 * time.Second,
    17  		},
    18  	)
    19  
    20  	for k := 0.; k <= 10; k++ {
    21  		n := int(math.Pow(2, k))
    22  		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
    23  			for i := 0; i < b.N*n; i++ {
    24  				key := fmt.Sprintf("test-%d", n)
    25  				value := []byte(fmt.Sprintf("value-%d", n))
    26  
    27  				_ = store.Set(key, value, &Options{
    28  					Tags: []string{fmt.Sprintf("tag-%d", n)},
    29  				})
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func BenchmarkMemcacheGet(b *testing.B) {
    36  	store := NewMemcache(
    37  		memcache.New("127.0.0.1:11211"),
    38  		&Options{
    39  			Expiration: 100 * time.Second,
    40  		},
    41  	)
    42  
    43  	key := "test"
    44  	value := []byte("value")
    45  
    46  	_ = store.Set(key, value, nil)
    47  
    48  	for k := 0.; k <= 10; k++ {
    49  		n := int(math.Pow(2, k))
    50  		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
    51  			for i := 0; i < b.N*n; i++ {
    52  				_, _ = store.Get(key)
    53  			}
    54  		})
    55  	}
    56  }