loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add cache test for admins (#31265)

Add a test to probe the cache similar to the email test func.

![image](https://github.com/go-gitea/gitea/assets/24977596/700e2733-586d-4091-900f-f5f71e6e94bf)

![image](https://github.com/go-gitea/gitea/assets/24977596/2a953802-18fc-4e81-a37d-24ebe1297365)

![image](https://github.com/go-gitea/gitea/assets/24977596/e00d62ad-bb60-41cc-9138-09993daee156)

---------

Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit 363c1235987793dffa5cc851aaae585eb81f091e)

Conflicts:
options/locale/locale_en-US.ini
templates/admin/self_check.tmpl
trivial context conflict

authored by

6543
delvh
silverwind
and committed by
Earl Warren
77da92f4 40cd885c

+93 -2
+31
modules/cache/cache.go
··· 40 40 return err 41 41 } 42 42 43 + const ( 44 + testCacheKey = "DefaultCache.TestKey" 45 + SlowCacheThreshold = 100 * time.Microsecond 46 + ) 47 + 48 + func Test() (time.Duration, error) { 49 + if defaultCache == nil { 50 + return 0, fmt.Errorf("default cache not initialized") 51 + } 52 + 53 + testData := fmt.Sprintf("%x", make([]byte, 500)) 54 + 55 + start := time.Now() 56 + 57 + if err := defaultCache.Delete(testCacheKey); err != nil { 58 + return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err) 59 + } 60 + if err := defaultCache.Put(testCacheKey, testData, 10); err != nil { 61 + return 0, fmt.Errorf("expect cache to store data but got: %w", err) 62 + } 63 + testVal, hit := defaultCache.Get(testCacheKey) 64 + if !hit { 65 + return 0, fmt.Errorf("expect cache hit but got none") 66 + } 67 + if testVal != testData { 68 + return 0, fmt.Errorf("expect cache to return same value as stored but got other") 69 + } 70 + 71 + return time.Since(start), nil 72 + } 73 + 43 74 // GetCache returns the currently configured cache 44 75 func GetCache() mc.Cache { 45 76 return conn
+12
modules/cache/cache_test.go
··· 34 34 assert.Nil(t, con) 35 35 } 36 36 37 + func TestTest(t *testing.T) { 38 + defaultCache = nil 39 + _, err := Test() 40 + assert.Error(t, err) 41 + 42 + createTestCache() 43 + elapsed, err := Test() 44 + assert.NoError(t, err) 45 + // mem cache should take from 300ns up to 1ms on modern hardware ... 46 + assert.Less(t, elapsed, SlowCacheThreshold) 47 + } 48 + 37 49 func TestGetCache(t *testing.T) { 38 50 createTestCache() 39 51
+6
options/locale/locale_en-US.ini
··· 93 93 remove_label_str = Remove item "%s" 94 94 edit = Edit 95 95 view = View 96 + test = Test 96 97 97 98 enabled = Enabled 98 99 disabled = Disabled ··· 3316 3317 config.cache_interval = Cache interval 3317 3318 config.cache_conn = Cache connection 3318 3319 config.cache_item_ttl = Cache item TTL 3320 + 3321 + config.cache_test = Test Cache 3322 + config.cache_test_failed = Failed to probe the cache: %v. 3323 + config.cache_test_slow = Cache test successful, but response is slow: %s. 3324 + config.cache_test_succeeded = Cache test successful, got a response in %s. 3319 3325 3320 3326 config.session_config = Session configuration 3321 3327 config.session_provider = Session provider
+9
routers/web/admin/admin.go
··· 14 14 activities_model "code.gitea.io/gitea/models/activities" 15 15 "code.gitea.io/gitea/models/db" 16 16 "code.gitea.io/gitea/modules/base" 17 + "code.gitea.io/gitea/modules/cache" 17 18 "code.gitea.io/gitea/modules/graceful" 18 19 "code.gitea.io/gitea/modules/log" 19 20 "code.gitea.io/gitea/modules/setting" ··· 211 212 212 213 ctx.Data["DatabaseCheckHasProblems"] = hasProblem 213 214 } 215 + 216 + elapsed, err := cache.Test() 217 + if err != nil { 218 + ctx.Data["CacheError"] = err 219 + } else if elapsed > cache.SlowCacheThreshold { 220 + ctx.Data["CacheSlow"] = fmt.Sprint(elapsed) 221 + } 222 + 214 223 ctx.HTML(http.StatusOK, tplSelfCheck) 215 224 } 216 225
+17
routers/web/admin/config.go
··· 12 12 13 13 system_model "code.gitea.io/gitea/models/system" 14 14 "code.gitea.io/gitea/modules/base" 15 + "code.gitea.io/gitea/modules/cache" 15 16 "code.gitea.io/gitea/modules/git" 16 17 "code.gitea.io/gitea/modules/json" 17 18 "code.gitea.io/gitea/modules/log" ··· 37 38 ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err)) 38 39 } else { 39 40 ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email)) 41 + } 42 + 43 + ctx.Redirect(setting.AppSubURL + "/admin/config") 44 + } 45 + 46 + // TestCache test the cache settings 47 + func TestCache(ctx *context.Context) { 48 + elapsed, err := cache.Test() 49 + if err != nil { 50 + ctx.Flash.Error(ctx.Tr("admin.config.cache_test_failed", err)) 51 + } else { 52 + if elapsed > cache.SlowCacheThreshold { 53 + ctx.Flash.Warning(ctx.Tr("admin.config.cache_test_slow", elapsed)) 54 + } else { 55 + ctx.Flash.Info(ctx.Tr("admin.config.cache_test_succeeded", elapsed)) 56 + } 40 57 } 41 58 42 59 ctx.Redirect(setting.AppSubURL + "/admin/config")
+1
routers/web/web.go
··· 665 665 m.Get("", admin.Config) 666 666 m.Post("", admin.ChangeConfig) 667 667 m.Post("/test_mail", admin.SendTestMail) 668 + m.Post("/test_cache", admin.TestCache) 668 669 m.Get("/settings", admin.ConfigSettings) 669 670 }) 670 671
+10 -2
templates/admin/config.tmpl
··· 233 233 <dt>{{ctx.Locale.Tr "admin.config.mailer_user"}}</dt> 234 234 <dd>{{if .Mailer.User}}{{.Mailer.User}}{{else}}(empty){{end}}</dd> 235 235 <div class="divider"></div> 236 - <dt class="tw-py-1">{{ctx.Locale.Tr "admin.config.send_test_mail"}}</dt> 237 - <dd> 236 + <dt class="tw-py-1 tw-flex tw-items-center">{{ctx.Locale.Tr "admin.config.send_test_mail"}}</dt> 237 + <dd class="tw-py-0"> 238 238 <form class="ui form ignore-dirty" action="{{AppSubUrl}}/admin/config/test_mail" method="post"> 239 239 {{.CsrfTokenHtml}} 240 240 <div class="ui tiny input"> ··· 264 264 <dt>{{ctx.Locale.Tr "admin.config.cache_item_ttl"}}</dt> 265 265 <dd><code>{{.CacheItemTTL}}</code></dd> 266 266 {{end}} 267 + <div class="divider"></div> 268 + <dt class="tw-py-1 tw-flex tw-items-center">{{ctx.Locale.Tr "admin.config.cache_test"}}</dt> 269 + <dd class="tw-py-0"> 270 + <form class="ui form ignore-dirty" action="{{AppSubUrl}}/admin/config/test_cache" method="post"> 271 + {{.CsrfTokenHtml}} 272 + <button class="ui tiny primary button">{{ctx.Locale.Tr "test"}}</button> 273 + </form> 274 + </dd> 267 275 </dl> 268 276 </div> 269 277
+7
templates/admin/self_check.tmpl
··· 28 28 {{else}} 29 29 <div class="tw-p-2">{{ctx.Locale.Tr "admin.self_check.no_problem_found"}}</div> 30 30 {{end}} 31 + 32 + {{if .CacheError}} 33 + <div class="ui red message">{{ctx.Locale.Tr "admin.config.cache_test_failed" .CacheError}}</div> 34 + {{end}} 35 + {{if .CacheSlow}} 36 + <div class="ui warning message">{{ctx.Locale.Tr "admin.config.cache_test_slow" .CacheSlow}}</div> 37 + {{end}} 31 38 </div> 32 39 </div> 33 40