hikan.ru/benchmarks/post_description_test.go

42 lines
1.1 KiB
Go
Raw Normal View History

2025-02-02 16:43:55 +03:00
package benchmarks
import (
"html/template"
"strings"
"testing"
)
const (
LOCK_POST_DESCR = "🤖 Пост доступен только для зарегистрированных пользователей. Аккаунты создаются администратором. Превью для приватных постов не предусмотрено..."
SMALL_POST_DESCR = "🛸 Прилетело НЛО и украло описание поста..."
)
func BenchmarkPostDescription(b *testing.B) {
post := GetLargePost()
b.ResetTimer() // reset timer before cycle
b.Run("PostDescription1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
post.PostDescription1()
}
})
}
func (post Post) PostDescription1() template.HTML {
const maxLength = 500
if post.Lock == 1 {
return template.HTML(LOCK_POST_DESCR)
}
if len(post.Body) > maxLength {
spanIndex := strings.Index(string(post.Body), "<span>")
min := min(spanIndex, maxLength)
if min != -1 {
return template.HTML(post.Body[:min] + "...")
} else {
return template.HTML(post.Body[:maxLength] + "...")
}
}
return template.HTML(SMALL_POST_DESCR)
}