temp commit
This commit is contained in:
parent
fe7cf44c40
commit
db690bede9
5 changed files with 403 additions and 0 deletions
116
lib/load_testing/fetcher.ex
Normal file
116
lib/load_testing/fetcher.ex
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
defmodule Pleroma.LoadTesting.Fetcher do
|
||||
use Pleroma.LoadTesting.Helper
|
||||
|
||||
def fetch_user(user) do
|
||||
IO.puts("=================================")
|
||||
|
||||
{time, _value} = :timer.tc(fn -> Repo.get_by(User, id: user.id) end)
|
||||
|
||||
IO.puts("Query user by id: #{to_sec(time)} sec.")
|
||||
|
||||
{time, _value} =
|
||||
:timer.tc(fn ->
|
||||
Repo.get_by(User, ap_id: user.ap_id)
|
||||
end)
|
||||
|
||||
IO.puts("Query user by ap_id: #{to_sec(time)} sec.")
|
||||
|
||||
{time, _value} =
|
||||
:timer.tc(fn ->
|
||||
Repo.get_by(User, email: user.email)
|
||||
end)
|
||||
|
||||
IO.puts("Query user by email: #{to_sec(time)} sec.")
|
||||
|
||||
{time, _value} = :timer.tc(fn -> Repo.get_by(User, nickname: user.nickname) end)
|
||||
|
||||
IO.puts("Query user by nickname: #{to_sec(time)} sec.")
|
||||
end
|
||||
|
||||
def query_timelines(user) do
|
||||
IO.puts("\n=================================")
|
||||
|
||||
params = %{
|
||||
"count" => 20,
|
||||
"with_muted" => true,
|
||||
"type" => ["Create", "Announce"],
|
||||
"blocking_user" => user,
|
||||
"muting_user" => user,
|
||||
"user" => user
|
||||
}
|
||||
|
||||
{time, _} =
|
||||
:timer.tc(fn ->
|
||||
ActivityPub.ActivityPub.fetch_activities([user.ap_id | user.following], params)
|
||||
end)
|
||||
|
||||
IO.puts("Query user home timeline: #{to_sec(time)} sec.")
|
||||
|
||||
params = %{
|
||||
"count" => 20,
|
||||
"local_only" => true,
|
||||
"only_media" => "false",
|
||||
"type" => ["Create", "Announce"],
|
||||
"with_muted" => "true",
|
||||
"blocking_user" => user,
|
||||
"muting_user" => user
|
||||
}
|
||||
|
||||
{time, _} =
|
||||
:timer.tc(fn ->
|
||||
ActivityPub.ActivityPub.fetch_public_activities(params)
|
||||
end)
|
||||
|
||||
IO.puts("Query user mastodon public timeline: #{to_sec(time)} sec.")
|
||||
|
||||
params = %{
|
||||
"count" => 20,
|
||||
"only_media" => "false",
|
||||
"type" => ["Create", "Announce"],
|
||||
"with_muted" => "true",
|
||||
"blocking_user" => user,
|
||||
"muting_user" => user
|
||||
}
|
||||
|
||||
{time, _} =
|
||||
:timer.tc(fn ->
|
||||
ActivityPub.ActivityPub.fetch_public_activities(params)
|
||||
end)
|
||||
|
||||
IO.puts("Query user mastodon federated public timeline: #{to_sec(time)} sec.")
|
||||
end
|
||||
|
||||
def query_notifications(user) do
|
||||
IO.puts("\n=================================")
|
||||
params = %{"count" => "20", "with_muted" => "false"}
|
||||
|
||||
{time, _} =
|
||||
:timer.tc(fn -> Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, params) end)
|
||||
|
||||
IO.puts("Query user notifications with out muted: #{to_sec(time)} sec.")
|
||||
|
||||
params = %{"count" => "20", "with_muted" => "true"}
|
||||
|
||||
{time, _} =
|
||||
:timer.tc(fn -> Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, params) end)
|
||||
|
||||
IO.puts("Query user notifications with muted: #{to_sec(time)} sec.")
|
||||
end
|
||||
|
||||
def query_long_thread(user, activity) do
|
||||
IO.puts("\n=================================")
|
||||
|
||||
{time, replies} =
|
||||
:timer.tc(fn ->
|
||||
Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
|
||||
activity.data["context"],
|
||||
%{
|
||||
"blocking_user" => user,
|
||||
"user" => user
|
||||
}
|
||||
)
|
||||
end)
|
||||
|
||||
IO.puts("Query long thread with #{length(replies)} replies: #{to_sec(time)} sec.")
|
||||
end
|
||||
end
|
||||
83
lib/load_testing/generator.ex
Normal file
83
lib/load_testing/generator.ex
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
defmodule Pleroma.LoadTesting.Generator do
|
||||
use Pleroma.LoadTesting.Helper
|
||||
|
||||
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
|
||||
min = Keyword.get(opts, :users_min, 1)
|
||||
max = Keyword.get(opts, :users_max)
|
||||
|
||||
query =
|
||||
"INSERT INTO \"users\" (\"ap_id\",\"bio\",\"email\",\"follower_address\",\"following\",\"following_address\",\"info\",
|
||||
\"local\",\"name\",\"nickname\",\"password_hash\",\"tags\",\"id\",\"inserted_at\",\"updated_at\") VALUES \n"
|
||||
|
||||
users =
|
||||
Task.async_stream(
|
||||
min..max,
|
||||
&generate_user_data(&1),
|
||||
max_concurrency: 10,
|
||||
timeout: 30_000
|
||||
)
|
||||
|> Enum.reduce("", fn {:ok, data}, acc -> acc <> data <> ", \n" end)
|
||||
|
||||
query = query <> String.replace_trailing(users, ", \n", ";")
|
||||
|
||||
Ecto.Adapters.SQL.query!(Repo, query)
|
||||
end
|
||||
|
||||
defp generate_user_data(i) do
|
||||
user = %User{
|
||||
name: "Test テスト User #{i}",
|
||||
email: "user#{i}@example.com",
|
||||
nickname: "nick#{i}",
|
||||
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
|
||||
bio: "Tester Number #{i}",
|
||||
info: %{}
|
||||
}
|
||||
|
||||
user = %{
|
||||
user
|
||||
| ap_id: User.ap_id(user),
|
||||
follower_address: User.ap_followers(user),
|
||||
following_address: User.ap_following(user),
|
||||
following: [User.ap_id(user)]
|
||||
}
|
||||
|
||||
"('#{user.ap_id}', '#{user.bio}', '#{user.email}', '#{user.follower_address}', '{#{
|
||||
user.following
|
||||
}}', '#{user.following_address}', '#{Jason.encode!(user.info)}', '#{user.local}', '#{
|
||||
user.name
|
||||
}', '#{user.nickname}', '#{user.password_hash}', '{#{user.tags}}', uuid_generate_v4(), NOW(), NOW())"
|
||||
end
|
||||
|
||||
def generate_activities(users, opts) do
|
||||
IO.puts("Starting generating #{opts[:activities_max]} activities...")
|
||||
{time, _} = :timer.tc(fn -> do_generate_activities(users, opts) end)
|
||||
IO.puts("Inserting activities take #{to_sec(time)} sec.\n")
|
||||
end
|
||||
|
||||
defp do_generate_activities(users, opts) do
|
||||
Task.async_stream(
|
||||
1..opts[:activities_max],
|
||||
fn _ ->
|
||||
do_generate_activity(users, opts)
|
||||
end,
|
||||
max_concurrency: 10,
|
||||
timeout: 30_000
|
||||
)
|
||||
|> Stream.run()
|
||||
end
|
||||
|
||||
defp do_generate_activity(users, opts) do
|
||||
status =
|
||||
if opts[:mention],
|
||||
do: "some status with @#{opts[:mention].nickname}",
|
||||
else: "some status"
|
||||
|
||||
Pleroma.Web.CommonAPI.post(Enum.random(users), %{"status" => status})
|
||||
end
|
||||
end
|
||||
16
lib/load_testing/helper.ex
Normal file
16
lib/load_testing/helper.ex
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
defmodule Pleroma.LoadTesting.Helper do
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
import Ecto.Query
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Notification
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub
|
||||
alias Pleroma.Web.CommonAPI
|
||||
|
||||
defp to_sec(microseconds), do: microseconds / 1_000_000
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue