test(lyrics): add per-format ParseLyrics benchmarks

Baseline measurements (count=2 runs) on M2:

BenchmarkParseLyrics_LRC-8           	    5725	    178796 ns/op	  49.78 MB/s	  427877 B/op	     523 allocs/op
BenchmarkParseLyrics_Plain-8         	    5425	    230854 ns/op	  32.44 MB/s	  102508 B/op	      16 allocs/op
BenchmarkParseLyrics_EnhancedLRC-8   	    1942	    605893 ns/op	  17.66 MB/s	  860678 B/op	    4256 allocs/op
BenchmarkParseLyrics_SRT-8           	    3249	    373991 ns/op	  25.91 MB/s	 1113575 B/op	    4407 allocs/op
BenchmarkParseLyrics_TTML-8          	    1483	    813027 ns/op	  13.86 MB/s	 2198052 B/op	    8665 allocs/op
BenchmarkParseLyrics_YAML-8          	    1700	    678250 ns/op	  13.01 MB/s	 1235096 B/op	    8288 allocs/op
BenchmarkParseLyrics_SniffTTML-8     	    1525	    776482 ns/op	  14.51 MB/s	 2225448 B/op	    8681 allocs/op
BenchmarkParseLyrics_SniffSRT-8      	    2528	    451210 ns/op	  21.48 MB/s	 1157000 B/op	    4422 allocs/op
BenchmarkParseLyrics_SniffYAML-8     	    1333	    827152 ns/op	  10.67 MB/s	 1337195 B/op	    8718 allocs/op
BenchmarkParseLyrics_SniffLRC-8      	    2820	    413038 ns/op	  21.55 MB/s	  588934 B/op	    1812 allocs/op
BenchmarkParseLyrics_SniffPlain-8    	    2968	    409091 ns/op	  18.31 MB/s	  254470 B/op	    1491 allocs/op

Content-sniff path overhead: 1.5–15% depending on format.
This commit is contained in:
Deluan 2026-06-19 15:12:28 -04:00
commit 49f6abb7b3

View file

@ -0,0 +1,78 @@
package model
import (
"fmt"
"strings"
"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)
}
return sb.String()
}
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) {
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", 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()) }
// 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()) }