users.ex 4.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
  ]

Alexander Strizhakov's avatar
fix    
Alexander Strizhakov committed
17
  @max_concurrency 10
18
19
20
21
22

  @spec generate(keyword()) :: User.t()
  def generate(opts \\ []) do
    opts = Keyword.merge(@defaults, opts)

Alexander Strizhakov's avatar
Alexander Strizhakov committed
23
    generate_users(opts[:users])
24
25
26
27

    main_user =
      Repo.one(from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1))

Alexander Strizhakov's avatar
Alexander Strizhakov committed
28
    make_friends(main_user, opts[:friends])
29

30
    User.get_by_id(main_user.id)
31
32
  end

Alexander Strizhakov's avatar
Alexander Strizhakov committed
33
  def generate_users(max) do
Alexander Strizhakov's avatar
fix    
Alexander Strizhakov committed
34
    IO.puts("Starting generating #{max} users...")
Alexander Strizhakov's avatar
Alexander Strizhakov committed
35

36
    {time, users} =
Alexander Strizhakov's avatar
Alexander Strizhakov committed
37
38
39
40
41
42
43
      :timer.tc(fn ->
        Task.async_stream(
          1..max,
          &generate_user(&1),
          max_concurrency: @max_concurrency,
          timeout: 30_000
        )
44
        |> Enum.to_list()
Alexander Strizhakov's avatar
Alexander Strizhakov committed
45
46
      end)

47
48
    IO.puts("Generating users took #{to_sec(time)} sec.\n")
    users
49
50
51
52
53
54
55
56
57
  end

  defp generate_user(i) do
    remote = Enum.random([true, false])

    %User{
      name: "Test テスト User #{i}",
      email: "user#{i}@example.com",
      nickname: "nick#{i}",
lain's avatar
lain committed
58
      password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
      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

Alexander Strizhakov's avatar
Alexander Strizhakov committed
90
  def make_friends(main_user, max) when is_integer(max) do
Alexander Strizhakov's avatar
fix    
Alexander Strizhakov committed
91
    IO.puts("Starting making friends for #{max} users...")
92

Alexander Strizhakov's avatar
Alexander Strizhakov committed
93
94
95
96
97
    {time, _} =
      :timer.tc(fn ->
        number_of_users =
          (max / 2)
          |> Kernel.trunc()
98

Alexander Strizhakov's avatar
Alexander Strizhakov committed
99
100
101
102
103
104
105
106
107
        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)

108
    IO.puts("Making friends took #{to_sec(time)} sec.\n")
109
110
  end

Alexander Strizhakov's avatar
fix    
Alexander Strizhakov committed
111
  def make_friends(%User{} = main_user, %User{} = user) do
minibikini's avatar
minibikini committed
112
113
    {:ok, _, _} = User.follow(main_user, user)
    {:ok, _, _} = User.follow(user, main_user)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

  @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
189
end