summaryrefslogtreecommitdiff
path: root/benchmarks/load_testing/generator.ex
blob: 3f88fefd7001b6e98030b14486637b916bbb8ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
defmodule Pleroma.LoadTesting.Generator do
  use Pleroma.LoadTesting.Helper
  alias Pleroma.Web.CommonAPI

  def generate_like_activities(user, posts) do
    count_likes = Kernel.trunc(length(posts) / 4)
    IO.puts("Starting generating #{count_likes} like activities...")

    {time, _} =
      :timer.tc(fn ->
        Task.async_stream(
          Enum.take_random(posts, count_likes),
          fn post -> {:ok, _, _} = CommonAPI.favorite(post.id, user) end,
          max_concurrency: 10,
          timeout: 30_000
        )
        |> Stream.run()
      end)

    IO.puts("Inserting like activities take #{to_sec(time)} sec.\n")
  end

  def generate_users(opts) do
    IO.puts("Starting generating #{opts[:users_max]} users...")
    {time, _} = :timer.tc(fn -> do_generate_users(opts) end)

    IO.puts("Inserting users take #{to_sec(time)} sec.\n")
  end

  defp do_generate_users(opts) do
    max = Keyword.get(opts, :users_max)

    Task.async_stream(
      1..max,
      &generate_user_data(&1),
      max_concurrency: 10,
      timeout: 30_000
    )
    |> Enum.to_list()
  end

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

    user = %User{
      name: "Test テスト User #{i}",
      email: "user#{i}@example.com",
      nickname: "nick#{i}",
      password_hash:
        "$pbkdf2-sha512$160000$bU.OSFI7H/yqWb5DPEqyjw$uKp/2rmXw12QqnRRTqTtuk2DTwZfF8VR4MYW2xMeIlqPR/UX1nT1CEKVUx2CowFMZ5JON8aDvURrZpJjSgqXrg",
      bio: "Tester Number #{i}",
      local: remote
    }

    user_urls =
      if remote do
        base_url =
          Enum.random(["https://domain1.com", "https://domain2.com", "https://domain3.com"])

        ap_id = "#{base_url}/users/#{user.nickname}"

        %{
          ap_id: ap_id,
          follower_address: ap_id <> "/followers",
          following_address: ap_id <> "/following"
        }
      else
        %{
          ap_id: User.ap_id(user),
          follower_address: User.ap_followers(user),
          following_address: User.ap_following(user)
        }
      end

    user = Map.merge(user, user_urls)

    Repo.insert!(user)
  end

  def generate_activities(user, users) do
    do_generate_activities(user, users)
  end

  defp do_generate_activities(user, users) do
    IO.puts("Starting generating 20000 common activities...")

    {time, _} =
      :timer.tc(fn ->
        Task.async_stream(
          1..20_000,
          fn _ ->
            do_generate_activity([user | users])
          end,
          max_concurrency: 10,
          timeout: 30_000
        )
        |> Stream.run()
      end)

    IO.puts("Inserting common activities take #{to_sec(time)} sec.\n")

    IO.puts("Starting generating 20000 activities with mentions...")

    {time, _} =
      :timer.tc(fn ->
        Task.async_stream(
          1..20_000,
          fn _ ->
            do_generate_activity_with_mention(user, users)
          end,
          max_concurrency: 10,
          timeout: 30_000
        )
        |> Stream.run()
      end)

    IO.puts("Inserting activities with menthions take #{to_sec(time)} sec.\n")

    IO.puts("Starting generating 10000 activities with threads...")

    {time, _} =
      :timer.tc(fn ->
        Task.async_stream(
          1..10_000,
          fn _ ->
            do_generate_threads([user | users])
          end,
          max_concurrency: 10,
          timeout: 30_000
        )
        |> Stream.run()
      end)

    IO.puts("Inserting activities with threads take #{to_sec(time)} sec.\n")
  end

  defp do_generate_activity(users) do
    post = %{
      "status" => "Some status without mention with random user"
    }

    CommonAPI.post(Enum.random(users), post)
  end

  def generate_power_intervals(opts \\ []) do
    count = Keyword.get(opts, :count, 20)
    power = Keyword.get(opts, :power, 2)
    IO.puts("Generating #{count} intervals for a power #{power} series...")
    counts = Enum.map(1..count, fn n -> :math.pow(n, power) end)
    sum = Enum.sum(counts)

    densities =
      Enum.map(counts, fn c ->
        c / sum
      end)

    densities
    |> Enum.reduce(0, fn density, acc ->
      if acc == 0 do
        [{0, density}]
      else
        [{_, lower} | _] = acc
        [{lower, lower + density} | acc]
      end
    end)
    |> Enum.reverse()
  end

  def generate_tagged_activities(opts \\ []) do
    tag_count = Keyword.get(opts, :tag_count, 20)
    users = Keyword.get(opts, :users, Repo.all(User))
    activity_count = Keyword.get(opts, :count, 200_000)

    intervals = generate_power_intervals(count: tag_count)

    IO.puts(
      "Generating #{activity_count} activities using #{tag_count} different tags of format `tag_n`, starting at tag_0"
    )

    Enum.each(1..activity_count, fn _ ->
      random = :rand.uniform()
      i = Enum.find_index(intervals, fn {lower, upper} -> lower <= random && upper > random end)
      CommonAPI.post(Enum.random(users), %{"status" => "a post with the tag #tag_#{i}"})
    end)
  end

  defp do_generate_activity_with_mention(user, users) do
    mentions_cnt = Enum.random([2, 3, 4, 5])
    with_user = Enum.random([true, false])
    users = Enum.shuffle(users)
    mentions_users = Enum.take(users, mentions_cnt)
    mentions_users = if with_user, do: [user | mentions_users], else: mentions_users

    mentions_str =
      Enum.map(mentions_users, fn user -> "@" <> user.nickname end) |> Enum.join(", ")

    post = %{
      "status" => mentions_str <> "some status with mentions random users"
    }

    CommonAPI.post(Enum.random(users), post)
  end

  defp do_generate_threads(users) do
    thread_length = Enum.random([2, 3, 4, 5])
    actor = Enum.random(users)

    post = %{
      "status" => "Start of the thread"
    }

    {:ok, activity} = CommonAPI.post(actor, post)

    Enum.each(1..thread_length, fn _ ->
      user = Enum.random(users)

      post = %{
        "status" => "@#{actor.nickname} reply to thread",
        "in_reply_to_status_id" => activity.id
      }

      CommonAPI.post(user, post)
    end)
  end

  def generate_remote_activities(user, users) do
    do_generate_remote_activities(user, users)
  end

  defp do_generate_remote_activities(user, users) do
    IO.puts("Starting generating 10000 remote activities...")

    {time, _} =
      :timer.tc(fn ->
        Task.async_stream(
          1..10_000,
          fn i ->
            do_generate_remote_activity(i, user, users)
          end,
          max_concurrency: 10,
          timeout: 30_000
        )
        |> Stream.run()
      end)

    IO.puts("Inserting remote activities take #{to_sec(time)} sec.\n")
  end

  defp do_generate_remote_activity(i, user, users) do
    actor = Enum.random(users)
    %{host: host} = URI.parse(actor.ap_id)
    date = Date.utc_today()
    datetime = DateTime.utc_now()

    map = %{
      "actor" => actor.ap_id,
      "cc" => [actor.follower_address, user.ap_id],
      "context" => "tag:mastodon.example.org,#{date}:objectId=#{i}:objectType=Conversation",
      "id" => actor.ap_id <> "/statuses/#{i}/activity",
      "object" => %{
        "actor" => actor.ap_id,
        "atomUri" => actor.ap_id <> "/statuses/#{i}",
        "attachment" => [],
        "attributedTo" => actor.ap_id,
        "bcc" => [],
        "bto" => [],
        "cc" => [actor.follower_address, user.ap_id],
        "content" =>
          "<p><span class=\"h-card\"><a href=\"" <>
            user.ap_id <>
            "\" class=\"u-url mention\">@<span>" <> user.nickname <> "</span></a></span></p>",
        "context" => "tag:mastodon.example.org,#{date}:objectId=#{i}:objectType=Conversation",
        "conversation" =>
          "tag:mastodon.example.org,#{date}:objectId=#{i}:objectType=Conversation",
        "emoji" => %{},
        "id" => actor.ap_id <> "/statuses/#{i}",
        "inReplyTo" => nil,
        "inReplyToAtomUri" => nil,
        "published" => datetime,
        "sensitive" => true,
        "summary" => "cw",
        "tag" => [
          %{
            "href" => user.ap_id,
            "name" => "@#{user.nickname}@#{host}",
            "type" => "Mention"
          }
        ],
        "to" => ["https://www.w3.org/ns/activitystreams#Public"],
        "type" => "Note",
        "url" => "http://#{host}/@#{actor.nickname}/#{i}"
      },
      "published" => datetime,
      "to" => ["https://www.w3.org/ns/activitystreams#Public"],
      "type" => "Create"
    }

    Pleroma.Web.ActivityPub.ActivityPub.insert(map, false)
  end

  def generate_dms(user, users, opts) do
    IO.puts("Starting generating #{opts[:dms_max]} DMs")
    {time, _} = :timer.tc(fn -> do_generate_dms(user, users, opts) end)
    IO.puts("Inserting dms take #{to_sec(time)} sec.\n")
  end

  defp do_generate_dms(user, users, opts) do
    Task.async_stream(
      1..opts[:dms_max],
      fn _ ->
        do_generate_dm(user, users)
      end,
      max_concurrency: 10,
      timeout: 30_000
    )
    |> Stream.run()
  end

  defp do_generate_dm(user, users) do
    post = %{
      "status" => "@#{user.nickname} some direct message",
      "visibility" => "direct"
    }

    CommonAPI.post(Enum.random(users), post)
  end

  def generate_long_thread(user, users, opts) do
    IO.puts("Starting generating long thread with #{opts[:thread_length]} replies")
    {time, activity} = :timer.tc(fn -> do_generate_long_thread(user, users, opts) end)
    IO.puts("Inserting long thread replies take #{to_sec(time)} sec.\n")
    {:ok, activity}
  end

  defp do_generate_long_thread(user, users, opts) do
    {:ok, %{id: id} = activity} = CommonAPI.post(user, %{"status" => "Start of long thread"})

    Task.async_stream(
      1..opts[:thread_length],
      fn _ -> do_generate_thread(users, id) end,
      max_concurrency: 10,
      timeout: 30_000
    )
    |> Stream.run()

    activity
  end

  defp do_generate_thread(users, activity_id) do
    CommonAPI.post(Enum.random(users), %{
      "status" => "reply to main post",
      "in_reply_to_status_id" => activity_id
    })
  end

  def generate_non_visible_message(user, users) do
    IO.puts("Starting generating 1000 non visible posts")

    {time, _} =
      :timer.tc(fn ->
        do_generate_non_visible_posts(user, users)
      end)

    IO.puts("Inserting non visible posts take #{to_sec(time)} sec.\n")
  end

  defp do_generate_non_visible_posts(user, users) do
    [not_friend | users] = users

    make_friends(user, users)

    Task.async_stream(1..1000, fn _ -> do_generate_non_visible_post(not_friend, users) end,
      max_concurrency: 10,
      timeout: 30_000
    )
    |> Stream.run()
  end

  defp make_friends(_user, []), do: nil

  defp make_friends(user, [friend | users]) do
    {:ok, _} = User.follow(user, friend)
    {:ok, _} = User.follow(friend, user)
    make_friends(user, users)
  end

  defp do_generate_non_visible_post(not_friend, users) do
    post = %{
      "status" => "some non visible post",
      "visibility" => "private"
    }

    {:ok, activity} = CommonAPI.post(not_friend, post)

    thread_length = Enum.random([2, 3, 4, 5])

    Enum.each(1..thread_length, fn _ ->
      user = Enum.random(users)

      post = %{
        "status" => "@#{not_friend.nickname} reply to non visible post",
        "in_reply_to_status_id" => activity.id,
        "visibility" => "private"
      }

      CommonAPI.post(user, post)
    end)
  end
end