1 defmodule Pleroma.LoadTesting.Activities do
3 Module for generating different activities.
6 import Pleroma.LoadTesting.Helper, only: [to_sec: 1]
9 alias Pleroma.Constants
10 alias Pleroma.LoadTesting.Users
12 alias Pleroma.Web.CommonAPI
24 @visibility ~w(public private direct unlisted)
36 @groups [:friends_local, :friends_remote, :non_friends_local, :non_friends_local]
37 @remote_groups [:friends_remote, :non_friends_remote]
38 @friends_groups [:friends_local, :friends_remote]
39 @non_friends_groups [:non_friends_local, :non_friends_remote]
41 @spec generate(User.t(), keyword()) :: :ok
42 def generate(user, opts \\ []) do
44 Agent.start_link(fn -> %{} end,
45 name: :benchmark_state
48 opts = Keyword.merge(@defaults, opts)
50 users = Users.prepare_users(user, opts)
52 {:ok, _} = Agent.start_link(fn -> users[:non_friends_remote] end, name: :non_friends_remote)
55 for visibility <- @visibility,
57 group <- [:user | @groups],
58 do: {visibility, type, group}
60 IO.puts("Starting generating #{opts[:iterations]} iterations of activities...")
62 public_long_thread = fn ->
63 generate_long_thread("public", users, opts)
66 private_long_thread = fn ->
67 generate_long_thread("private", users, opts)
70 iterations = opts[:iterations]
77 i when i == iterations - 2 ->
78 spawn(public_long_thread)
79 spawn(private_long_thread)
80 generate_activities(users, Enum.shuffle(task_data), opts)
83 generate_activities(users, Enum.shuffle(task_data), opts)
88 IO.puts("Generating iterations of activities took #{to_sec(time)} sec.\n")
92 def generate_power_intervals(opts \\ []) do
93 count = Keyword.get(opts, :count, 20)
94 power = Keyword.get(opts, :power, 2)
95 IO.puts("Generating #{count} intervals for a power #{power} series...")
96 counts = Enum.map(1..count, fn n -> :math.pow(n, power) end)
97 sum = Enum.sum(counts)
100 Enum.map(counts, fn c ->
105 |> Enum.reduce(0, fn density, acc ->
109 [{_, lower} | _] = acc
110 [{lower, lower + density} | acc]
116 def generate_tagged_activities(opts \\ []) do
117 tag_count = Keyword.get(opts, :tag_count, 20)
118 users = Keyword.get(opts, :users, Repo.all(Pleroma.User))
119 activity_count = Keyword.get(opts, :count, 200_000)
121 intervals = generate_power_intervals(count: tag_count)
124 "Generating #{activity_count} activities using #{tag_count} different tags of format `tag_n`, starting at tag_0"
127 Enum.each(1..activity_count, fn _ ->
128 random = :rand.uniform()
129 i = Enum.find_index(intervals, fn {lower, upper} -> lower <= random && upper > random end)
130 CommonAPI.post(Enum.random(users), %{status: "a post with the tag #tag_#{i}"})
134 defp generate_long_thread(visibility, users, _opts) do
136 if visibility == "public",
140 tasks = get_reply_tasks(visibility, group) |> Stream.cycle() |> Enum.take(50)
143 CommonAPI.post(users[:user], %{
144 status: "Start of #{visibility} long thread",
145 visibility: visibility
148 Agent.update(:benchmark_state, fn state ->
150 if visibility == "public",
152 else: :private_thread
154 Map.put(state, key, activity)
157 acc = {activity.id, ["@" <> users[:user].nickname, "reply to long thread"]}
158 insert_replies_for_long_thread(tasks, visibility, users, acc)
159 IO.puts("Generating #{visibility} long thread ended\n")
162 defp insert_replies_for_long_thread(tasks, visibility, users, acc) do
163 Enum.reduce(tasks, acc, fn
166 insert_reply(user, List.delete(data, "@" <> user.nickname), id, visibility)
169 replier = Enum.random(users[group])
170 insert_reply(replier, List.delete(data, "@" <> replier.nickname), id, visibility)
174 defp generate_activities(users, task_data, opts) do
177 fn {visibility, type, group} ->
178 insert_activity(type, visibility, group, users, opts)
180 max_concurrency: @max_concurrency,
186 defp insert_local_activity(visibility, group, users, status) do
190 |> CommonAPI.post(%{status: status, visibility: visibility})
193 defp insert_remote_activity(visibility, group, users, status) do
194 actor = get_actor(group, users)
195 {act_data, obj_data} = prepare_activity_data(actor, visibility, users[:user])
196 {activity_data, object_data} = other_data(actor, status)
199 |> Map.merge(act_data)
200 |> Map.put("object", Map.merge(object_data, obj_data))
201 |> Pleroma.Web.ActivityPub.ActivityPub.insert(false)
204 defp user_mentions(users) do
210 acc ++ get_random_mentions(users[group], Enum.random(0..2))
214 if Enum.random([true, false]),
215 do: ["@" <> users[:user].nickname | user_mentions],
219 defp hell_thread_mentions(users) do
220 with {:ok, nil} <- Cachex.get(:user_cache, "hell_thread_mentions") do
223 |> Enum.reduce([users[:user]], fn group, acc ->
224 acc ++ Enum.take(users[group], 5)
226 |> Enum.map(&"@#{&1.nickname}")
229 Cachex.put(:user_cache, "hell_thread_mentions", cached)
232 {:ok, cached} -> cached
236 defp insert_activity(:simple, visibility, group, users, _opts)
237 when group in @remote_groups do
238 insert_remote_activity(visibility, group, users, "Remote status")
241 defp insert_activity(:simple, visibility, group, users, _opts) do
242 insert_local_activity(visibility, group, users, "Simple status")
245 defp insert_activity(:emoji, visibility, group, users, _opts)
246 when group in @remote_groups do
247 insert_remote_activity(visibility, group, users, "Remote status with emoji :firefox:")
250 defp insert_activity(:emoji, visibility, group, users, _opts) do
251 insert_local_activity(visibility, group, users, "Simple status with emoji :firefox:")
254 defp insert_activity(:mentions, visibility, group, users, _opts)
255 when group in @remote_groups do
256 mentions = user_mentions(users)
258 status = Enum.join(mentions, ", ") <> " remote status with mentions"
260 insert_remote_activity(visibility, group, users, status)
263 defp insert_activity(:mentions, visibility, group, users, _opts) do
264 mentions = user_mentions(users)
266 status = Enum.join(mentions, ", ") <> " simple status with mentions"
267 insert_remote_activity(visibility, group, users, status)
270 defp insert_activity(:hell_thread, visibility, group, users, _)
271 when group in @remote_groups do
272 mentions = hell_thread_mentions(users)
273 insert_remote_activity(visibility, group, users, mentions <> " remote hell thread status")
276 defp insert_activity(:hell_thread, visibility, group, users, _opts) do
277 mentions = hell_thread_mentions(users)
279 insert_local_activity(visibility, group, users, mentions <> " hell thread status")
282 defp insert_activity(:attachment, visibility, group, users, _opts) do
283 actor = get_actor(group, users)
286 "actor" => actor.ap_id,
287 "name" => "4467-11.jpg",
288 "type" => "Document",
292 "#{Pleroma.Web.base_url()}/media/b1b873552422a07bf53af01f3c231c841db4dfc42c35efde681abaf0f2a4eab7.jpg",
293 "mediaType" => "image/jpeg",
299 object = Repo.insert!(%Pleroma.Object{data: obj_data})
302 CommonAPI.post(actor, %{
303 status: "Post with attachment",
304 visibility: visibility,
305 media_ids: [object.id]
309 defp insert_activity(:tag, visibility, group, users, _opts) do
310 insert_local_activity(visibility, group, users, "Status with #tag")
313 defp insert_activity(:like, visibility, group, users, opts) do
314 actor = get_actor(group, users)
316 with activity_id when not is_nil(activity_id) <- get_random_create_activity_id(),
317 {:ok, _activity} <- CommonAPI.favorite(actor, activity_id) do
321 insert_activity(:like, visibility, group, users, opts)
325 insert_activity(:like, visibility, group, users, opts)
329 defp insert_activity(:reblog, visibility, group, users, opts) do
330 actor = get_actor(group, users)
332 with activity_id when not is_nil(activity_id) <- get_random_create_activity_id(),
333 {:ok, _activity} <- CommonAPI.repeat(activity_id, actor) do
337 insert_activity(:reblog, visibility, group, users, opts)
341 insert_activity(:reblog, visibility, group, users, opts)
345 defp insert_activity(:simple_thread, "direct", group, users, _opts) do
346 actor = get_actor(group, users)
347 tasks = get_reply_tasks("direct", group)
352 group = Enum.random(@friends_groups)
353 Enum.take(users[group], 3)
356 Enum.take(users[group], 3)
359 data = Enum.map(list, &("@" <> &1.nickname))
362 CommonAPI.post(actor, %{
363 status: Enum.join(data, ", ") <> "simple status",
367 acc = {activity.id, ["@" <> users[:user].nickname | data] ++ ["reply to status"]}
368 insert_direct_replies(tasks, users[:user], list, acc)
371 defp insert_activity(:simple_thread, visibility, group, users, _opts) do
372 actor = get_actor(group, users)
373 tasks = get_reply_tasks(visibility, group)
376 CommonAPI.post(users[:user], %{status: "Simple status", visibility: visibility})
378 acc = {activity.id, ["@" <> actor.nickname, "reply to status"]}
379 insert_replies(tasks, visibility, users, acc)
382 defp get_actor(:user, %{user: user}), do: user
383 defp get_actor(group, users), do: Enum.random(users[group])
385 defp other_data(actor, content) do
386 %{host: host} = URI.parse(actor.ap_id)
387 datetime = DateTime.utc_now()
388 context_id = "https://#{host}/contexts/#{UUID.generate()}"
389 activity_id = "https://#{host}/activities/#{UUID.generate()}"
390 object_id = "https://#{host}/objects/#{UUID.generate()}"
393 "actor" => actor.ap_id,
394 "context" => context_id,
396 "published" => datetime,
398 "directMessage" => false
402 "actor" => actor.ap_id,
404 "attributedTo" => actor.ap_id,
407 "content" => content,
408 "context" => context_id,
409 "conversation" => context_id,
412 "published" => datetime,
413 "sensitive" => false,
416 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
420 {activity_data, object_data}
423 defp prepare_activity_data(actor, "public", _mention) do
425 "cc" => [actor.follower_address],
426 "to" => [Constants.as_public()]
430 "cc" => [actor.follower_address],
431 "to" => [Constants.as_public()]
437 defp prepare_activity_data(actor, "private", _mention) do
440 "to" => [actor.follower_address]
445 "to" => [actor.follower_address]
451 defp prepare_activity_data(actor, "unlisted", _mention) do
453 "cc" => [Constants.as_public()],
454 "to" => [actor.follower_address]
458 "cc" => [Constants.as_public()],
459 "to" => [actor.follower_address]
465 defp prepare_activity_data(_actor, "direct", mention) do
466 %{host: mentioned_host} = URI.parse(mention.ap_id)
471 "<span class=\"h-card\"><a class=\"u-url mention\" href=\"#{mention.ap_id}\" rel=\"ugc\">@<span>#{
473 }</span></a></span> direct message",
476 "href" => mention.ap_id,
477 "name" => "@#{mention.nickname}@#{mentioned_host}",
481 "to" => [mention.ap_id]
486 "directMessage" => true,
487 "to" => [mention.ap_id]
493 defp get_reply_tasks("public", :user) do
494 [:friends_local, :friends_remote, :non_friends_local, :non_friends_remote, :user]
497 defp get_reply_tasks("public", group) when group in @friends_groups do
498 [:non_friends_local, :non_friends_remote, :user, :friends_local, :friends_remote]
501 defp get_reply_tasks("public", group) when group in @non_friends_groups do
502 [:user, :friends_local, :friends_remote, :non_friends_local, :non_friends_remote]
505 defp get_reply_tasks(visibility, :user) when visibility in ["unlisted", "private"] do
506 [:friends_local, :friends_remote, :user, :friends_local, :friends_remote]
509 defp get_reply_tasks(visibility, group)
510 when visibility in ["unlisted", "private"] and group in @friends_groups do
511 [:user, :friends_remote, :friends_local, :user]
514 defp get_reply_tasks(visibility, group)
515 when visibility in ["unlisted", "private"] and
516 group in @non_friends_groups,
519 defp get_reply_tasks("direct", :user), do: [:friends_local, :user, :friends_remote]
521 defp get_reply_tasks("direct", group) when group in @friends_groups,
522 do: [:user, group, :user]
524 defp get_reply_tasks("direct", group) when group in @non_friends_groups do
525 [:user, :non_friends_remote, :user, :non_friends_local]
528 defp insert_replies(tasks, visibility, users, acc) do
529 Enum.reduce(tasks, acc, fn
531 insert_reply(users[:user], data, id, visibility)
534 replier = Enum.random(users[group])
535 insert_reply(replier, data, id, visibility)
539 defp insert_direct_replies(tasks, user, list, acc) do
540 Enum.reduce(tasks, acc, fn
542 {reply_id, _} = insert_reply(user, List.delete(data, "@" <> user.nickname), id, "direct")
546 actor = Enum.random(list)
549 insert_reply(actor, List.delete(data, "@" <> actor.nickname), id, "direct")
555 defp insert_reply(actor, data, activity_id, visibility) do
557 CommonAPI.post(actor, %{
558 status: Enum.join(data, ", "),
559 visibility: visibility,
560 in_reply_to_status_id: activity_id
563 {reply.id, ["@" <> actor.nickname | data]}
566 defp get_random_mentions(_users, count) when count == 0, do: []
568 defp get_random_mentions(users, count) do
572 |> Enum.map(&"@#{&1.nickname}")
575 defp get_random_create_activity_id do
577 from(a in Pleroma.Activity,
578 where: fragment("(?)->>'type' = ?", a.data, ^"Create"),
579 order_by: fragment("RANDOM()"),