mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-01 07:21:17 +00:00
* feat(conf): add Dir type with lazy directory creation Introduces the Dir type that wraps a directory path string and defers os.MkdirAll until the first call to Path() or MustPath(), using sync.Once to ensure the creation happens exactly once. Implements fmt.Stringer, encoding.TextMarshaler, and encoding.TextUnmarshaler for config integration. Includes Ginkgo/Gomega tests covering all methods and error paths. * refactor(conf): replace eager dir creation with lazy Dir type Change DataFolder, CacheFolder, Plugins.Folder, and Backup.Path from string to Dir. Remove all os.MkdirAll calls from Load() so directories are created lazily on first Path()/MustPath() call. Artwork folder creation was already handled at point-of-use in image_upload.go. Add SnapshotConfig() to conf package for safe test config save/restore that avoids copying sync.Once inside Dir fields. Fix copy-lock vet warning in nativeapi/config.go by marshalling pointer instead of value. * refactor(conf): migrate tests and db init to lazy Dir type Update all test files to use conf.NewDir() for Dir field assignments. Ensure DataFolder is created lazily when the database is first opened in db.Db(). Remove eager directory creation from conf.Load() tests. * fix(conf): address review findings for Dir type - Use os.ModePerm for DataFolder/CacheFolder (was 0700, should match original behavior). Add NewDirWithPerm for PluginsFolder (0700). - Use Path() instead of MustPath() in db.Prune() to avoid logFatal from background cron job. - Panic on marshal/unmarshal errors in SnapshotConfig (test helper). - Clean up redundant String()/MustPath() calls in plugin manager. - Remove dead code in dir_test.go. Signed-off-by: Deluan <deluan@navidrome.org> * fix(conf): add GoString to Dir for clean config dump output Implement fmt.GoStringer on Dir so pretty.Sprintf shows the path string instead of internal struct fields (sync.Once, perm, err). Also add TODO comment to configtest about removing the indirection. * fix(dir): improve error logging in MustPath method Signed-off-by: Deluan <deluan@navidrome.org> * refactor(tests): remove redundant tests for unwritable DataFolder and CacheFolder Signed-off-by: Deluan <deluan@navidrome.org> * fix(conf): address PR review feedback - Ensure Plugins.Folder always uses 0700, even when user-configured (previously only the derived default got restrictive permissions). - Create LogFile parent directory before opening, so LogFile paths inside a not-yet-created DataFolder work correctly. --------- Signed-off-by: Deluan <deluan@navidrome.org>
190 lines
4.8 KiB
Go
190 lines
4.8 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"image/jpeg"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/tests"
|
|
"github.com/navidrome/navidrome/utils/cache"
|
|
)
|
|
|
|
// setupE2EBenchmark creates an artwork instance with a real album cover image on disk,
|
|
// backed by either a real file cache or disabled cache depending on cacheSize.
|
|
// Note: This benchmarks artwork.Get() directly (not the full HTTP handler), which covers
|
|
// the critical path (source selection, decode, resize, encode, cache). This is a deliberate
|
|
// spec deviation — the full HTTP round-trip benchmark requires significant infrastructure
|
|
// (DB, scanner, fake filesystem) and can be added later if HTTP overhead proves significant.
|
|
//
|
|
// Depends on fakeFolderRepo defined in reader_artist_test.go (same package, compiled together).
|
|
func setupE2EBenchmark(b *testing.B, cacheSize string) (Artwork, model.ArtworkID, func()) {
|
|
b.Helper()
|
|
cleanup := configtest.SetupConfig()
|
|
b.Cleanup(cleanup)
|
|
|
|
tmpDir, err := os.MkdirTemp("", "artwork-bench-*")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
// Create a realistic cover image on disk
|
|
coverPath := filepath.Join(tmpDir, "cover.jpg")
|
|
coverImg := generateGradientImage(1000, 1000)
|
|
f, err := os.Create(coverPath)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
if err := jpeg.Encode(f, coverImg, &jpeg.Options{Quality: 90}); err != nil {
|
|
f.Close()
|
|
b.Fatal(err)
|
|
}
|
|
f.Close()
|
|
|
|
// Configure cache
|
|
conf.Server.ImageCacheSize = cacheSize
|
|
conf.Server.CacheFolder = conf.NewDir(tmpDir)
|
|
conf.Server.CoverArtQuality = 75
|
|
conf.Server.CoverArtPriority = "cover.*"
|
|
|
|
// Set up mock data store with album pointing to our cover.
|
|
// Set UpdatedAt so CoverArtID().LastUpdate is consistent across calls.
|
|
album := model.Album{
|
|
ID: "bench-album-1",
|
|
Name: "Benchmark Album",
|
|
FolderIDs: []string{"f1"},
|
|
UpdatedAt: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
}
|
|
folderRepo := &fakeFolderRepo{
|
|
result: []model.Folder{{
|
|
Path: tmpDir,
|
|
ImageFiles: []string{"cover.jpg"},
|
|
}},
|
|
}
|
|
ds := &tests.MockDataStore{
|
|
MockedTranscoding: &tests.MockTranscodingRepo{},
|
|
MockedFolder: folderRepo,
|
|
}
|
|
ds.Album(context.Background()).(*tests.MockAlbumRepo).SetData(model.Albums{album})
|
|
|
|
artID := album.CoverArtID()
|
|
|
|
imgCache := cache.NewFileCache("BenchImage", cacheSize, "bench-images", 0,
|
|
func(ctx context.Context, arg cache.Item) (io.Reader, error) {
|
|
r, _, err := arg.(artworkReader).Reader(ctx)
|
|
return r, err
|
|
})
|
|
|
|
// Wait for cache init if enabled
|
|
if cacheSize != "0" {
|
|
for !imgCache.Available(context.Background()) && !imgCache.Disabled(context.Background()) {
|
|
runtime.Gosched() // Yield to allow background init goroutine to run
|
|
}
|
|
}
|
|
|
|
ffmpeg := tests.NewMockFFmpeg("fallback content")
|
|
aw := NewArtwork(ds, imgCache, ffmpeg, nil)
|
|
|
|
cleanupAll := func() {
|
|
os.RemoveAll(tmpDir)
|
|
}
|
|
return aw, artID, cleanupAll
|
|
}
|
|
|
|
func BenchmarkArtworkGetE2E(b *testing.B) {
|
|
cacheConfigs := []struct {
|
|
name string
|
|
cacheSize string
|
|
}{
|
|
{"no_cache", "0"},
|
|
{"with_cache", "100MB"},
|
|
}
|
|
sizes := []int{0, 300}
|
|
|
|
for _, cc := range cacheConfigs {
|
|
for _, size := range sizes {
|
|
b.Run(fmt.Sprintf("%s/size_%d", cc.name, size), func(b *testing.B) {
|
|
aw, artID, cleanup := setupE2EBenchmark(b, cc.cacheSize)
|
|
defer cleanup()
|
|
|
|
// Warm the cache on first call if cache is enabled
|
|
if cc.cacheSize != "0" {
|
|
r, _, err := aw.Get(context.Background(), artID, size, size > 0)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
_, _ = io.ReadAll(r)
|
|
r.Close()
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
r, _, err := aw.Get(context.Background(), artID, size, size > 0)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
_, _ = io.ReadAll(r)
|
|
r.Close()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkArtworkGetE2EConcurrent(b *testing.B) {
|
|
cacheConfigs := []struct {
|
|
name string
|
|
cacheSize string
|
|
}{
|
|
{"no_cache", "0"},
|
|
{"with_cache", "100MB"},
|
|
}
|
|
concurrencyLevels := []int{10, 50}
|
|
|
|
for _, cc := range cacheConfigs {
|
|
for _, n := range concurrencyLevels {
|
|
b.Run(fmt.Sprintf("%s/goroutines_%d", cc.name, n), func(b *testing.B) {
|
|
aw, artID, cleanup := setupE2EBenchmark(b, cc.cacheSize)
|
|
defer cleanup()
|
|
|
|
// Warm cache
|
|
if cc.cacheSize != "0" {
|
|
r, _, _ := aw.Get(context.Background(), artID, 300, true)
|
|
if r != nil {
|
|
_, _ = io.ReadAll(r)
|
|
r.Close()
|
|
}
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
for g := 0; g < n; g++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
r, _, err := aw.Get(context.Background(), artID, 300, true)
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
_, _ = io.ReadAll(r)
|
|
r.Close()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|