navidrome/model/lyrics_benchmark_test.go
Deluan 39dfc54e28 test(lyrics): use real public-domain fixtures for parser benchmarks
Replace synthetic benchmark payloads with 'Auld Lang Syne' (Robert Burns,
1788, public domain) rendered into every supported format (LRC, plain,
enhanced LRC, SRT, TTML, Lyricsfile YAML) so the numbers reflect realistic
content. Same song across formats makes per-format cost comparable.

Baseline (Apple M-series, -benchmem, real fixtures):
  LRC          ~28 us/op   42 KB   147 allocs
  Plain        ~23 us/op   18 KB    22 allocs
  EnhancedLRC  ~37 us/op   51 KB   374 allocs
  SRT          ~52 us/op  139 KB   581 allocs
  TTML        ~119 us/op  276 KB  1227 allocs
  YAML        ~142 us/op  193 KB  1732 allocs
  Sniff(LRC)   ~47 us/op   57 KB   237 allocs
  Sniff(TTML) ~122 us/op  282 KB  1250 allocs
  Sniff(YAML) ~186 us/op  218 KB  1847 allocs

Fixtures in tests/fixtures/lyrics/.
2026-06-19 15:20:20 -04:00

45 lines
2 KiB
Go

package model
import (
"os"
"path/filepath"
"testing"
)
// Benchmark payloads are real public-domain lyrics ("Auld Lang Syne", Robert
// Burns, 1788) rendered into every supported format, so the numbers reflect
// realistic content and sizing. The same song across formats makes per-format
// cost directly comparable. Fixtures live in tests/fixtures/lyrics/.
func loadLyricFixture(b *testing.B, name string) []byte {
b.Helper()
contents, err := os.ReadFile(filepath.Join("..", "tests", "fixtures", "lyrics", name))
if err != nil {
b.Fatal(err)
}
return contents
}
func benchmarkParse(b *testing.B, suffix, fixture string) {
contents := loadLyricFixture(b, fixture)
b.ReportAllocs()
b.SetBytes(int64(len(contents)))
for b.Loop() {
if _, err := ParseLyrics(suffix, "eng", contents); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseLyrics_LRC(b *testing.B) { benchmarkParse(b, ".lrc", "auld-lang-syne.lrc") }
func BenchmarkParseLyrics_Plain(b *testing.B) { benchmarkParse(b, ".txt", "auld-lang-syne.txt") }
func BenchmarkParseLyrics_EnhancedLRC(b *testing.B) { benchmarkParse(b, ".lrc", "auld-lang-syne.elrc") }
func BenchmarkParseLyrics_SRT(b *testing.B) { benchmarkParse(b, ".srt", "auld-lang-syne.srt") }
func BenchmarkParseLyrics_TTML(b *testing.B) { benchmarkParse(b, ".ttml", "auld-lang-syne.ttml") }
func BenchmarkParseLyrics_YAML(b *testing.B) { benchmarkParse(b, ".yaml", "auld-lang-syne.yaml") }
// Content-sniff path (empty suffix) — what embedded tags and plugins hit.
func BenchmarkParseLyrics_SniffTTML(b *testing.B) { benchmarkParse(b, "", "auld-lang-syne.ttml") }
func BenchmarkParseLyrics_SniffSRT(b *testing.B) { benchmarkParse(b, "", "auld-lang-syne.srt") }
func BenchmarkParseLyrics_SniffYAML(b *testing.B) { benchmarkParse(b, "", "auld-lang-syne.yaml") }
func BenchmarkParseLyrics_SniffLRC(b *testing.B) { benchmarkParse(b, "", "auld-lang-syne.lrc") }
func BenchmarkParseLyrics_SniffPlain(b *testing.B) { benchmarkParse(b, "", "auld-lang-syne.txt") }