Skip to content

Commit 117d6d0

Browse files
ernadoclaude
andcommitted
test(bench): add allocation benchmarks for hot paths
Benchmark the pure translation functions on the per-message hot path: entity conversion (both directions), reply-markup conversion, user conversion and file_unique_id derivation, with -benchmem allocation reporting. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 656be12 commit 117d6d0

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

bench_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package botapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gotd/td/fileid"
7+
"github.com/gotd/td/tg"
8+
)
9+
10+
// Benchmarks for the pure translation hot paths exercised on every incoming and
11+
// outgoing message. Run with: go test -bench . -benchmem
12+
13+
func BenchmarkEntitiesToTg(b *testing.B) {
14+
entities := []MessageEntity{
15+
{Type: EntityBold, Offset: 0, Length: 4},
16+
{Type: EntityItalic, Offset: 5, Length: 6},
17+
{Type: EntityCode, Offset: 12, Length: 3},
18+
{Type: EntityTextLink, Offset: 16, Length: 8, URL: "https://example.com"},
19+
{Type: EntityTextMention, Offset: 25, Length: 5, User: &User{ID: 42}},
20+
}
21+
b.ReportAllocs()
22+
for b.Loop() {
23+
_ = entitiesToTg(entities)
24+
}
25+
}
26+
27+
func BenchmarkEntitiesFromTg(b *testing.B) {
28+
entities := []tg.MessageEntityClass{
29+
&tg.MessageEntityBold{Offset: 0, Length: 4},
30+
&tg.MessageEntityItalic{Offset: 5, Length: 6},
31+
&tg.MessageEntityCode{Offset: 12, Length: 3},
32+
&tg.MessageEntityTextURL{Offset: 16, Length: 8, URL: "https://example.com"},
33+
&tg.MessageEntityMentionName{Offset: 25, Length: 5, UserID: 42},
34+
}
35+
b.ReportAllocs()
36+
for b.Loop() {
37+
_ = entitiesFromTg(entities)
38+
}
39+
}
40+
41+
func BenchmarkReplyMarkupToTg(b *testing.B) {
42+
q := "go"
43+
markup := &InlineKeyboardMarkup{InlineKeyboard: [][]InlineKeyboardButton{
44+
{
45+
{Text: "site", URL: "https://example.com"},
46+
{Text: "ok", CallbackData: "ok:1"},
47+
},
48+
{
49+
{Text: "switch", SwitchInlineQuery: &q},
50+
},
51+
}}
52+
b.ReportAllocs()
53+
for b.Loop() {
54+
if _, err := replyMarkupToTg(markup); err != nil {
55+
b.Fatal(err)
56+
}
57+
}
58+
}
59+
60+
func BenchmarkUserFromTgUser(b *testing.B) {
61+
u := &tg.User{
62+
ID: 42,
63+
FirstName: "Ada",
64+
LastName: "Lovelace",
65+
Username: "ada",
66+
Bot: false,
67+
}
68+
b.ReportAllocs()
69+
for b.Loop() {
70+
_ = userFromTgUser(u)
71+
}
72+
}
73+
74+
func BenchmarkFileUniqueID(b *testing.B) {
75+
f := fileid.FileID{Type: fileid.Document, ID: 0x1122334455667788}
76+
b.ReportAllocs()
77+
for b.Loop() {
78+
_ = fileUniqueID(f)
79+
}
80+
}

0 commit comments

Comments
 (0)