2025-11-20 18:05:45 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/juanatsap/cv-site/internal/config"
|
|
|
|
|
"github.com/juanatsap/cv-site/internal/templates"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// BenchmarkHome benchmarks the Home handler
|
|
|
|
|
func BenchmarkHome(b *testing.B) {
|
|
|
|
|
cfg := &config.TemplateConfig{
|
|
|
|
|
Dir: "../../templates",
|
|
|
|
|
PartialsDir: "../../templates/partials",
|
|
|
|
|
HotReload: false, // Disable hot reload for benchmarks
|
|
|
|
|
}
|
|
|
|
|
tmplManager, err := templates.NewManager(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("Failed to create template manager: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 14:27:03 +00:00
|
|
|
handler := NewCVHandler(tmplManager, "localhost:8080", nil)
|
2025-11-20 18:05:45 +00:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?lang=en", nil)
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
handler.Home(w, req)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkCVContent benchmarks the CVContent handler
|
|
|
|
|
func BenchmarkCVContent(b *testing.B) {
|
|
|
|
|
cfg := &config.TemplateConfig{
|
|
|
|
|
Dir: "../../templates",
|
|
|
|
|
PartialsDir: "../../templates/partials",
|
|
|
|
|
HotReload: false,
|
|
|
|
|
}
|
|
|
|
|
tmplManager, err := templates.NewManager(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("Failed to create template manager: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 14:27:03 +00:00
|
|
|
handler := NewCVHandler(tmplManager, "localhost:8080", nil)
|
2025-11-20 18:05:45 +00:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/cv?lang=en", nil)
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
handler.CVContent(w, req)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkToggleLength benchmarks the ToggleLength handler
|
|
|
|
|
func BenchmarkToggleLength(b *testing.B) {
|
|
|
|
|
cfg := &config.TemplateConfig{
|
|
|
|
|
Dir: "../../templates",
|
|
|
|
|
PartialsDir: "../../templates/partials",
|
|
|
|
|
HotReload: false,
|
|
|
|
|
}
|
|
|
|
|
tmplManager, err := templates.NewManager(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("Failed to create template manager: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 14:27:03 +00:00
|
|
|
handler := NewCVHandler(tmplManager, "localhost:8080", nil)
|
2025-11-20 18:05:45 +00:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/toggle-length", nil)
|
|
|
|
|
req.AddCookie(&http.Cookie{Name: "cv-length", Value: "short"})
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
handler.ToggleLength(w, req)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkParsePDFExportRequest benchmarks request parsing
|
|
|
|
|
func BenchmarkParsePDFExportRequest(b *testing.B) {
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/export-pdf?lang=en&length=long&icons=show&version=with_skills", nil)
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, err := ParsePDFExportRequest(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkPrepareTemplateData benchmarks template data preparation
|
|
|
|
|
func BenchmarkPrepareTemplateData(b *testing.B) {
|
|
|
|
|
cfg := &config.TemplateConfig{
|
|
|
|
|
Dir: "../../templates",
|
|
|
|
|
PartialsDir: "../../templates/partials",
|
|
|
|
|
HotReload: false,
|
|
|
|
|
}
|
|
|
|
|
tmplManager, err := templates.NewManager(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("Failed to create template manager: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 14:27:03 +00:00
|
|
|
handler := NewCVHandler(tmplManager, "localhost:8080", nil)
|
2025-11-20 18:05:45 +00:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, err := handler.prepareTemplateData("en")
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkResponseTypes benchmarks response creation
|
|
|
|
|
func BenchmarkSuccessResponse(b *testing.B) {
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"status": "ok",
|
|
|
|
|
"count": 100,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_ = SuccessResponse(data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkNewErrorResponse(b *testing.B) {
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_ = NewErrorResponse("INVALID_INPUT", "Invalid request parameter")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkParallelHome benchmarks Home handler under parallel load
|
|
|
|
|
func BenchmarkParallelHome(b *testing.B) {
|
|
|
|
|
cfg := &config.TemplateConfig{
|
|
|
|
|
Dir: "../../templates",
|
|
|
|
|
PartialsDir: "../../templates/partials",
|
|
|
|
|
HotReload: false,
|
|
|
|
|
}
|
|
|
|
|
tmplManager, err := templates.NewManager(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("Failed to create template manager: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 14:27:03 +00:00
|
|
|
handler := NewCVHandler(tmplManager, "localhost:8080", nil)
|
2025-11-20 18:05:45 +00:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/?lang=en", nil)
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
handler.Home(w, req)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BenchmarkParallelToggleLength benchmarks toggle under parallel load
|
|
|
|
|
func BenchmarkParallelToggleLength(b *testing.B) {
|
|
|
|
|
cfg := &config.TemplateConfig{
|
|
|
|
|
Dir: "../../templates",
|
|
|
|
|
PartialsDir: "../../templates/partials",
|
|
|
|
|
HotReload: false,
|
|
|
|
|
}
|
|
|
|
|
tmplManager, err := templates.NewManager(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatalf("Failed to create template manager: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 14:27:03 +00:00
|
|
|
handler := NewCVHandler(tmplManager, "localhost:8080", nil)
|
2025-11-20 18:05:45 +00:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/toggle-length", nil)
|
|
|
|
|
req.AddCookie(&http.Cookie{Name: "cv-length", Value: "short"})
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
handler.ToggleLength(w, req)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|