From f8d14d879fc22b1ae6e3a58f2cb96841becda440 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sat, 9 Apr 2022 10:02:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9F=BA=E5=87=86=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/ttlcache/cache_test.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/ttlcache/cache_test.go b/internal/ttlcache/cache_test.go index df01e24..ca2c999 100644 --- a/internal/ttlcache/cache_test.go +++ b/internal/ttlcache/cache_test.go @@ -138,12 +138,12 @@ func TestCache_GC(t *testing.T) { func TestCache_GC2(t *testing.T) { runtime.GOMAXPROCS(1) - cache1 := NewCache(NewPiecesOption(32)) + var cache1 = NewCache(NewPiecesOption(32)) for i := 0; i < 1_000_000; i++ { cache1.Write(strconv.Itoa(i), i, time.Now().Unix()+int64(rands.Int(0, 10))) } - cache2 := NewCache(NewPiecesOption(5)) + var cache2 = NewCache(NewPiecesOption(5)) for i := 0; i < 1_000_000; i++ { cache2.Write(strconv.Itoa(i), i, time.Now().Unix()+int64(rands.Int(0, 10))) } @@ -153,3 +153,21 @@ func TestCache_GC2(t *testing.T) { time.Sleep(1 * time.Second) } } + +func BenchmarkNewCache(b *testing.B) { + runtime.GOMAXPROCS(1) + + var cache = NewCache(NewPiecesOption(128)) + for i := 0; i < 2_000_000; i++ { + cache.Write(strconv.Itoa(i), i, time.Now().Unix()+int64(rands.Int(10, 100))) + } + b.Log("start reading ...") + + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + cache.Read(strconv.Itoa(rands.Int(0, 999999))) + } + }) +}