gitee.com/woood2/luca@v1.0.4/internal/service/app_test.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"gitee.com/woood2/luca/internal/cache"
     6  	"gitee.com/woood2/luca/internal/errcode"
     7  	"gitee.com/woood2/luca/internal/util"
     8  	"log"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestCacheHit(t *testing.T) {
    14  	util.SkipCI(t)
    15  	a, _ := appSvc.GetByKeyFromCache(context.Background(), cache.Redis(), "weibo2")
    16  	b, _ := appSvc.GetByKeyFromCache(context.Background(), cache.Redis(), "weibo2")
    17  	if a.Name != b.Name || a.Name != "新浪微博2" {
    18  		t.Errorf("expected 新浪微博2, actual: %v, %v", a.Name, b.Name)
    19  	}
    20  	if a, e := appSvc.GetByKeyFromCache(context.Background(), cache.Redis(), "air"); a != nil || e != errcode.AppNotExists {
    21  		t.Errorf("expected a=nil, e=output.AppNotExists; actual a=%v, e=%v", a, e)
    22  	}
    23  	if a, e := appSvc.GetByKeyFromCache(context.Background(), cache.Redis(), "air"); a != nil || e != errcode.AppNotExists {
    24  		t.Errorf("expected a=nil, e=output.AppNotExists; actual a=%v, e=%v", a, e)
    25  	}
    26  }
    27  
    28  //func TestTwins(t *testing.T) {
    29  //	if err:=appSvc.Twins(context.Background());err!=nil{
    30  //		log.Println(err.Error())
    31  //	}
    32  //}
    33  
    34  func BenchmarkGetAppByKey(b *testing.B) {
    35  	b.SetParallelism(1)
    36  	b.RunParallel(func(pb *testing.PB) {
    37  		for pb.Next() {
    38  			appSvc.GetByKey(context.Background(), "weibo2")
    39  		}
    40  	})
    41  }
    42  
    43  func BenchmarkGetAppByKeyFromMem(b *testing.B) {
    44  	for i := 0; i < 10; i++ {
    45  		go func() {
    46  			app, _ := appSvc.GetByKeyFromCache(context.Background(), cache.Local(), "weibo2")
    47  			log.Println(app.Name)
    48  		}()
    49  		if i > 5 {
    50  			time.Sleep(10 * time.Millisecond)
    51  		}
    52  	}
    53  	time.Sleep(10 * time.Second)
    54  }
    55  
    56  func BenchmarkGetAppByKeyFromMemSafely(b *testing.B) {
    57  	for i := 0; i < 10; i++ {
    58  		go func() {
    59  			app, _ := appSvc.GetByKeyFromCacheSafely(context.Background(), cache.Local(), "weibo2")
    60  			log.Println(app.Name)
    61  		}()
    62  	}
    63  	time.Sleep(10 * time.Second)
    64  }
    65  
    66  func BenchmarkGetAppByKeyFromRedis(b *testing.B) {
    67  	for i := 0; i < 10; i++ {
    68  		go func() {
    69  			app, _ := appSvc.GetByKeyFromCache(context.Background(), cache.Redis(), "weibo2")
    70  			log.Println(app.Name)
    71  		}()
    72  		if i > 5 {
    73  			time.Sleep(10 * time.Millisecond)
    74  		}
    75  	}
    76  	time.Sleep(10 * time.Second)
    77  }
    78  
    79  func BenchmarkGetAppByKeyFromRedisSafely(b *testing.B) {
    80  	for i := 0; i < 10; i++ {
    81  		go func() {
    82  			app, _ := appSvc.GetByKeyFromCacheSafely(context.Background(), cache.Redis(), "weibo2")
    83  			log.Println(app.Name)
    84  		}()
    85  	}
    86  	time.Sleep(10 * time.Second)
    87  }