dc6f3d3fcc53b4c780b32f6ea978263277f9039c
[akkoma] / benchmarks / mix / tasks / pleroma / benchmarks / timelines.ex
1 defmodule Mix.Tasks.Pleroma.Benchmarks.Timelines do
2 use Mix.Task
3 alias Pleroma.Repo
4 alias Pleroma.LoadTesting.Generator
5
6 alias Pleroma.Web.CommonAPI
7
8 def run(_args) do
9 Mix.Pleroma.start_pleroma()
10
11 # Cleaning tables
12 clean_tables()
13
14 [{:ok, user} | users] = Generator.generate_users(users_max: 1000)
15
16 # Let the user make 100 posts
17
18 1..100
19 |> Enum.each(fn i -> CommonAPI.post(user, %{"status" => to_string(i)}) end)
20
21 # Let 10 random users post
22 posts =
23 users
24 |> Enum.take_random(10)
25 |> Enum.map(fn {:ok, random_user} ->
26 {:ok, activity} = CommonAPI.post(random_user, %{"status" => "."})
27 activity
28 end)
29
30 # let our user repeat them
31 posts
32 |> Enum.each(fn activity ->
33 CommonAPI.repeat(activity.id, user)
34 end)
35
36 Benchee.run(
37 %{
38 "user timeline, no followers" => fn reading_user ->
39 conn =
40 Phoenix.ConnTest.build_conn()
41 |> Plug.Conn.assign(:user, reading_user)
42 |> Plug.Conn.assign(:skip_link_headers, true)
43
44 Pleroma.Web.MastodonAPI.AccountController.statuses(conn, %{"id" => user.id})
45 end
46 },
47 inputs: %{"user" => user, "no user" => nil},
48 time: 60
49 )
50
51 users
52 |> Enum.each(fn {:ok, follower} -> Pleroma.User.follow(follower, user) end)
53
54 Benchee.run(
55 %{
56 "user timeline, all following" => fn reading_user ->
57 conn =
58 Phoenix.ConnTest.build_conn()
59 |> Plug.Conn.assign(:user, reading_user)
60 |> Plug.Conn.assign(:skip_link_headers, true)
61
62 Pleroma.Web.MastodonAPI.AccountController.statuses(conn, %{"id" => user.id})
63 end
64 },
65 inputs: %{"user" => user, "no user" => nil},
66 time: 60
67 )
68 end
69
70 defp clean_tables do
71 IO.puts("Deleting old data...\n")
72 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE users CASCADE;")
73 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE activities CASCADE;")
74 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE objects CASCADE;")
75 end
76 end