pleroma/benchmarks/mix/tasks/pleroma/load_testing.ex

135 lines
3.7 KiB
Elixir
Raw Normal View History

2019-08-02 11:33:12 -07:00
defmodule Mix.Tasks.Pleroma.LoadTesting do
use Mix.Task
use Pleroma.LoadTesting.Helper
import Mix.Pleroma
import Pleroma.LoadTesting.Generator
import Pleroma.LoadTesting.Fetcher
@shortdoc "Factory for generation data"
@moduledoc """
Generates data like:
2019-09-19 04:02:27 -07:00
- local/remote users
- local/remote activities with notifications
2019-09-06 06:37:18 -07:00
- direct messages
- long thread
- non visible posts
2019-08-02 11:33:12 -07:00
## Generate data
2019-09-06 06:37:18 -07:00
MIX_ENV=benchmark mix pleroma.load_testing --users 20000 --dms 20000 --thread_length 2000
MIX_ENV=benchmark mix pleroma.load_testing -u 20000 -d 20000 -t 2000
2019-08-02 11:33:12 -07:00
Options:
2019-09-06 06:37:18 -07:00
- `--users NUMBER` - number of users to generate. Defaults to: 20000. Alias: `-u`
- `--dms NUMBER` - number of direct messages to generate. Defaults to: 20000. Alias `-d`
- `--thread_length` - number of messages in thread. Defaults to: 2000. ALias `-t`
2019-08-02 11:33:12 -07:00
"""
2019-09-05 06:01:52 -07:00
@aliases [u: :users, d: :dms, t: :thread_length]
2019-09-04 10:18:11 -07:00
@switches [
users: :integer,
dms: :integer,
2019-09-05 06:01:52 -07:00
thread_length: :integer
2019-09-04 10:18:11 -07:00
]
2019-08-02 11:33:12 -07:00
@users_default 20_000
2019-10-15 06:26:04 -07:00
@dms_default 1_000
2019-09-04 10:18:11 -07:00
@thread_length_default 2_000
2019-08-02 11:33:12 -07:00
def run(args) do
start_pleroma()
2019-09-06 06:37:18 -07:00
Pleroma.Config.put([:instance, :skip_thread_containment], true)
2019-09-04 10:18:11 -07:00
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
2019-08-02 11:33:12 -07:00
2019-09-04 10:18:11 -07:00
users_max = Keyword.get(opts, :users, @users_default)
dms_max = Keyword.get(opts, :dms, @dms_default)
2019-09-05 06:01:52 -07:00
thread_length = Keyword.get(opts, :thread_length, @thread_length_default)
2019-08-02 11:33:12 -07:00
2019-09-04 10:18:11 -07:00
clean_tables()
2019-08-02 11:33:12 -07:00
opts =
2019-09-04 10:18:11 -07:00
Keyword.put(opts, :users_max, users_max)
|> Keyword.put(:dms_max, dms_max)
2019-09-05 06:01:52 -07:00
|> Keyword.put(:thread_length, thread_length)
2019-08-02 11:33:12 -07:00
generate_users(opts)
# main user for queries
2019-09-19 02:59:36 -07:00
IO.puts("Fetching local main user...")
2019-08-02 11:33:12 -07:00
{time, user} =
2019-09-05 06:01:52 -07:00
:timer.tc(fn ->
2019-09-19 02:59:36 -07:00
Repo.one(
from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1)
)
2019-09-05 06:01:52 -07:00
end)
2019-08-02 11:33:12 -07:00
IO.puts("Fetching main user take #{to_sec(time)} sec.\n")
2019-09-19 02:59:36 -07:00
IO.puts("Fetching local users...")
2019-08-02 11:33:12 -07:00
{time, users} =
:timer.tc(fn ->
Repo.all(
from(u in User,
where: u.id != ^user.id,
2019-09-19 02:59:36 -07:00
where: u.local == true,
order_by: fragment("RANDOM()"),
limit: 10
)
)
end)
IO.puts("Fetching local users take #{to_sec(time)} sec.\n")
IO.puts("Fetching remote users...")
{time, remote_users} =
:timer.tc(fn ->
Repo.all(
from(u in User,
where: u.id != ^user.id,
where: u.local == false,
2019-08-02 11:33:12 -07:00
order_by: fragment("RANDOM()"),
limit: 10
)
)
end)
2019-09-19 02:59:36 -07:00
IO.puts("Fetching remote users take #{to_sec(time)} sec.\n")
2019-08-02 11:33:12 -07:00
2019-09-05 06:01:52 -07:00
generate_activities(user, users)
2019-09-19 02:59:36 -07:00
generate_remote_activities(user, remote_users)
2019-08-02 11:33:12 -07:00
2019-09-04 10:18:11 -07:00
generate_dms(user, users, opts)
{:ok, activity} = generate_long_thread(user, users, opts)
2019-09-05 06:01:52 -07:00
generate_non_visible_message(user, users)
2019-09-04 10:18:11 -07:00
2019-09-05 06:01:52 -07:00
IO.puts("Users in DB: #{Repo.aggregate(from(u in User), :count, :id)}")
2019-08-02 11:33:12 -07:00
2019-09-05 06:01:52 -07:00
IO.puts("Activities in DB: #{Repo.aggregate(from(a in Pleroma.Activity), :count, :id)}")
2019-08-02 11:33:12 -07:00
2019-09-05 06:01:52 -07:00
IO.puts("Objects in DB: #{Repo.aggregate(from(o in Pleroma.Object), :count, :id)}")
IO.puts(
"Notifications in DB: #{Repo.aggregate(from(n in Pleroma.Notification), :count, :id)}"
)
2019-08-02 11:33:12 -07:00
2019-09-04 10:18:11 -07:00
fetch_user(user)
2019-08-02 11:33:12 -07:00
query_timelines(user)
query_notifications(user)
2019-09-04 10:18:11 -07:00
query_dms(user)
query_long_thread(user, activity)
2019-09-06 06:37:18 -07:00
Pleroma.Config.put([:instance, :skip_thread_containment], false)
2019-09-04 10:18:11 -07:00
query_timelines(user)
2019-08-02 11:33:12 -07:00
end
defp clean_tables do
2019-09-06 06:37:18 -07:00
IO.puts("Deleting old data...\n")
2019-08-02 11:33:12 -07:00
Ecto.Adapters.SQL.query!(Repo, "TRUNCATE users CASCADE;")
Ecto.Adapters.SQL.query!(Repo, "TRUNCATE activities CASCADE;")
Ecto.Adapters.SQL.query!(Repo, "TRUNCATE objects CASCADE;")
end
end