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/.
This commit is contained in:
Deluan 2026-06-19 15:20:20 -04:00
commit 39dfc54e28
7 changed files with 309 additions and 58 deletions

View file

@ -1,59 +1,26 @@
package model
import (
"fmt"
"strings"
"os"
"path/filepath"
"testing"
)
// repeatLines builds a payload of n lines using the given per-line template,
// where %d is the line index, so benchmark inputs are realistically sized.
func repeatLines(n int, format string) string {
var sb strings.Builder
for i := 0; i < n; i++ {
fmt.Fprintf(&sb, format, i)
// 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 sb.String()
return contents
}
const benchLineCount = 200
func benchLRC() []byte {
return []byte(repeatLines(benchLineCount, "[00:%02d.00]lyric line number with some words\n"))
}
func benchPlain() []byte {
return []byte(repeatLines(benchLineCount, "lyric line number %d with some words\n"))
}
func benchEnhancedLRC() []byte {
return []byte(repeatLines(benchLineCount, "[00:%02d.00]<00:00.10>word <00:00.50>by <00:00.90>word\n"))
}
func benchSRT() []byte {
return []byte(repeatLines(benchLineCount, "%d\n00:00:01,000 --> 00:00:02,000\nsubtitle line\n\n"))
}
func benchTTML() []byte {
var sb strings.Builder
sb.WriteString(`<tt xmlns="http://www.w3.org/ns/ttml"><body><div>`)
for i := 0; i < benchLineCount; i++ {
fmt.Fprintf(&sb, `<p begin="00:00:%02d.000" end="00:00:%02d.000">ttml line</p>`, i%60, (i+1)%60)
}
sb.WriteString(`</div></body></tt>`)
return []byte(sb.String())
}
func benchYAML() []byte {
var sb strings.Builder
sb.WriteString("version: \"1.0\"\nmetadata:\n language: eng\nlines:\n")
for i := 0; i < benchLineCount; i++ {
fmt.Fprintf(&sb, " - text: yaml line %d\n start_ms: %d\n", i, i*1000)
}
return []byte(sb.String())
}
func benchmarkParse(b *testing.B, suffix string, contents []byte) {
func benchmarkParse(b *testing.B, suffix, fixture string) {
contents := loadLyricFixture(b, fixture)
b.ReportAllocs()
b.SetBytes(int64(len(contents)))
for b.Loop() {
@ -63,16 +30,16 @@ func benchmarkParse(b *testing.B, suffix string, contents []byte) {
}
}
func BenchmarkParseLyrics_LRC(b *testing.B) { benchmarkParse(b, ".lrc", benchLRC()) }
func BenchmarkParseLyrics_Plain(b *testing.B) { benchmarkParse(b, ".txt", benchPlain()) }
func BenchmarkParseLyrics_EnhancedLRC(b *testing.B) { benchmarkParse(b, ".lrc", benchEnhancedLRC()) }
func BenchmarkParseLyrics_SRT(b *testing.B) { benchmarkParse(b, ".srt", benchSRT()) }
func BenchmarkParseLyrics_TTML(b *testing.B) { benchmarkParse(b, ".ttml", benchTTML()) }
func BenchmarkParseLyrics_YAML(b *testing.B) { benchmarkParse(b, ".yaml", benchYAML()) }
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, "", benchTTML()) }
func BenchmarkParseLyrics_SniffSRT(b *testing.B) { benchmarkParse(b, "", benchSRT()) }
func BenchmarkParseLyrics_SniffYAML(b *testing.B) { benchmarkParse(b, "", benchYAML()) }
func BenchmarkParseLyrics_SniffLRC(b *testing.B) { benchmarkParse(b, "", benchLRC()) }
func BenchmarkParseLyrics_SniffPlain(b *testing.B) { benchmarkParse(b, "", benchPlain()) }
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") }