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
|
package crawler
import (
"net/http"
"net/http/httptest"
"testing"
"adammathes.com/neko/config"
"adammathes.com/neko/internal/safehttp"
"adammathes.com/neko/models"
"adammathes.com/neko/models/feed"
"github.com/mmcdole/gofeed"
)
const testRSSFeed = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Bench Test Feed</title>
<link>https://example.com</link>
<description>A feed for benchmarking</description>
<item>
<title>Article One</title>
<link>https://example.com/1</link>
<description><p>First article with <b>bold</b> and <a href="https://example.com">link</a></p></description>
<pubDate>Mon, 01 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Two</title>
<link>https://example.com/2</link>
<description><p>Second article with some content</p></description>
<pubDate>Tue, 02 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Three</title>
<link>https://example.com/3</link>
<description><p>Third article</p></description>
<pubDate>Wed, 03 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Four</title>
<link>https://example.com/4</link>
<description><p>Fourth article with <img src="https://example.com/img.jpg"></p></description>
<pubDate>Thu, 04 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Five</title>
<link>https://example.com/5</link>
<description><p>Fifth article</p></description>
<pubDate>Fri, 05 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Six</title>
<link>https://example.com/6</link>
<description><p>Sixth article</p></description>
<pubDate>Sat, 06 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Seven</title>
<link>https://example.com/7</link>
<description><p>Seventh article</p></description>
<pubDate>Sun, 07 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Eight</title>
<link>https://example.com/8</link>
<description><p>Eighth article</p></description>
<pubDate>Mon, 08 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Nine</title>
<link>https://example.com/9</link>
<description><p>Ninth article with longer content to simulate a real feed item that has more text in it</p></description>
<pubDate>Tue, 09 Jan 2024 00:00:00 +0000</pubDate>
</item>
<item>
<title>Article Ten</title>
<link>https://example.com/10</link>
<description><p>Tenth article</p></description>
<pubDate>Wed, 10 Jan 2024 00:00:00 +0000</pubDate>
</item>
</channel>
</rss>`
func BenchmarkParseFeed(b *testing.B) {
fp := gofeed.NewParser()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := fp.ParseString(testRSSFeed)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkCrawlFeedMocked(b *testing.B) {
safehttp.AllowLocal = true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/rss+xml")
w.WriteHeader(200)
w.Write([]byte(testRSSFeed))
}))
defer ts.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Each iteration needs a fresh DB since CrawlFeed inserts items
config.Config.DBFile = ":memory:"
models.InitDB()
f := &feed.Feed{Url: ts.URL, Title: "Bench Feed"}
f.Create()
ch := make(chan string, 1)
CrawlFeed(f, ch)
<-ch
models.DB.Close()
}
}
func BenchmarkGetFeedContent(b *testing.B) {
safehttp.AllowLocal = true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/rss+xml")
w.WriteHeader(200)
w.Write([]byte(testRSSFeed))
}))
defer ts.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
content := GetFeedContent(ts.URL)
if content == "" {
b.Fatal("empty content")
}
}
}
|