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)
37 @groups [:friends_local, :friends_remote, :non_friends_local, :non_friends_local]
38 @remote_groups [:friends_remote, :non_friends_remote]
39 @friends_groups [:friends_local, :friends_remote]
40 @non_friends_groups [:non_friends_local, :non_friends_remote]
42 @spec generate(User.t(), keyword()) :: :ok
43 def generate(user, opts \\ []) do
45 Agent.start_link(fn -> %{} end,
46 name: :benchmark_state
49 opts = Keyword.merge(@defaults, opts)
51 users = Users.prepare_users(user, opts)
53 {:ok, _} = Agent.start_link(fn -> users[:non_friends_remote] end, name: :non_friends_remote)
56 for visibility <- @visibility,
58 group <- [:user | @groups],
59 do: {visibility, type, group}
61 IO.puts("Starting generating #{opts[:iterations]} iterations of activities...")
63 public_long_thread = fn ->
64 generate_long_thread("public", users, opts)
67 private_long_thread = fn ->
68 generate_long_thread("private", users, opts)
71 iterations = opts[:iterations]
78 i when i == iterations - 2 ->
79 spawn(public_long_thread)
80 spawn(private_long_thread)
81 generate_activities(users, Enum.shuffle(task_data), opts)
84 generate_activities(users, Enum.shuffle(task_data), opts)
89 IO.puts("Generating iterations of activities took #{to_sec(time)} sec.\n")
93 def generate_power_intervals(opts \\ []) do
94 count = Keyword.get(opts, :count, 20)
95 power = Keyword.get(opts, :power, 2)
96 IO.puts("Generating #{count} intervals for a power #{power} series...")
97 counts = Enum.map(1..count, fn n -> :math.pow(n, power) end)
98 sum = Enum.sum(counts)
101 Enum.map(counts, fn c ->
106 |> Enum.reduce(0, fn density, acc ->
110 [{_, lower} | _] = acc
111 [{lower, lower + density} | acc]
117 def generate_tagged_activities(opts \\ []) do
118 tag_count = Keyword.get(opts, :tag_count, 20)
119 users = Keyword.get(opts, :users, Repo.all(Pleroma.User))
120 activity_count = Keyword.get(opts, :count, 200_000)
122 intervals = generate_power_intervals(count: tag_count)
125 "Generating #{activity_count} activities using #{tag_count} different tags of format `tag_n`, starting at tag_0"
128 Enum.each(1..activity_count, fn _ ->
129 random = :rand.uniform()
130 i = Enum.find_index(intervals, fn {lower, upper} -> lower <= random && upper > random end)
131 CommonAPI.post(Enum.random(users), %{status: "a post with the tag #tag_#{i}"})
135 defp generate_long_thread(visibility, users, _opts) do
137 if visibility == "public",
141 tasks = get_reply_tasks(visibility, group) |> Stream.cycle() |> Enum.take(50)
144 CommonAPI.post(users[:user], %{
145 status: "Start of #{visibility} long thread",
146 visibility: visibility
149 Agent.update(:benchmark_state, fn state ->
151 if visibility == "public",
153 else: :private_thread
155 Map.put(state, key, activity)
158 acc = {activity.id, ["@" <> users[:user].nickname, "reply to long thread"]}
159 insert_replies_for_long_thread(tasks, visibility, users, acc)
160 IO.puts("Generating #{visibility} long thread ended\n")
163 defp insert_replies_for_long_thread(tasks, visibility, users, acc) do
164 Enum.reduce(tasks, acc, fn
167 insert_reply(user, List.delete(data, "@" <> user.nickname), id, visibility)
170 replier = Enum.random(users[group])
171 insert_reply(replier, List.delete(data, "@" <> replier.nickname), id, visibility)
175 defp generate_activities(users, task_data, opts) do
178 fn {visibility, type, group} ->
179 insert_activity(type, visibility, group, users, opts)
181 max_concurrency: @max_concurrency,
187 defp insert_local_activity(visibility, group, users, status) do
191 |> CommonAPI.post(%{status: status, visibility: visibility})
194 defp insert_remote_activity(visibility, group, users, status) do
195 actor = get_actor(group, users)
196 {act_data, obj_data} = prepare_activity_data(actor, visibility, users[:user])
197 {activity_data, object_data} = other_data(actor, status)
200 |> Map.merge(act_data)
201 |> Map.put("object", Map.merge(object_data, obj_data))
202 |> Pleroma.Web.ActivityPub.ActivityPub.insert(false)
205 defp user_mentions(users) do
211 acc ++ get_random_mentions(users[group], Enum.random(0..2))
215 if Enum.random([true, false]),
216 do: ["@" <> users[:user].nickname | user_mentions],
220 defp hell_thread_mentions(users) do
221 with {:ok, nil} <- Cachex.get(:user_cache, "hell_thread_mentions") do
224 |> Enum.reduce([users[:user]], fn group, acc ->
225 acc ++ Enum.take(users[group], 5)
227 |> Enum.map(&"@#{&1.nickname}")
230 Cachex.put(:user_cache, "hell_thread_mentions", cached)
233 {:ok, cached} -> cached
237 defp insert_activity(:simple, visibility, group, users, _opts)
238 when group in @remote_groups do
239 insert_remote_activity(visibility, group, users, "Remote status")
242 defp insert_activity(:simple, visibility, group, users, _opts) do
243 insert_local_activity(visibility, group, users, "Simple status")
246 defp insert_activity(:simple_filtered, visibility, group, users, _opts)
247 when group in @remote_groups do
248 insert_remote_activity(visibility, group, users, "Remote status which must be filtered")
251 defp insert_activity(:simple_filtered, visibility, group, users, _opts) do
252 insert_local_activity(visibility, group, users, "Simple status which must be filtered")
255 defp insert_activity(:emoji, visibility, group, users, _opts)
256 when group in @remote_groups do
257 insert_remote_activity(visibility, group, users, "Remote status with emoji :firefox:")
260 defp insert_activity(:emoji, visibility, group, users, _opts) do
261 insert_local_activity(visibility, group, users, "Simple status with emoji :firefox:")
264 defp insert_activity(:mentions, visibility, group, users, _opts)
265 when group in @remote_groups do
266 mentions = user_mentions(users)
268 status = Enum.join(mentions, ", ") <> " remote status with mentions"
270 insert_remote_activity(visibility, group, users, status)
273 defp insert_activity(:mentions, visibility, group, users, _opts) do
274 mentions = user_mentions(users)
276 status = Enum.join(mentions, ", ") <> " simple status with mentions"
277 insert_remote_activity(visibility, group, users, status)
280 defp insert_activity(:hell_thread, visibility, group, users, _)
281 when group in @remote_groups do
282 mentions = hell_thread_mentions(users)
283 insert_remote_activity(visibility, group, users, mentions <> " remote hell thread status")
286 defp insert_activity(:hell_thread, visibility, group, users, _opts) do
287 mentions = hell_thread_mentions(users)
289 insert_local_activity(visibility, group, users, mentions <> " hell thread status")
292 defp insert_activity(:attachment, visibility, group, users, _opts) do
293 actor = get_actor(group, users)
296 "actor" => actor.ap_id,
297 "name" => "4467-11.jpg",
298 "type" => "Document",
302 "#{Pleroma.Web.Endpoint.url()}/media/b1b873552422a07bf53af01f3c231c841db4dfc42c35efde681abaf0f2a4eab7.jpg",
303 "mediaType" => "image/jpeg",
309 object = Repo.insert!(%Pleroma.Object{data: obj_data})
312 CommonAPI.post(actor, %{
313 status: "Post with attachment",
314 visibility: visibility,
315 media_ids: [object.id]
319 defp insert_activity(:tag, visibility, group, users, _opts) do
320 insert_local_activity(visibility, group, users, "Status with #tag")
323 defp insert_activity(:like, visibility, group, users, opts) do
324 actor = get_actor(group, users)
326 with activity_id when not is_nil(activity_id) <- get_random_create_activity_id(),
327 {:ok, _activity} <- CommonAPI.favorite(actor, activity_id) do
331 insert_activity(:like, visibility, group, users, opts)
335 insert_activity(:like, visibility, group, users, opts)
339 defp insert_activity(:reblog, visibility, group, users, opts) do
340 actor = get_actor(group, users)
342 with activity_id when not is_nil(activity_id) <- get_random_create_activity_id(),
343 {:ok, _activity} <- CommonAPI.repeat(activity_id, actor) do
347 insert_activity(:reblog, visibility, group, users, opts)
351 insert_activity(:reblog, visibility, group, users, opts)
355 defp insert_activity(:simple_thread, "direct", group, users, _opts) do
356 actor = get_actor(group, users)
357 tasks = get_reply_tasks("direct", group)
362 group = Enum.random(@friends_groups)
363 Enum.take(users[group], 3)
366 Enum.take(users[group], 3)
369 data = Enum.map(list, &("@" <> &1.nickname))
372 CommonAPI.post(actor, %{
373 status: Enum.join(data, ", ") <> "simple status",
377 acc = {activity.id, ["@" <> users[:user].nickname | data] ++ ["reply to status"]}
378 insert_direct_replies(tasks, users[:user], list, acc)
381 defp insert_activity(:simple_thread, visibility, group, users, _opts) do
382 actor = get_actor(group, users)
383 tasks = get_reply_tasks(visibility, group)
386 CommonAPI.post(users[:user], %{status: "Simple status", visibility: visibility})
388 acc = {activity.id, ["@" <> actor.nickname, "reply to status"]}
389 insert_replies(tasks, visibility, users, acc)
392 defp get_actor(:user, %{user: user}), do: user
393 defp get_actor(group, users), do: Enum.random(users[group])
395 defp other_data(actor, content) do
396 %{host: host} = URI.parse(actor.ap_id)
397 datetime = DateTime.utc_now() |> to_string()
398 context_id = "https://#{host}/contexts/#{UUID.generate()}"
399 activity_id = "https://#{host}/activities/#{UUID.generate()}"
400 object_id = "https://#{host}/objects/#{UUID.generate()}"
403 "actor" => actor.ap_id,
404 "context" => context_id,
406 "published" => datetime,
408 "directMessage" => false
412 "actor" => actor.ap_id,
414 "attributedTo" => actor.ap_id,
417 "content" => content,
418 "context" => context_id,
419 "conversation" => context_id,
422 "published" => datetime,
423 "sensitive" => false,
426 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
430 {activity_data, object_data}
433 defp prepare_activity_data(actor, "public", _mention) do
435 "cc" => [actor.follower_address],
436 "to" => [Constants.as_public()]
440 "cc" => [actor.follower_address],
441 "to" => [Constants.as_public()]
447 defp prepare_activity_data(actor, "private", _mention) do
450 "to" => [actor.follower_address]
455 "to" => [actor.follower_address]
461 defp prepare_activity_data(actor, "unlisted", _mention) do
463 "cc" => [Constants.as_public()],
464 "to" => [actor.follower_address]
468 "cc" => [Constants.as_public()],
469 "to" => [actor.follower_address]
475 defp prepare_activity_data(_actor, "direct", mention) do
476 %{host: mentioned_host} = URI.parse(mention.ap_id)
481 "<span class=\"h-card\"><a class=\"u-url mention\" href=\"#{mention.ap_id}\" rel=\"ugc\">@<span>#{
483 }</span></a></span> direct message",
486 "href" => mention.ap_id,
487 "name" => "@#{mention.nickname}@#{mentioned_host}",
491 "to" => [mention.ap_id]
496 "directMessage" => true,
497 "to" => [mention.ap_id]
503 defp get_reply_tasks("public", :user) do
504 [:friends_local, :friends_remote, :non_friends_local, :non_friends_remote, :user]
507 defp get_reply_tasks("public", group) when group in @friends_groups do
508 [:non_friends_local, :non_friends_remote, :user, :friends_local, :friends_remote]
511 defp get_reply_tasks("public", group) when group in @non_friends_groups do
512 [:user, :friends_local, :friends_remote, :non_friends_local, :non_friends_remote]
515 defp get_reply_tasks(visibility, :user) when visibility in ["unlisted", "private"] do
516 [:friends_local, :friends_remote, :user, :friends_local, :friends_remote]
519 defp get_reply_tasks(visibility, group)
520 when visibility in ["unlisted", "private"] and group in @friends_groups do
521 [:user, :friends_remote, :friends_local, :user]
524 defp get_reply_tasks(visibility, group)
525 when visibility in ["unlisted", "private"] and
526 group in @non_friends_groups,
529 defp get_reply_tasks("direct", :user), do: [:friends_local, :user, :friends_remote]
531 defp get_reply_tasks("direct", group) when group in @friends_groups,
532 do: [:user, group, :user]
534 defp get_reply_tasks("direct", group) when group in @non_friends_groups do
535 [:user, :non_friends_remote, :user, :non_friends_local]
538 defp insert_replies(tasks, visibility, users, acc) do
539 Enum.reduce(tasks, acc, fn
541 insert_reply(users[:user], data, id, visibility)
544 replier = Enum.random(users[group])
545 insert_reply(replier, data, id, visibility)
549 defp insert_direct_replies(tasks, user, list, acc) do
550 Enum.reduce(tasks, acc, fn
552 {reply_id, _} = insert_reply(user, List.delete(data, "@" <> user.nickname), id, "direct")
556 actor = Enum.random(list)
559 insert_reply(actor, List.delete(data, "@" <> actor.nickname), id, "direct")
565 defp insert_reply(actor, data, activity_id, visibility) do
567 CommonAPI.post(actor, %{
568 status: Enum.join(data, ", "),
569 visibility: visibility,
570 in_reply_to_status_id: activity_id
573 {reply.id, ["@" <> actor.nickname | data]}
576 defp get_random_mentions(_users, count) when count == 0, do: []
578 defp get_random_mentions(users, count) do
582 |> Enum.map(&"@#{&1.nickname}")
585 defp get_random_create_activity_id do
587 from(a in Pleroma.Activity,
588 where: fragment("(?)->>'type' = ?", a.data, ^"Create"),
589 order_by: fragment("RANDOM()"),