qb_akkoma/benchmarks/load_testing/users.ex

190 lines
4.4 KiB
Elixir
Raw Normal View History

2020-01-14 11:42:30 +00:00
defmodule Pleroma.LoadTesting.Users do
@moduledoc """
Module for generating users with friends.
"""
import Ecto.Query
import Pleroma.LoadTesting.Helper, only: [to_sec: 1]
alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.User.Query
@defaults [
users: 20_000,
friends: 100
]
2020-03-17 10:47:13 +00:00
@max_concurrency 10
2020-01-14 11:42:30 +00:00
@spec generate(keyword()) :: User.t()
def generate(opts \\ []) do
opts = Keyword.merge(@defaults, opts)
2020-02-18 09:19:10 +00:00
generate_users(opts[:users])
2020-01-14 11:42:30 +00:00
main_user =
Repo.one(from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1))
2020-02-18 09:19:10 +00:00
make_friends(main_user, opts[:friends])
2020-01-14 11:42:30 +00:00
2020-05-20 14:45:06 +00:00
User.get_by_id(main_user.id)
2020-01-14 11:42:30 +00:00
end
2020-02-18 09:19:10 +00:00
def generate_users(max) do
2020-03-17 10:47:13 +00:00
IO.puts("Starting generating #{max} users...")
2020-02-18 09:19:10 +00:00
{time, users} =
2020-02-18 09:19:10 +00:00
:timer.tc(fn ->
Task.async_stream(
1..max,
&generate_user(&1),
max_concurrency: @max_concurrency,
timeout: 30_000
)
|> Enum.to_list()
2020-02-18 09:19:10 +00:00
end)
IO.puts("Generating users took #{to_sec(time)} sec.\n")
users
2020-01-14 11:42:30 +00:00
end
defp generate_user(i) do
remote = Enum.random([true, false])
%User{
name: "Test テスト User #{i}",
email: "user#{i}@example.com",
nickname: "nick#{i}",
2021-01-14 14:06:16 +00:00
password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
2020-01-14 11:42:30 +00:00
bio: "Tester Number #{i}",
local: !remote
}
|> user_urls()
|> Repo.insert!()
end
defp user_urls(%{local: true} = user) do
urls = %{
ap_id: User.ap_id(user),
follower_address: User.ap_followers(user),
following_address: User.ap_following(user)
}
Map.merge(user, urls)
end
defp user_urls(%{local: false} = user) do
base_domain = Enum.random(["domain1.com", "domain2.com", "domain3.com"])
ap_id = "https://#{base_domain}/users/#{user.nickname}"
urls = %{
ap_id: ap_id,
follower_address: ap_id <> "/followers",
following_address: ap_id <> "/following"
}
Map.merge(user, urls)
end
2020-02-18 09:19:10 +00:00
def make_friends(main_user, max) when is_integer(max) do
2020-03-17 10:47:13 +00:00
IO.puts("Starting making friends for #{max} users...")
2020-01-14 11:42:30 +00:00
2020-02-18 09:19:10 +00:00
{time, _} =
:timer.tc(fn ->
number_of_users =
(max / 2)
|> Kernel.trunc()
2020-01-14 11:42:30 +00:00
2020-02-18 09:19:10 +00:00
main_user
|> get_users(%{limit: number_of_users, local: :local})
|> run_stream(main_user)
main_user
|> get_users(%{limit: number_of_users, local: :external})
|> run_stream(main_user)
end)
IO.puts("Making friends took #{to_sec(time)} sec.\n")
2020-01-14 11:42:30 +00:00
end
2020-03-17 10:47:13 +00:00
def make_friends(%User{} = main_user, %User{} = user) do
2020-12-01 20:17:52 +00:00
{:ok, _, _} = User.follow(main_user, user)
{:ok, _, _} = User.follow(user, main_user)
2020-01-14 11:42:30 +00:00
end
@spec get_users(User.t(), keyword()) :: [User.t()]
def get_users(user, opts) do
criteria = %{limit: opts[:limit]}
criteria =
if opts[:local] do
Map.put(criteria, opts[:local], true)
else
criteria
end
criteria =
if opts[:friends?] do
Map.put(criteria, :friends, user)
else
criteria
end
query =
criteria
|> Query.build()
|> random_without_user(user)
query =
if opts[:friends?] == false do
friends_ids =
%{friends: user}
|> Query.build()
|> Repo.all()
|> Enum.map(& &1.id)
from(u in query, where: u.id not in ^friends_ids)
else
query
end
Repo.all(query)
end
defp random_without_user(query, user) do
from(u in query,
where: u.id != ^user.id,
order_by: fragment("RANDOM()")
)
end
defp run_stream(users, main_user) do
Task.async_stream(users, &make_friends(main_user, &1),
max_concurrency: @max_concurrency,
timeout: 30_000
)
|> Stream.run()
end
2020-05-20 14:45:06 +00:00
@spec prepare_users(User.t(), keyword()) :: map()
def prepare_users(user, opts) do
friends_limit = opts[:friends_used]
non_friends_limit = opts[:non_friends_used]
%{
user: user,
friends_local: fetch_users(user, friends_limit, :local, true),
friends_remote: fetch_users(user, friends_limit, :external, true),
non_friends_local: fetch_users(user, non_friends_limit, :local, false),
non_friends_remote: fetch_users(user, non_friends_limit, :external, false)
}
end
defp fetch_users(user, limit, local, friends?) do
user
|> get_users(limit: limit, local: local, friends?: friends?)
|> Enum.shuffle()
end
2020-01-14 11:42:30 +00:00
end