1 defmodule Pleroma.LoadTesting.Users do
3 Module for generating users with friends.
6 import Pleroma.LoadTesting.Helper, only: [to_sec: 1]
10 alias Pleroma.User.Query
19 @spec generate(keyword()) :: User.t()
20 def generate(opts \\ []) do
21 opts = Keyword.merge(@defaults, opts)
23 generate_users(opts[:users])
26 Repo.one(from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1))
28 make_friends(main_user, opts[:friends])
30 Repo.get(User, main_user.id)
33 def generate_users(max) do
34 IO.puts("Starting generating #{max} users...")
41 max_concurrency: @max_concurrency,
47 IO.puts("Generating users took #{to_sec(time)} sec.\n")
51 defp generate_user(i) do
52 remote = Enum.random([true, false])
55 name: "Test ใในใ User #{i}",
56 email: "user#{i}@example.com",
58 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
59 bio: "Tester Number #{i}",
66 defp user_urls(%{local: true} = user) do
68 ap_id: User.ap_id(user),
69 follower_address: User.ap_followers(user),
70 following_address: User.ap_following(user)
76 defp user_urls(%{local: false} = user) do
77 base_domain = Enum.random(["domain1.com", "domain2.com", "domain3.com"])
79 ap_id = "https://#{base_domain}/users/#{user.nickname}"
83 follower_address: ap_id <> "/followers",
84 following_address: ap_id <> "/following"
90 def make_friends(main_user, max) when is_integer(max) do
91 IO.puts("Starting making friends for #{max} users...")
100 |> get_users(%{limit: number_of_users, local: :local})
101 |> run_stream(main_user)
104 |> get_users(%{limit: number_of_users, local: :external})
105 |> run_stream(main_user)
108 IO.puts("Making friends took #{to_sec(time)} sec.\n")
111 def make_friends(%User{} = main_user, %User{} = user) do
112 {:ok, _} = User.follow(main_user, user)
113 {:ok, _} = User.follow(user, main_user)
116 @spec get_users(User.t(), keyword()) :: [User.t()]
117 def get_users(user, opts) do
118 criteria = %{limit: opts[:limit]}
122 Map.put(criteria, opts[:local], true)
128 if opts[:friends?] do
129 Map.put(criteria, :friends, user)
137 |> random_without_user(user)
140 if opts[:friends?] == false do
147 from(u in query, where: u.id not in ^friends_ids)
155 defp random_without_user(query, user) do
157 where: u.id != ^user.id,
158 order_by: fragment("RANDOM()")
162 defp run_stream(users, main_user) do
163 Task.async_stream(users, &make_friends(main_user, &1),
164 max_concurrency: @max_concurrency,