stario/io_benchmark_test.go

82 lines
1.9 KiB
Go
Raw Permalink Normal View History

package stario
import (
"strings"
"testing"
)
func benchmarkRawRedrawAppend(b *testing.B, current []rune, next rune, mask string) {
b.Run("legacy", func(b *testing.B) {
ioBuf := make([]rune, len(current)+1)
copy(ioBuf, current)
ioBuf[len(current)] = next
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
echo := renderRawEcho(ioBuf, mask)
_ = stringDisplayWidth(echo)
}
})
b.Run("fast", func(b *testing.B) {
maskWidth := stringDisplayWidth(mask)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, ok := rawEchoRenderUnit(next, mask, maskWidth)
if !ok {
b.Fatal("fast path rejected append rune")
}
}
})
}
func benchmarkRawRedrawBackspace(b *testing.B, current []rune, mask string) {
if len(current) == 0 {
b.Fatal("backspace benchmark requires non-empty input")
}
last := current[len(current)-1]
trimmed := current[:len(current)-1]
b.Run("legacy", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
echo := renderRawEcho(trimmed, mask)
_ = stringDisplayWidth(echo)
}
})
b.Run("fast", func(b *testing.B) {
maskWidth := stringDisplayWidth(mask)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, ok := rawEchoRenderUnit(last, mask, maskWidth)
if !ok {
b.Fatal("fast path rejected backspace rune")
}
}
})
}
func BenchmarkRawRedrawAppendPlainLong(b *testing.B) {
current := []rune(strings.Repeat("a", 4096))
benchmarkRawRedrawAppend(b, current, 'b', "")
}
func BenchmarkRawRedrawAppendMaskLong(b *testing.B) {
current := []rune(strings.Repeat("a", 4096))
benchmarkRawRedrawAppend(b, current, 'b', "[]")
}
func BenchmarkRawRedrawBackspacePlainLong(b *testing.B) {
current := []rune(strings.Repeat("a", 4096))
benchmarkRawRedrawBackspace(b, current, "")
}
func BenchmarkRawRedrawBackspaceMaskLong(b *testing.B) {
current := []rune(strings.Repeat("a", 4096))
benchmarkRawRedrawBackspace(b, current, "[]")
}