Merge branch 'develop' into issue/1276
[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 })
62
63 home_activities =
64 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
65 following,
66 home_timeline_params
67 )
68
69 public_activities =
70 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(mastodon_public_timeline_params)
71
72 public_federated_activities =
73 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
74 mastodon_federated_timeline_params
75 )
76
77 Benchee.run(%{
78 "Rendering home timeline" => fn ->
79 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
80 activities: home_activities,
81 for: user,
82 as: :activity
83 })
84 end,
85 "Rendering public timeline" => fn ->
86 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
87 activities: public_activities,
88 for: user,
89 as: :activity
90 })
91 end,
92 "Rendering public federated timeline" => fn ->
93 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
94 activities: public_federated_activities,
95 for: user,
96 as: :activity
97 })
98 end,
99 "Rendering favorites timeline" => fn ->
100 conn = Phoenix.ConnTest.build_conn(:get, "http://localhost:4001/api/v1/favourites", nil)
101 Pleroma.Web.MastodonAPI.StatusController.favourites(
102 %Plug.Conn{conn |
103 assigns: %{user: user},
104 query_params: %{"limit" => "0"},
105 body_params: %{},
106 cookies: %{},
107 params: %{},
108 path_params: %{},
109 private: %{
110 Pleroma.Web.Router => {[], %{}},
111 phoenix_router: Pleroma.Web.Router,
112 phoenix_action: :favourites,
113 phoenix_controller: Pleroma.Web.MastodonAPI.StatusController,
114 phoenix_endpoint: Pleroma.Web.Endpoint,
115 phoenix_format: "json",
116 phoenix_layout: {Pleroma.Web.LayoutView, "app.html"},
117 phoenix_recycled: true,
118
119 phoenix_view: Pleroma.Web.MastodonAPI.StatusView,
120 plug_session: %{"user_id" => user.id},
121 plug_session_fetch: :done,
122 plug_session_info: :write,
123 plug_skip_csrf_protection: true
124 }
125 },
126 %{})
127 end,
128 })
129 end
130
131 def query_notifications(user) do
132 without_muted_params = %{"count" => "20", "with_muted" => "false"}
133 with_muted_params = %{"count" => "20", "with_muted" => "true"}
134
135 Benchee.run(%{
136 "Notifications without muted" => fn ->
137 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
138 end,
139 "Notifications with muted" => fn ->
140 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
141 end
142 })
143
144 without_muted_notifications =
145 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
146
147 with_muted_notifications =
148 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
149
150 Benchee.run(%{
151 "Render notifications without muted" => fn ->
152 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
153 notifications: without_muted_notifications,
154 for: user
155 })
156 end,
157 "Render notifications with muted" => fn ->
158 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
159 notifications: with_muted_notifications,
160 for: user
161 })
162 end
163 })
164 end
165
166 def query_dms(user) do
167 params = %{
168 "count" => "20",
169 "with_muted" => "true",
170 "type" => "Create",
171 "blocking_user" => user,
172 "user" => user,
173 visibility: "direct"
174 }
175
176 Benchee.run(%{
177 "Direct messages with muted" => fn ->
178 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
179 |> Pleroma.Pagination.fetch_paginated(params)
180 end,
181 "Direct messages without muted" => fn ->
182 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
183 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
184 end
185 })
186
187 dms_with_muted =
188 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
189 |> Pleroma.Pagination.fetch_paginated(params)
190
191 dms_without_muted =
192 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
193 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
194
195 Benchee.run(%{
196 "Rendering dms with muted" => fn ->
197 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
198 activities: dms_with_muted,
199 for: user,
200 as: :activity
201 })
202 end,
203 "Rendering dms without muted" => fn ->
204 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
205 activities: dms_without_muted,
206 for: user,
207 as: :activity
208 })
209 end
210 })
211 end
212
213 def query_long_thread(user, activity) do
214 Benchee.run(%{
215 "Fetch main post" => fn ->
216 Pleroma.Activity.get_by_id_with_object(activity.id)
217 end,
218 "Fetch context of main post" => fn ->
219 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
220 activity.data["context"],
221 %{
222 "blocking_user" => user,
223 "user" => user,
224 "exclude_id" => activity.id
225 }
226 )
227 end
228 })
229
230 activity = Pleroma.Activity.get_by_id_with_object(activity.id)
231
232 context =
233 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
234 activity.data["context"],
235 %{
236 "blocking_user" => user,
237 "user" => user,
238 "exclude_id" => activity.id
239 }
240 )
241
242 Benchee.run(%{
243 "Render status" => fn ->
244 Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{
245 activity: activity,
246 for: user
247 })
248 end,
249 "Render context" => fn ->
250 Pleroma.Web.MastodonAPI.StatusView.render(
251 "index.json",
252 for: user,
253 activities: context,
254 as: :activity
255 )
256 |> Enum.reverse()
257 end
258 })
259 end
260 end