Nothing much just a build up of things
[anni] / benchmarks / mix / tasks / pleroma / benchmark.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Benchmark do
6   @shortdoc "Benchmarks"
7   @moduledoc """
8   Benchmark tasks available:
9   
10   adapters 
11   render_timeline
12   search
13   tag
14
15   MIX_ENV=benchmark mix pleroma.benchmark adapters
16   """
17
18   use Mix.Task
19   import Mix.Pleroma
20
21   def run(["search"]) do
22     start_pleroma()
23
24     Benchee.run(%{
25       "search" => fn ->
26         Pleroma.Activity.search(nil, "cofe")
27       end
28     })
29   end
30
31   def run(["tag"]) do
32     start_pleroma()
33
34     Benchee.run(%{
35       "tag" => fn ->
36         %{"type" => "Create", "tag" => "cofe"}
37         |> Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities()
38       end
39     })
40   end
41
42   def run(["render_timeline", nickname | _] = args) do
43     start_pleroma()
44     user = Pleroma.User.get_by_nickname(nickname)
45
46     activities =
47       %{}
48       |> Map.put("type", ["Create", "Announce"])
49       |> Map.put("blocking_user", user)
50       |> Map.put("muting_user", user)
51       |> Map.put("user", user)
52       |> Map.put("limit", 4096)
53       |> Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities()
54       |> Enum.reverse()
55
56     inputs = %{
57       "1 activity" => Enum.take_random(activities, 1),
58       "10 activities" => Enum.take_random(activities, 10),
59       "20 activities" => Enum.take_random(activities, 20),
60       "40 activities" => Enum.take_random(activities, 40),
61       "80 activities" => Enum.take_random(activities, 80)
62     }
63
64     inputs =
65       if Enum.at(args, 2) == "extended" do
66         Map.merge(inputs, %{
67           "200 activities" => Enum.take_random(activities, 200),
68           "500 activities" => Enum.take_random(activities, 500),
69           "2000 activities" => Enum.take_random(activities, 2000),
70           "4096 activities" => Enum.take_random(activities, 4096)
71         })
72       else
73         inputs
74       end
75
76     Benchee.run(
77       %{
78         "Standard rendering" => fn activities ->
79           Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{
80             activities: activities,
81             for: user,
82             as: :activity
83           })
84         end
85       },
86       inputs: inputs
87     )
88   end
89
90   def run(["adapters"]) do
91     start_pleroma()
92
93     :ok =
94       Pleroma.Gun.Conn.open(
95         "https://httpbin.org/stream-bytes/1500",
96         :gun_connections
97       )
98
99     Process.sleep(1_500)
100
101     Benchee.run(
102       %{
103         "Without conn and without pool" => fn ->
104           {:ok, %Tesla.Env{}} =
105             Pleroma.HTTP.get("https://httpbin.org/stream-bytes/1500", [],
106               pool: :no_pool,
107               receive_conn: false
108             )
109         end,
110         "Without conn and with pool" => fn ->
111           {:ok, %Tesla.Env{}} =
112             Pleroma.HTTP.get("https://httpbin.org/stream-bytes/1500", [], receive_conn: false)
113         end,
114         "With reused conn and without pool" => fn ->
115           {:ok, %Tesla.Env{}} =
116             Pleroma.HTTP.get("https://httpbin.org/stream-bytes/1500", [], pool: :no_pool)
117         end,
118         "With reused conn and with pool" => fn ->
119           {:ok, %Tesla.Env{}} = Pleroma.HTTP.get("https://httpbin.org/stream-bytes/1500")
120         end
121       },
122       parallel: 10
123     )
124   end
125 end