fetcher.ex 6.61 KB
Newer Older
Alexander Strizhakov's avatar
Alexander Strizhakov committed
1 2 3 4
defmodule Pleroma.LoadTesting.Fetcher do
  use Pleroma.LoadTesting.Helper

  def fetch_user(user) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
5 6 7 8 9 10
    Benchee.run(%{
      "By id" => fn -> Repo.get_by(User, id: user.id) end,
      "By ap_id" => fn -> Repo.get_by(User, ap_id: user.ap_id) end,
      "By email" => fn -> Repo.get_by(User, email: user.email) end,
      "By nickname" => fn -> Repo.get_by(User, nickname: user.nickname) end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
11 12 13
  end

  def query_timelines(user) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
14
    home_timeline_params = %{
Alexander Strizhakov's avatar
Alexander Strizhakov committed
15 16 17 18 19 20 21 22
      "count" => 20,
      "with_muted" => true,
      "type" => ["Create", "Announce"],
      "blocking_user" => user,
      "muting_user" => user,
      "user" => user
    }

Alexander Strizhakov's avatar
Alexander Strizhakov committed
23
    mastodon_public_timeline_params = %{
Alexander Strizhakov's avatar
Alexander Strizhakov committed
24 25 26 27 28 29 30 31 32
      "count" => 20,
      "local_only" => true,
      "only_media" => "false",
      "type" => ["Create", "Announce"],
      "with_muted" => "true",
      "blocking_user" => user,
      "muting_user" => user
    }

Alexander Strizhakov's avatar
Alexander Strizhakov committed
33
    mastodon_federated_timeline_params = %{
Alexander Strizhakov's avatar
Alexander Strizhakov committed
34 35 36 37 38 39 40 41
      "count" => 20,
      "only_media" => "false",
      "type" => ["Create", "Announce"],
      "with_muted" => "true",
      "blocking_user" => user,
      "muting_user" => user
    }

Alexander Strizhakov's avatar
Alexander Strizhakov committed
42 43 44 45 46 47 48 49
    Benchee.run(%{
      "User home timeline" => fn ->
        Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
          [user.ap_id | user.following],
          home_timeline_params
        )
      end,
      "User mastodon public timeline" => fn ->
Alexander Strizhakov's avatar
Alexander Strizhakov committed
50 51 52
        Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
          mastodon_public_timeline_params
        )
Alexander Strizhakov's avatar
Alexander Strizhakov committed
53 54
      end,
      "User mastodon federated public timeline" => fn ->
Alexander Strizhakov's avatar
Alexander Strizhakov committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
          mastodon_federated_timeline_params
        )
      end
    })

    home_activities =
      Pleroma.Web.ActivityPub.ActivityPub.fetch_activities(
        [user.ap_id | user.following],
        home_timeline_params
      )

    public_activities =
      Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(mastodon_public_timeline_params)

    public_federated_activities =
      Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities(
        mastodon_federated_timeline_params
      )

    Benchee.run(%{
      "Rendering home timeline" => fn ->
        Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
          activities: home_activities,
          for: user,
          as: :activity
        })
      end,
      "Rendering public timeline" => fn ->
        Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
          activities: public_activities,
          for: user,
          as: :activity
        })
      end,
      "Rendering public federated timeline" => fn ->
        Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
          activities: public_federated_activities,
          for: user,
          as: :activity
        })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
96 97
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
98 99 100
  end

  def query_notifications(user) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
101 102 103 104 105 106 107 108 109 110 111
    without_muted_params = %{"count" => "20", "with_muted" => "false"}
    with_muted_params = %{"count" => "20", "with_muted" => "true"}

    Benchee.run(%{
      "Notifications without muted" => fn ->
        Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)
      end,
      "Notifications with muted" => fn ->
        Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    without_muted_notifications =
      Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, without_muted_params)

    with_muted_notifications =
      Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(user, with_muted_params)

    Benchee.run(%{
      "Render notifications without muted" => fn ->
        Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
          notifications: without_muted_notifications,
          for: user
        })
      end,
      "Render notifications with muted" => fn ->
        Pleroma.Web.MastodonAPI.NotificationView.render("index.json", %{
          notifications: with_muted_notifications,
          for: user
        })
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
133
  end
Alexander Strizhakov's avatar
Alexander Strizhakov committed
134

Alexander Strizhakov's avatar
Alexander Strizhakov committed
135 136 137 138 139 140 141 142 143
  def query_dms(user) do
    params = %{
      "count" => "20",
      "with_muted" => "true",
      "type" => "Create",
      "blocking_user" => user,
      "user" => user,
      visibility: "direct"
    }
Alexander Strizhakov's avatar
Alexander Strizhakov committed
144

Alexander Strizhakov's avatar
Alexander Strizhakov committed
145 146 147 148 149 150 151 152 153 154
    Benchee.run(%{
      "Direct messages with muted" => fn ->
        Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
        |> Pleroma.Pagination.fetch_paginated(params)
      end,
      "Direct messages without muted" => fn ->
        Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
        |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

    dms_with_muted =
      Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
      |> Pleroma.Pagination.fetch_paginated(params)

    dms_without_muted =
      Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_query([user.ap_id], params)
      |> Pleroma.Pagination.fetch_paginated(Map.put(params, "with_muted", false))

    Benchee.run(%{
      "Rendering dms with muted" => fn ->
        Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
          activities: dms_with_muted,
          for: user,
          as: :activity
        })
      end,
      "Rendering dms without muted" => fn ->
        Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
          activities: dms_without_muted,
          for: user,
          as: :activity
        })
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
180 181 182
  end

  def query_long_thread(user, activity) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
183
    Benchee.run(%{
Alexander Strizhakov's avatar
Alexander Strizhakov committed
184 185 186
      "Fetch main post" => fn ->
        Pleroma.Activity.get_by_id_with_object(activity.id)
      end,
Alexander Strizhakov's avatar
Alexander Strizhakov committed
187
      "Fetch context of main post" => fn ->
Alexander Strizhakov's avatar
Alexander Strizhakov committed
188 189 190 191
        Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
          activity.data["context"],
          %{
            "blocking_user" => user,
Alexander Strizhakov's avatar
Alexander Strizhakov committed
192 193
            "user" => user,
            "exclude_id" => activity.id
Alexander Strizhakov's avatar
Alexander Strizhakov committed
194 195
          }
        )
Alexander Strizhakov's avatar
Alexander Strizhakov committed
196 197
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    activity = Pleroma.Activity.get_by_id_with_object(activity.id)

    context =
      Pleroma.Web.ActivityPub.ActivityPub.fetch_activities_for_context(
        activity.data["context"],
        %{
          "blocking_user" => user,
          "user" => user,
          "exclude_id" => activity.id
        }
      )

    Benchee.run(%{
      "Render status" => fn ->
Alexander Strizhakov's avatar
Alexander Strizhakov committed
213
        Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{
Alexander Strizhakov's avatar
Alexander Strizhakov committed
214 215 216 217
          activity: activity,
          for: user
        })
      end,
Alexander Strizhakov's avatar
Alexander Strizhakov committed
218
      "Render context" => fn ->
Alexander Strizhakov's avatar
Alexander Strizhakov committed
219 220 221 222 223 224 225 226 227
        Pleroma.Web.MastodonAPI.StatusView.render(
          "index.json",
          for: user,
          activities: context,
          as: :activity
        )
        |> Enum.reverse()
      end
    })
Alexander Strizhakov's avatar
Alexander Strizhakov committed
228 229
  end
end