lolly/internal/handler/fileinfo_cache_test.go
xfy 1128eb644f perf(static): enable FileInfoCache by default with negative caching
Production static file serving now uses FileInfoCache by default
with a 2-second TTL in router.go, dramatically reducing os.Stat
syscalls for missing files and repeated paths.

Changes:
- Add negative cache support to FileInfoCache (caches 'not found' results)
- Introduce statWithCache() helper in StaticHandler for uniform caching
- Make FileInfoCache TTL configurable via SetTTL()
- Default cacheTTL=0 disables caching in NewStaticHandler (tests compat)
- router.go enables fileInfoCache with 2s TTL for all static handlers

Benchmark (repeated 404s):
  No cache:    ~2651 ns/op, 2225 B/op, 15 allocs/op
  With cache:  ~1505 ns/op, 1905 B/op, 12 allocs/op
  Improvement: -43% latency, -14% allocations

This addresses the dominant allocation source in v0.4.0 profile
(os.statNolog at 74.95% of allocations).
2026-06-11 14:05:56 +08:00

110 lines
2.4 KiB
Go

package handler
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestFileInfoCache(t *testing.T) {
// 创建临时文件
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.txt")
if err := os.WriteFile(tmpFile, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
cache := NewFileInfoCache()
t.Run("缓存未命中", func(t *testing.T) {
info, ok := cache.Get(tmpFile)
if ok {
t.Error("未命中的缓存应返回 false")
}
if info != nil {
t.Error("未命中时应返回 nil")
}
})
t.Run("缓存命中", func(t *testing.T) {
// 先获取真实 FileInfo
realInfo, err := os.Stat(tmpFile)
if err != nil {
t.Fatal(err)
}
// 存入缓存
cache.Set(tmpFile, realInfo)
// 从缓存获取
cachedInfo, ok := cache.Get(tmpFile)
if !ok {
t.Error("缓存应命中")
}
if cachedInfo == nil {
t.Fatal("缓存命中时应返回非 nil")
}
if cachedInfo.Name() != realInfo.Name() {
t.Errorf("Name() = %q, want %q", cachedInfo.Name(), realInfo.Name())
}
if cachedInfo.Size() != realInfo.Size() {
t.Errorf("Size() = %d, want %d", cachedInfo.Size(), realInfo.Size())
}
})
}
func TestFileInfoCacheTTL(t *testing.T) {
// 创建临时文件
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.txt")
if err := os.WriteFile(tmpFile, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
cache := NewFileInfoCache()
realInfo, _ := os.Stat(tmpFile)
// 存入缓存
cache.Set(tmpFile, realInfo)
// 立即获取应命中
_, ok := cache.Get(tmpFile)
if !ok {
t.Error("立即获取应命中")
}
// 模拟过期:修改 cachedAt
cache.mu.Lock()
if entry, exists := cache.entries[tmpFile]; exists {
entry.cachedAt = time.Now().Add(-cache.ttl - time.Second)
}
cache.mu.Unlock()
// 过期后应未命中
_, ok = cache.Get(tmpFile)
if ok {
t.Error("过期后应未命中")
}
}
func TestFileInfoCacheLRU(t *testing.T) {
cache := NewFileInfoCache()
// 创建测试文件信息
tmpDir := t.TempDir()
for i := range fileInfoCacheMaxEntries + 10 {
tmpFile := filepath.Join(tmpDir, "test"+string(rune('0'+i%10))+".txt")
os.WriteFile(tmpFile, []byte("hello"), 0o644)
info, _ := os.Stat(tmpFile)
cache.Set(tmpFile, info)
}
cache.mu.RLock()
entries := len(cache.entries)
cache.mu.RUnlock()
if entries > fileInfoCacheMaxEntries {
t.Errorf("Entries = %d, should not exceed %d", entries, fileInfoCacheMaxEntries)
}
}