+ defp delete_filter(filter), do: Repo.delete(filter)
+
+ defp fetch_timelines(user) do
+ fetch_home_timeline(user)
+ fetch_home_timeline_with_filter(user)
+ fetch_direct_timeline(user)
+ fetch_public_timeline(user)
+ fetch_public_timeline_with_filter(user)
+ fetch_public_timeline(user, :with_blocks)
+ fetch_public_timeline(user, :local)
+ fetch_public_timeline(user, :tag)
+ fetch_notifications(user)
+ fetch_favourites(user)
+ fetch_long_thread(user)
+ fetch_timelines_with_reply_filtering(user)
+ end
+
+ defp render_views(user) do
+ render_timelines(user)
+ render_long_thread(user)
+ end
+
+ defp opts_for_home_timeline(user) do
+ %{
+ blocking_user: user,
+ count: "20",
+ muting_user: user,
+ type: ["Create", "Announce"],
+ user: user,
+ with_muted: true
+ }
+ end
+
+ defp fetch_home_timeline(user, title_end \\ "") do
+ opts = opts_for_home_timeline(user)
+
+ recipients = [user.ap_id | User.following(user)]
+
+ first_page_last =
+ ActivityPub.fetch_activities(recipients, opts) |> Enum.reverse() |> List.last()
+
+ second_page_last =
+ ActivityPub.fetch_activities(recipients, Map.put(opts, :max_id, first_page_last.id))
+ |> Enum.reverse()
+ |> List.last()
+
+ third_page_last =
+ ActivityPub.fetch_activities(recipients, Map.put(opts, :max_id, second_page_last.id))
+ |> Enum.reverse()
+ |> List.last()
+
+ forth_page_last =
+ ActivityPub.fetch_activities(recipients, Map.put(opts, :max_id, third_page_last.id))
+ |> Enum.reverse()
+ |> List.last()
+
+ title = "home timeline " <> title_end
+
+ Benchee.run(
+ %{
+ title => fn opts -> ActivityPub.fetch_activities(recipients, opts) end
+ },
+ inputs: %{
+ "1 page" => opts,
+ "2 page" => Map.put(opts, :max_id, first_page_last.id),
+ "3 page" => Map.put(opts, :max_id, second_page_last.id),
+ "4 page" => Map.put(opts, :max_id, third_page_last.id),
+ "5 page" => Map.put(opts, :max_id, forth_page_last.id),
+ "1 page only media" => Map.put(opts, :only_media, true),
+ "2 page only media" =>
+ Map.put(opts, :max_id, first_page_last.id) |> Map.put(:only_media, true),
+ "3 page only media" =>
+ Map.put(opts, :max_id, second_page_last.id) |> Map.put(:only_media, true),
+ "4 page only media" =>
+ Map.put(opts, :max_id, third_page_last.id) |> Map.put(:only_media, true),
+ "5 page only media" =>
+ Map.put(opts, :max_id, forth_page_last.id) |> Map.put(:only_media, true)
+ },
+ formatters: formatters()
+ )
+ end
+
+ defp fetch_home_timeline_with_filter(user) do
+ {:ok, filter} = create_filter(user)
+
+ fetch_home_timeline(user, "with filters")
+
+ delete_filter(filter)
+ end
+
+ defp opts_for_direct_timeline(user) do
+ %{
+ visibility: "direct",
+ blocking_user: user,
+ count: "20",
+ type: "Create",
+ user: user,
+ with_muted: true
+ }
+ end
+
+ defp fetch_direct_timeline(user) do
+ recipients = [user.ap_id]
+
+ opts = opts_for_direct_timeline(user)
+
+ first_page_last =
+ recipients
+ |> ActivityPub.fetch_activities_query(opts)
+ |> Pagination.fetch_paginated(opts)
+ |> List.last()
+
+ opts2 = Map.put(opts, :max_id, first_page_last.id)
+
+ second_page_last =
+ recipients
+ |> ActivityPub.fetch_activities_query(opts2)
+ |> Pagination.fetch_paginated(opts2)
+ |> List.last()
+
+ opts3 = Map.put(opts, :max_id, second_page_last.id)
+
+ third_page_last =
+ recipients
+ |> ActivityPub.fetch_activities_query(opts3)
+ |> Pagination.fetch_paginated(opts3)
+ |> List.last()
+
+ opts4 = Map.put(opts, :max_id, third_page_last.id)
+
+ forth_page_last =
+ recipients
+ |> ActivityPub.fetch_activities_query(opts4)
+ |> Pagination.fetch_paginated(opts4)
+ |> List.last()
+
+ Benchee.run(
+ %{
+ "direct timeline" => fn opts ->
+ ActivityPub.fetch_activities_query(recipients, opts) |> Pagination.fetch_paginated(opts)
+ end
+ },
+ inputs: %{
+ "1 page" => opts,
+ "2 page" => opts2,
+ "3 page" => opts3,
+ "4 page" => opts4,
+ "5 page" => Map.put(opts4, :max_id, forth_page_last.id)
+ },
+ formatters: formatters()
+ )
+ end
+
+ defp opts_for_public_timeline(user) do
+ %{
+ type: ["Create", "Announce"],
+ local_only: false,
+ blocking_user: user,
+ muting_user: user