Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel
[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 })
100 end
101
102 def query_notifications(user) do
103 without_muted_params = %{"count" => "20", "with_muted" => "false"}
104 with_muted_params = %{"count" => "20", "with_muted" => "true"}
105
106 Benchee.run(%{
107 "Notifications without muted" => fn ->
108 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
109 end,
110 "Notifications with muted" => fn ->
111 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
112 end
113 })
114
115 without_muted_notifications =
116 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
117
118 with_muted_notifications =
119 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
120
121 Benchee.run(%{
122 "Render notifications without muted" => fn ->
123 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
124 notifications: without_muted_notifications,
125 for: user
126 })
127 end,
128 "Render notifications with muted" => fn ->
129 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
130 notifications: with_muted_notifications,
131 for: user
132 })
133 end
134 })
135 end
136
137 def query_dms(user) do
138 params = %{
139 "count" => "20",
140 "with_muted" => "true",
141 "type" => "Create",
142 "blocking_user" => user,
143 "user" => user,
144 visibility: "direct"
145 }
146
147 Benchee.run(%{
148 "Direct messages with muted" => fn ->
149 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
150 |> Pleroma.Pagination.fetch_paginated(params)
151 end,
152 "Direct messages without muted" => fn ->
153 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
154 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
155 end
156 })
157
158 dms_with_muted =
159 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
160 |> Pleroma.Pagination.fetch_paginated(params)
161
162 dms_without_muted =
163 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
164 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
165
166 Benchee.run(%{
167 "Rendering dms with muted" => fn ->
168 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
169 activities: dms_with_muted,
170 for: user,
171 as: :activity
172 })
173 end,
174 "Rendering dms without muted" => fn ->
175 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
176 activities: dms_without_muted,
177 for: user,
178 as: :activity
179 })
180 end
181 })
182 end
183
184 def query_long_thread(user, activity) do
185 Benchee.run(%{
186 "Fetch main post" => fn ->
187 Pleroma.Activity.get_by_id_with_object(activity.id)
188 end,
189 "Fetch context of main post" => fn ->
190 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
191 activity.data["context"],
192 %{
193 "blocking_user" => user,
194 "user" => user,
195 "exclude_id" => activity.id
196 }
197 )
198 end
199 })
200
201 activity = Pleroma.Activity.get_by_id_with_object(activity.id)
202
203 context =
204 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
205 activity.data["context"],
206 %{
207 "blocking_user" => user,
208 "user" => user,
209 "exclude_id" => activity.id
210 }
211 )
212
213 Benchee.run(%{
214 "Render status" => fn ->
215 Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{
216 activity: activity,
217 for: user
218 })
219 end,
220 "Render context" => fn ->
221 Pleroma.Web.MastodonAPI.StatusView.render(
222 "index.json",
223 for: user,
224 activities: context,
225 as: :activity
226 )
227 |> Enum.reverse()
228 end
229 })
230 end
231 end