1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
package item
import (
"fmt"
"path/filepath"
"strings"
"testing"
"adammathes.com/neko/config"
"adammathes.com/neko/models"
)
func setupBenchDB(b *testing.B) {
b.Helper()
config.Config.DBFile = filepath.Join(b.TempDir(), "bench.db")
models.InitDB()
b.Cleanup(func() {
if models.DB != nil {
models.DB.Close()
}
})
}
func createBenchFeed(b *testing.B) int64 {
b.Helper()
res, err := models.DB.Exec("INSERT INTO feed(url, title, category) VALUES(?, ?, ?)",
"https://example.com/feed", "Bench Feed", "tech")
if err != nil {
b.Fatal(err)
}
id, _ := res.LastInsertId()
return id
}
func seedBenchItems(b *testing.B, feedID int64, count int) {
b.Helper()
for i := 0; i < count; i++ {
_, err := models.DB.Exec(
`INSERT INTO item(title, url, description, publish_date, feed_id, read_state, starred)
VALUES(?, ?, ?, datetime('now'), ?, 0, 0)`,
fmt.Sprintf("Bench Item %d", i),
fmt.Sprintf("https://example.com/item/%d", i),
fmt.Sprintf("<p>Description for item %d with <b>bold</b> and <a href='https://example.com'>link</a></p>", i),
feedID,
)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkItemCreate(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
item := &Item{
Title: fmt.Sprintf("Item %d", i),
Url: fmt.Sprintf("https://example.com/bench/%d", i),
Description: "<p>Benchmark item description</p>",
PublishDate: "2024-01-01 00:00:00",
FeedId: feedID,
}
_ = item.Create()
}
}
func BenchmarkItemCreateBatch100(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 100; j++ {
item := &Item{
Title: fmt.Sprintf("Batch %d Item %d", i, j),
Url: fmt.Sprintf("https://example.com/batch/%d/%d", i, j),
Description: "<p>Batch item description</p>",
PublishDate: "2024-01-01 00:00:00",
FeedId: feedID,
}
_ = item.Create()
}
}
}
func BenchmarkFilter_Empty(b *testing.B) {
setupBenchDB(b)
_ = createBenchFeed(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "")
}
}
func BenchmarkFilter_15Items(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
seedBenchItems(b, feedID, 15)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "")
}
}
func BenchmarkFilter_WithFTS(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
seedBenchItems(b, feedID, 50)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "Bench")
}
}
func BenchmarkFilter_WithImageProxy(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
// Seed items with image-heavy descriptions
for i := 0; i < 15; i++ {
_, err := models.DB.Exec(
`INSERT INTO item(title, url, description, publish_date, feed_id, read_state, starred)
VALUES(?, ?, ?, datetime('now'), ?, 0, 0)`,
fmt.Sprintf("Image Item %d", i),
fmt.Sprintf("https://example.com/img/%d", i),
`<p>Text with images <img src="https://example.com/a.jpg" alt="a"> and <img src="https://example.com/b.png" alt="b"></p>`,
feedID,
)
if err != nil {
b.Fatal(err)
}
}
config.Config.ProxyImages = true
b.Cleanup(func() { config.Config.ProxyImages = false })
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "")
}
}
func BenchmarkFilterPolicy(b *testing.B) {
html := `<p>Hello <b>world</b> with <a href="https://example.com">link</a> and <img src="https://example.com/img.jpg" alt="test"> and <script>alert('xss')</script></p>`
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := filterPolicy()
_ = p.Sanitize(html)
}
}
func BenchmarkRewriteImages(b *testing.B) {
html := `<p>Text <img src="https://example.com/1.jpg" alt="1"> more text
<img src="https://example.com/2.png" alt="2">
<img src="https://example.com/3.gif" alt="3">
<img src="https://example.com/4.webp" alt="4" srcset="https://example.com/4-2x.webp 2x, https://example.com/4-3x.webp 3x">
<img src="https://example.com/5.jpg" alt="5"></p>`
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = rewriteImages(html)
}
}
func BenchmarkItemSave(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
item := &Item{
Title: "Save Bench Item",
Url: "https://example.com/save-bench",
Description: "<p>Item to update</p>",
PublishDate: "2024-01-01 00:00:00",
FeedId: feedID,
}
if err := item.Create(); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
item.ReadState = !item.ReadState
item.Save()
}
}
func BenchmarkFilter_LargeDataset(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
// Bulk insert 500 items for a realistic dataset
var sb strings.Builder
for i := 0; i < 500; i++ {
if i > 0 {
sb.WriteString(",")
}
fmt.Fprintf(&sb,
"('Item %d', 'https://example.com/large/%d', '<p>Description %d</p>', datetime('now'), %d, 0, 0)",
i, i, i, feedID,
)
}
_, err := models.DB.Exec(
"INSERT INTO item(title, url, description, publish_date, feed_id, read_state, starred) VALUES " + sb.String(),
)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "")
}
}
// realWorldFullContent simulates a realistic scraped article (~10KB of HTML).
const realWorldFullContent = `<article><h1>Sample Article</h1>` +
`<p>This is a realistic full-text article with several paragraphs. ` +
`It contains <b>bold text</b>, <i>italic text</i>, and <a href="https://example.com">links</a>. ` +
`Real-world scraped content is typically several kilobytes of HTML.</p>` +
`<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ` +
`Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ` +
`Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ` +
`Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>` +
`<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, ` +
`totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. ` +
`Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos ` +
`qui ratione voluptatem sequi nesciunt.</p>` +
`<img src="https://example.com/img1.jpg" alt="Figure 1"><img src="https://example.com/img2.jpg" alt="Figure 2">` +
`<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque ` +
`corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa ` +
`qui officia deserunt mollitia animi, id est laborum et dolorum fuga.</p></article>`
// seedBenchItemsWithContent inserts items with full_content populated (realistic scraped articles).
func seedBenchItemsWithContent(b *testing.B, feedID int64, count int) {
b.Helper()
for i := 0; i < count; i++ {
_, err := models.DB.Exec(
`INSERT INTO item(title, url, description, publish_date, feed_id, read_state, starred, full_content)
VALUES(?, ?, ?, datetime('now'), ?, 0, 0, ?)`,
fmt.Sprintf("Full Content Item %d", i),
fmt.Sprintf("https://example.com/full/%d", i),
fmt.Sprintf("<p>Summary for item %d</p>", i),
feedID,
realWorldFullContent,
)
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFilter_15Items_WithFullContent measures Filter when items have full_content
// but it is excluded from list responses (the default). Compares to BenchmarkFilter_15Items.
func BenchmarkFilter_15Items_WithFullContent(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
seedBenchItemsWithContent(b, feedID, 15)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "")
}
}
// BenchmarkFilter_15Items_IncludeFullContent measures Filter when full_content IS included
// (includeContent=true). Compares to BenchmarkFilter_15Items_WithFullContent to show
// the savings from excluding full_content in list views.
func BenchmarkFilter_15Items_IncludeFullContent(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
seedBenchItemsWithContent(b, feedID, 15)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "", true)
}
}
// BenchmarkFilter_LargeDataset_WithFullContent measures Filter with 500 items that
// have full_content, showing real-world memory allocation for list views.
func BenchmarkFilter_LargeDataset_WithFullContent(b *testing.B) {
setupBenchDB(b)
feedID := createBenchFeed(b)
seedBenchItemsWithContent(b, feedID, 500)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = Filter(0, nil, "", false, false, 0, "")
}
}
|