benchmarks/ added favourites timeline
[akkoma] / benchmarks / load_testing / fetcher.ex
1 defmodule Pleroma.LoadTesting.Fetcher do
2 use Pleroma.LoadTesting.Helper
3
4 def fetch_user(user) do
5 Benchee.run(%{
6 "By id" => fn -> Repo.get_by(User, id: user.id) end,
7 "By ap_id" => fn -> Repo.get_by(User, ap_id: user.ap_id) end,
8 "By email" => fn -> Repo.get_by(User, email: user.email) end,
9 "By nickname" => fn -> Repo.get_by(User, nickname: user.nickname) end
10 })
11 end
12
13 def query_timelines(user) do
14 home_timeline_params = %{
15 "count" => 20,
16 "with_muted" => true,
17 "type" => ["Create", "Announce"],
18 "blocking_user" => user,
19 "muting_user" => user,
20 "user" => user
21 }
22
23 mastodon_public_timeline_params = %{
24 "count" => 20,
25 "local_only" => true,
26 "only_media" => "false",
27 "type" => ["Create", "Announce"],
28 "with_muted" => "true",
29 "blocking_user" => user,
30 "muting_user" => user
31 }
32
33 mastodon_federated_timeline_params = %{
34 "count" => 20,
35 "only_media" => "false",
36 "type" => ["Create", "Announce"],
37 "with_muted" => "true",
38 "blocking_user" => user,
39 "muting_user" => user
40 }
41
42 following = User.following(user)
43
44 Benchee.run(%{
45 "User home timeline" => fn ->
46 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
47 following,
48 home_timeline_params
49 )
50 end,
51 "User mastodon public timeline" => fn ->
52 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
53 mastodon_public_timeline_params
54 )
55 end,
56 "User mastodon federated public timeline" => fn ->
57 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
58 mastodon_federated_timeline_params
59 )
60 end,
61 "User favourites timeline" => fn ->
62 Pleroma.Web.ActivityPub.ActivityPub.fetch_favourites(user)
63 end
64 })
65
66 home_activities =
67 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
68 following,
69 home_timeline_params
70 )
71
72 public_activities =
73 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(mastodon_public_timeline_params)
74
75 public_federated_activities =
76 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
77 mastodon_federated_timeline_params
78 )
79
80 user_favourites = Pleroma.Web.ActivityPub.ActivityPub.fetch_favourites(user)
81
82 Benchee.run(%{
83 "Rendering home timeline" => fn ->
84 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
85 activities: home_activities,
86 for: user,
87 as: :activity
88 })
89 end,
90 "Rendering public timeline" => fn ->
91 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
92 activities: public_activities,
93 for: user,
94 as: :activity
95 })
96 end,
97 "Rendering public federated timeline" => fn ->
98 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
99 activities: public_federated_activities,
100 for: user,
101 as: :activity
102 })
103 end,
104 "Rendering favorites timeline" => fn ->
105 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
106 activities: user_favourites,
107 for: user,
108 as: :activity})
109 end,
110 })
111 end
112
113 def query_notifications(user) do
114 without_muted_params = %{"count" => "20", "with_muted" => "false"}
115 with_muted_params = %{"count" => "20", "with_muted" => "true"}
116
117 Benchee.run(%{
118 "Notifications without muted" => fn ->
119 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
120 end,
121 "Notifications with muted" => fn ->
122 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
123 end
124 })
125
126 without_muted_notifications =
127 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
128
129 with_muted_notifications =
130 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
131
132 Benchee.run(%{
133 "Render notifications without muted" => fn ->
134 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
135 notifications: without_muted_notifications,
136 for: user
137 })
138 end,
139 "Render notifications with muted" => fn ->
140 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
141 notifications: with_muted_notifications,
142 for: user
143 })
144 end
145 })
146 end
147
148 def query_dms(user) do
149 params = %{
150 "count" => "20",
151 "with_muted" => "true",
152 "type" => "Create",
153 "blocking_user" => user,
154 "user" => user,
155 visibility: "direct"
156 }
157
158 Benchee.run(%{
159 "Direct messages with muted" => fn ->
160 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
161 |> Pleroma.Pagination.fetch_paginated(params)
162 end,
163 "Direct messages without muted" => fn ->
164 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
165 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
166 end
167 })
168
169 dms_with_muted =
170 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
171 |> Pleroma.Pagination.fetch_paginated(params)
172
173 dms_without_muted =
174 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
175 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
176
177 Benchee.run(%{
178 "Rendering dms with muted" => fn ->
179 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
180 activities: dms_with_muted,
181 for: user,
182 as: :activity
183 })
184 end,
185 "Rendering dms without muted" => fn ->
186 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
187 activities: dms_without_muted,
188 for: user,
189 as: :activity
190 })
191 end
192 })
193 end
194
195 def query_long_thread(user, activity) do
196 Benchee.run(%{
197 "Fetch main post" => fn ->
198 Pleroma.Activity.get_by_id_with_object(activity.id)
199 end,
200 "Fetch context of main post" => fn ->
201 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
202 activity.data["context"],
203 %{
204 "blocking_user" => user,
205 "user" => user,
206 "exclude_id" => activity.id
207 }
208 )
209 end
210 })
211
212 activity = Pleroma.Activity.get_by_id_with_object(activity.id)
213
214 context =
215 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
216 activity.data["context"],
217 %{
218 "blocking_user" => user,
219 "user" => user,
220 "exclude_id" => activity.id
221 }
222 )
223
224 Benchee.run(%{
225 "Render status" => fn ->
226 Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{
227 activity: activity,
228 for: user
229 })
230 end,
231 "Render context" => fn ->
232 Pleroma.Web.MastodonAPI.StatusView.render(
233 "index.json",
234 for: user,
235 activities: context,
236 as: :activity
237 )
238 |> Enum.reverse()
239 end
240 })
241 end
242 end