aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/mix
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2020-03-08 10:38:35 +0000
committerrinpatch <rinpatch@sdf.org>2020-03-08 10:38:35 +0000
commite8493431bfc16977e43715bf8bdb09ac46580028 (patch)
tree2e1a0068583ca62870a15590c4ccba237999f51a /benchmarks/mix
parent42f76306e7fe69fc51be00285a4fef8569f54989 (diff)
parente7c49d51d724680c1f19888ea409687733753c7b (diff)
downloadpleroma-2.0.0.tar.gz
Merge branch 'release/2.0.0' into 'stable'v2.0.0
Release/2.0.0 See merge request pleroma/pleroma!2273
Diffstat (limited to 'benchmarks/mix')
-rw-r--r--benchmarks/mix/tasks/pleroma/benchmarks/tags.ex87
-rw-r--r--benchmarks/mix/tasks/pleroma/load_testing.ex138
2 files changed, 225 insertions, 0 deletions
diff --git a/benchmarks/mix/tasks/pleroma/benchmarks/tags.ex b/benchmarks/mix/tasks/pleroma/benchmarks/tags.ex
new file mode 100644
index 000000000..fd1506907
--- /dev/null
+++ b/benchmarks/mix/tasks/pleroma/benchmarks/tags.ex
@@ -0,0 +1,87 @@
+defmodule Mix.Tasks.Pleroma.Benchmarks.Tags do
+ use Mix.Task
+ alias Pleroma.Repo
+ alias Pleroma.LoadTesting.Generator
+ import Ecto.Query
+
+ def run(_args) do
+ Mix.Pleroma.start_pleroma()
+ activities_count = Repo.aggregate(from(a in Pleroma.Activity), :count, :id)
+
+ if activities_count == 0 do
+ IO.puts("Did not find any activities, cleaning and generating")
+ clean_tables()
+ Generator.generate_users(users_max: 10)
+ Generator.generate_tagged_activities()
+ else
+ IO.puts("Found #{activities_count} activities, won't generate new ones")
+ end
+
+ tags = Enum.map(0..20, fn i -> {"For #tag_#{i}", "tag_#{i}"} end)
+
+ Enum.each(tags, fn {_, tag} ->
+ query =
+ from(o in Pleroma.Object,
+ where: fragment("(?)->'tag' \\? (?)", o.data, ^tag)
+ )
+
+ count = Repo.aggregate(query, :count, :id)
+ IO.puts("Database contains #{count} posts tagged with #{tag}")
+ end)
+
+ user = Repo.all(Pleroma.User) |> List.first()
+
+ Benchee.run(
+ %{
+ "Hashtag fetching, any" => fn tags ->
+ Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
+ %{
+ "any" => tags
+ },
+ user,
+ false
+ )
+ end,
+ # Will always return zero results because no overlapping hashtags are generated.
+ "Hashtag fetching, all" => fn tags ->
+ Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
+ %{
+ "all" => tags
+ },
+ user,
+ false
+ )
+ end
+ },
+ inputs:
+ tags
+ |> Enum.map(fn {_, v} -> v end)
+ |> Enum.chunk_every(2)
+ |> Enum.map(fn tags -> {"For #{inspect(tags)}", tags} end),
+ time: 5
+ )
+
+ Benchee.run(
+ %{
+ "Hashtag fetching" => fn tag ->
+ Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
+ %{
+ "tag" => tag
+ },
+ user,
+ false
+ )
+ end
+ },
+ inputs: tags,
+ time: 5
+ )
+ end
+
+ defp clean_tables do
+ IO.puts("Deleting old data...\n")
+ 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
diff --git a/benchmarks/mix/tasks/pleroma/load_testing.ex b/benchmarks/mix/tasks/pleroma/load_testing.ex
new file mode 100644
index 000000000..0a751adac
--- /dev/null
+++ b/benchmarks/mix/tasks/pleroma/load_testing.ex
@@ -0,0 +1,138 @@
+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:
+ - local/remote users
+ - local/remote activities with notifications
+ - direct messages
+ - long thread
+ - non visible posts
+
+ ## Generate data
+ 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
+
+ Options:
+ - `--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`
+ """
+
+ @aliases [u: :users, d: :dms, t: :thread_length]
+ @switches [
+ users: :integer,
+ dms: :integer,
+ thread_length: :integer
+ ]
+ @users_default 20_000
+ @dms_default 1_000
+ @thread_length_default 2_000
+
+ def run(args) do
+ start_pleroma()
+ Pleroma.Config.put([:instance, :skip_thread_containment], true)
+ {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
+
+ users_max = Keyword.get(opts, :users, @users_default)
+ dms_max = Keyword.get(opts, :dms, @dms_default)
+ thread_length = Keyword.get(opts, :thread_length, @thread_length_default)
+
+ clean_tables()
+
+ opts =
+ Keyword.put(opts, :users_max, users_max)
+ |> Keyword.put(:dms_max, dms_max)
+ |> Keyword.put(:thread_length, thread_length)
+
+ generate_users(opts)
+
+ # main user for queries
+ IO.puts("Fetching local main user...")
+
+ {time, user} =
+ :timer.tc(fn ->
+ Repo.one(
+ from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1)
+ )
+ end)
+
+ IO.puts("Fetching main user take #{to_sec(time)} sec.\n")
+
+ IO.puts("Fetching local users...")
+
+ {time, users} =
+ :timer.tc(fn ->
+ Repo.all(
+ from(u in User,
+ where: u.id != ^user.id,
+ 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,
+ order_by: fragment("RANDOM()"),
+ limit: 10
+ )
+ )
+ end)
+
+ IO.puts("Fetching remote users take #{to_sec(time)} sec.\n")
+
+ generate_activities(user, users)
+
+ generate_remote_activities(user, remote_users)
+
+ generate_like_activities(
+ user, Pleroma.Repo.all(Pleroma.Activity.Queries.by_type("Create"))
+ )
+
+ generate_dms(user, users, opts)
+
+ {:ok, activity} = generate_long_thread(user, users, opts)
+
+ generate_non_visible_message(user, users)
+
+ IO.puts("Users in DB: #{Repo.aggregate(from(u in User), :count, :id)}")
+
+ IO.puts("Activities in DB: #{Repo.aggregate(from(a in Pleroma.Activity), :count, :id)}")
+
+ 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)}"
+ )
+
+ fetch_user(user)
+ query_timelines(user)
+ query_notifications(user)
+ query_dms(user)
+ query_long_thread(user, activity)
+ Pleroma.Config.put([:instance, :skip_thread_containment], false)
+ query_timelines(user)
+ end
+
+ defp clean_tables do
+ IO.puts("Deleting old data...\n")
+ 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