package bench
import (
"fmt"
"testing"
tea "github.com/charmbracelet/bubbletea"
"mercemay.top/httptap/internal/parser"
"mercemay.top/httptap/internal/tui"
"mercemay.top/httptap/internal/tui/theme"
"mercemay.top/httptap/internal/tui/view/list"
"mercemay.top/httptap/internal/tui/view/request"
)
func BenchmarkAppAppend(b *testing.B) {
a := tui.New()
a, _ = fakeResize(a, 120, 40)
msg := parser.Message{StartLine: "GET / HTTP/1.1"}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
a, _ = a.Update(tui.MessageReceivedMsg{M: msg})
}
}
func BenchmarkListRenderRows(b *testing.B) {
m := list.New(theme.Default())
m.SetSize(100, 30)
for i := 0; i < 200; i++ {
m.Append(parser.Message{
StartLine: fmt.Sprintf("GET /item/%d HTTP/1.1", i),
})
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = m.View()
}
}
func BenchmarkRenderHeaders(b *testing.B) {
pal := theme.Default()
var h [][2]string
for i := 0; i < 32; i++ {
h = append(h, [2]string{fmt.Sprintf("X-Bench-%d", i), "value"})
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = request.RenderHeaders(pal, h)
}
}
func fakeResize(a tui.App, w, h int) (tui.App, tea.Cmd) {
next, cmd := a.Update(tea.WindowSizeMsg{Width: w, Height: h})
app, ok := next.(tui.App)
if !ok {
return a, cmd
}
return app, cmd
}