add cache stats to debug page

This commit is contained in:
Sky Johnson 2025-05-05 16:44:24 -05:00
parent 9ea06eb1b4
commit 27ed139274
3 changed files with 40 additions and 12 deletions

View File

@ -218,14 +218,11 @@ func (s *Server) handleDebugStats(ctx *fasthttp.RequestCtx) {
stats := utils.CollectSystemStats(s.config) stats := utils.CollectSystemStats(s.config)
routeCount, bytecodeBytes := s.luaRouter.GetRouteStats() routeCount, bytecodeBytes := s.luaRouter.GetRouteStats()
//stateCount := s.luaRunner.GetStateCount()
//activeStates := s.luaRunner.GetActiveStateCount()
stats.Components = utils.ComponentStats{ stats.Components = utils.ComponentStats{
RouteCount: routeCount, RouteCount: routeCount,
BytecodeBytes: bytecodeBytes, BytecodeBytes: bytecodeBytes,
//StatesCount: stateCount, SessionStats: sessions.GlobalSessionManager.GetCacheStats(),
//ActiveStates: activeStates,
} }
ctx.SetContentType("text/html; charset=utf-8") ctx.SetContentType("text/html; charset=utf-8")

View File

@ -246,3 +246,22 @@ func (sm *SessionManager) CookieOptions() map[string]any {
// GlobalSessionManager is the default session manager instance // GlobalSessionManager is the default session manager instance
var GlobalSessionManager = NewSessionManager(DefaultMaxSessions) var GlobalSessionManager = NewSessionManager(DefaultMaxSessions)
// GetCacheStats returns statistics about the session cache
func (sm *SessionManager) GetCacheStats() map[string]uint64 {
if sm == nil || sm.cache == nil {
return map[string]uint64{}
}
var stats fastcache.Stats
sm.cache.UpdateStats(&stats)
return map[string]uint64{
"entries": stats.EntriesCount,
"bytes": stats.BytesSize,
"max_bytes": stats.MaxBytesSize,
"gets": stats.GetCalls,
"sets": stats.SetCalls,
"misses": stats.Misses,
}
}

View File

@ -13,9 +13,10 @@ import (
// ComponentStats holds stats from various system components // ComponentStats holds stats from various system components
type ComponentStats struct { type ComponentStats struct {
RouteCount int // Number of routes RouteCount int // Number of routes
BytecodeBytes int64 // Total size of bytecode in bytes BytecodeBytes int64 // Total size of bytecode in bytes
ModuleCount int // Number of loaded modules ModuleCount int // Number of loaded modules
SessionStats map[string]uint64 // Session cache statistics
} }
// SystemStats represents system statistics for debugging // SystemStats represents system statistics for debugging
@ -34,14 +35,12 @@ func CollectSystemStats(cfg *config.Config) SystemStats {
var stats SystemStats var stats SystemStats
var mem runtime.MemStats var mem runtime.MemStats
// Collect basic system info
stats.Timestamp = time.Now() stats.Timestamp = time.Now()
stats.GoVersion = runtime.Version() stats.GoVersion = runtime.Version()
stats.GoRoutines = runtime.NumGoroutine() stats.GoRoutines = runtime.NumGoroutine()
stats.Version = metadata.Version stats.Version = metadata.Version
stats.Config = cfg stats.Config = cfg
// Collect memory stats
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
stats.Memory = mem stats.Memory = mem
@ -121,6 +120,20 @@ table tr:nth-child(even), tbody tr:nth-child(even) { background-color: rgba(0, 0
</div> </div>
</div> </div>
<div class="section">
<h2>Sessions</h2>
<div class="card">
<table>
<tr><th>Active Sessions</th><td>{{index .Components.SessionStats "entries"}}</td></tr>
<tr><th>Cache Size</th><td>{{ByteCount (index .Components.SessionStats "bytes")}}</td></tr>
<tr><th>Max Cache Size</th><td>{{ByteCount (index .Components.SessionStats "max_bytes")}}</td></tr>
<tr><th>Cache Gets</th><td>{{index .Components.SessionStats "gets"}}</td></tr>
<tr><th>Cache Sets</th><td>{{index .Components.SessionStats "sets"}}</td></tr>
<tr><th>Cache Misses</th><td>{{index .Components.SessionStats "misses"}}</td></tr>
</table>
</div>
</div>
<div class="section"> <div class="section">
<h2>LuaRunner</h2> <h2>LuaRunner</h2>
<div class="card"> <div class="card">
@ -161,10 +174,9 @@ table tr:nth-child(even), tbody tr:nth-child(even) { background-color: rgba(0, 0
// Create a template function map // Create a template function map
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"ByteCount": func(b interface{}) string { "ByteCount": func(b any) string {
var bytes uint64 var bytes uint64
// Convert the value to uint64
switch v := b.(type) { switch v := b.(type) {
case uint64: case uint64:
bytes = v bytes = v
@ -173,7 +185,7 @@ table tr:nth-child(even), tbody tr:nth-child(even) { background-color: rgba(0, 0
case int: case int:
bytes = uint64(v) bytes = uint64(v)
default: default:
return "Unknown" return fmt.Sprintf("%T: %v", b, b)
} }
const unit = 1024 const unit = 1024