Merge branch 'develop' into feature/store-statuses-data-inside-flag
[akkoma] / benchmarks / mix / tasks / pleroma / load_testing.ex
1 defmodule Mix.Tasks.Pleroma.LoadTesting do
2 use Mix.Task
3 use Pleroma.LoadTesting.Helper
4 import Mix.Pleroma
5 import Pleroma.LoadTesting.Generator
6 import Pleroma.LoadTesting.Fetcher
7
8 @shortdoc "Factory for generation data"
9 @moduledoc """
10 Generates data like:
11 - local/remote users
12 - local/remote activities with notifications
13 - direct messages
14 - long thread
15 - non visible posts
16
17 ## Generate data
18 MIX_ENV=benchmark mix pleroma.load_testing --users 20000 --dms 20000 --thread_length 2000
19 MIX_ENV=benchmark mix pleroma.load_testing -u 20000 -d 20000 -t 2000
20
21 Options:
22 - `--users NUMBER` - number of users to generate. Defaults to: 20000. Alias: `-u`
23 - `--dms NUMBER` - number of direct messages to generate. Defaults to: 20000. Alias `-d`
24 - `--thread_length` - number of messages in thread. Defaults to: 2000. ALias `-t`
25 """
26
27 @aliases [u: :users, d: :dms, t: :thread_length]
28 @switches [
29 users: :integer,
30 dms: :integer,
31 thread_length: :integer
32 ]
33 @users_default 20_000
34 @dms_default 1_000
35 @thread_length_default 2_000
36
37 def run(args) do
38 start_pleroma()
39 Pleroma.Config.put([:instance, :skip_thread_containment], true)
40 {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
41
42 users_max = Keyword.get(opts, :users, @users_default)
43 dms_max = Keyword.get(opts, :dms, @dms_default)
44 thread_length = Keyword.get(opts, :thread_length, @thread_length_default)
45
46 clean_tables()
47
48 opts =
49 Keyword.put(opts, :users_max, users_max)
50 |> Keyword.put(:dms_max, dms_max)
51 |> Keyword.put(:thread_length, thread_length)
52
53 generate_users(opts)
54
55 # main user for queries
56 IO.puts("Fetching local main user...")
57
58 {time, user} =
59 :timer.tc(fn ->
60 Repo.one(
61 from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1)
62 )
63 end)
64
65 IO.puts("Fetching main user take #{to_sec(time)} sec.\n")
66
67 IO.puts("Fetching local users...")
68
69 {time, users} =
70 :timer.tc(fn ->
71 Repo.all(
72 from(u in User,
73 where: u.id != ^user.id,
74 where: u.local == true,
75 order_by: fragment("RANDOM()"),
76 limit: 10
77 )
78 )
79 end)
80
81 IO.puts("Fetching local users take #{to_sec(time)} sec.\n")
82
83 IO.puts("Fetching remote users...")
84
85 {time, remote_users} =
86 :timer.tc(fn ->
87 Repo.all(
88 from(u in User,
89 where: u.id != ^user.id,
90 where: u.local == false,
91 order_by: fragment("RANDOM()"),
92 limit: 10
93 )
94 )
95 end)
96
97 IO.puts("Fetching remote users take #{to_sec(time)} sec.\n")
98
99 generate_activities(user, users)
100
101 generate_remote_activities(user, remote_users)
102
103 generate_dms(user, users, opts)
104
105 {:ok, activity} = generate_long_thread(user, users, opts)
106
107 generate_non_visible_message(user, users)
108
109 IO.puts("Users in DB: #{Repo.aggregate(from(u in User), :count, :id)}")
110
111 IO.puts("Activities in DB: #{Repo.aggregate(from(a in Pleroma.Activity), :count, :id)}")
112
113 IO.puts("Objects in DB: #{Repo.aggregate(from(o in Pleroma.Object), :count, :id)}")
114
115 IO.puts(
116 "Notifications in DB: #{Repo.aggregate(from(n in Pleroma.Notification), :count, :id)}"
117 )
118
119 fetch_user(user)
120 query_timelines(user)
121 query_notifications(user)
122 query_dms(user)
123 query_long_thread(user, activity)
124 Pleroma.Config.put([:instance, :skip_thread_containment], false)
125 query_timelines(user)
126 end
127
128 defp clean_tables do
129 IO.puts("Deleting old data...\n")
130 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE users CASCADE;")
131 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE activities CASCADE;")
132 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE objects CASCADE;")
133 end
134 end