github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ttlcache/bench/bench_test.go (about)

     1  package bench
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	ttlcache "github.com/bingoohuang/gg/pkg/ttlcache"
     9  )
    10  
    11  func BenchmarkCacheSetWithoutTTL(b *testing.B) {
    12  	cache := ttlcache.New[string, string]()
    13  
    14  	for n := 0; n < b.N; n++ {
    15  		cache.Set(fmt.Sprint(n%1000000), "value", ttlcache.NoTTL)
    16  	}
    17  }
    18  
    19  func BenchmarkCacheSetWithGlobalTTL(b *testing.B) {
    20  	cache := ttlcache.New[string, string](
    21  		ttlcache.WithTTL[string, string](50 * time.Millisecond),
    22  	)
    23  
    24  	for n := 0; n < b.N; n++ {
    25  		cache.Set(fmt.Sprint(n%1000000), "value", ttlcache.DefaultTTL)
    26  	}
    27  }