Merge branch 'benchmark-finishing' into 'develop'
[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 Benchee.run(%{
43 "User home timeline" => fn ->
44 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
45 [user.ap_id | user.following],
46 home_timeline_params
47 )
48 end,
49 "User mastodon public timeline" => fn ->
50 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
51 mastodon_public_timeline_params
52 )
53 end,
54 "User mastodon federated public timeline" => fn ->
55 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
56 mastodon_federated_timeline_params
57 )
58 end
59 })
60
61 home_activities =
62 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
63 [user.ap_id | user.following],
64 home_timeline_params
65 )
66
67 public_activities =
68 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(mastodon_public_timeline_params)
69
70 public_federated_activities =
71 Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
72 mastodon_federated_timeline_params
73 )
74
75 Benchee.run(%{
76 "Rendering home timeline" => fn ->
77 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
78 activities: home_activities,
79 for: user,
80 as: :activity
81 })
82 end,
83 "Rendering public timeline" => fn ->
84 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
85 activities: public_activities,
86 for: user,
87 as: :activity
88 })
89 end,
90 "Rendering public federated timeline" => fn ->
91 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
92 activities: public_federated_activities,
93 for: user,
94 as: :activity
95 })
96 end
97 })
98 end
99
100 def query_notifications(user) do
101 without_muted_params = %{"count" => "20", "with_muted" => "false"}
102 with_muted_params = %{"count" => "20", "with_muted" => "true"}
103
104 Benchee.run(%{
105 "Notifications without muted" => fn ->
106 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
107 end,
108 "Notifications with muted" => fn ->
109 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
110 end
111 })
112
113 without_muted_notifications =
114 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
115
116 with_muted_notifications =
117 Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
118
119 Benchee.run(%{
120 "Render notifications without muted" => fn ->
121 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
122 notifications: without_muted_notifications,
123 for: user
124 })
125 end,
126 "Render notifications with muted" => fn ->
127 Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
128 notifications: with_muted_notifications,
129 for: user
130 })
131 end
132 })
133 end
134
135 def query_dms(user) do
136 params = %{
137 "count" => "20",
138 "with_muted" => "true",
139 "type" => "Create",
140 "blocking_user" => user,
141 "user" => user,
142 visibility: "direct"
143 }
144
145 Benchee.run(%{
146 "Direct messages with muted" => fn ->
147 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
148 |> Pleroma.Pagination.fetch_paginated(params)
149 end,
150 "Direct messages without muted" => fn ->
151 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
152 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
153 end
154 })
155
156 dms_with_muted =
157 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
158 |> Pleroma.Pagination.fetch_paginated(params)
159
160 dms_without_muted =
161 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
162 |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
163
164 Benchee.run(%{
165 "Rendering dms with muted" => fn ->
166 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
167 activities: dms_with_muted,
168 for: user,
169 as: :activity
170 })
171 end,
172 "Rendering dms without muted" => fn ->
173 Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
174 activities: dms_without_muted,
175 for: user,
176 as: :activity
177 })
178 end
179 })
180 end
181
182 def query_long_thread(user, activity) do
183 Benchee.run(%{
184 "Fetch main post" => fn ->
185 Pleroma.Activity.get_by_id_with_object(activity.id)
186 end,
187 "Fetch context of main post" => fn ->
188 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
189 activity.data["context"],
190 %{
191 "blocking_user" => user,
192 "user" => user,
193 "exclude_id" => activity.id
194 }
195 )
196 end
197 })
198
199 activity = Pleroma.Activity.get_by_id_with_object(activity.id)
200
201 context =
202 Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
203 activity.data["context"],
204 %{
205 "blocking_user" => user,
206 "user" => user,
207 "exclude_id" => activity.id
208 }
209 )
210
211 Benchee.run(%{
212 "Render status" => fn ->
213 Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{
214 activity: activity,
215 for: user
216 })
217 end,
218 "Render context" => fn ->
219 Pleroma.Web.MastodonAPI.StatusView.render(
220 "index.json",
221 for: user,
222 activities: context,
223 as: :activity
224 )
225 |> Enum.reverse()
226 end
227 })
228 end
229 end