Merge branch 'develop' into issue/1383
[akkoma] / benchmarks / mix / tasks / pleroma / benchmarks / tags.ex
1 defmodule Mix.Tasks.Pleroma.Benchmarks.Tags do
2 use Mix.Task
3 alias Pleroma.Repo
4 alias Pleroma.LoadTesting.Generator
5 import Ecto.Query
6
7 def run(_args) do
8 Mix.Pleroma.start_pleroma()
9 activities_count = Repo.aggregate(from(a in Pleroma.Activity), :count, :id)
10
11 if activities_count == 0 do
12 IO.puts("Did not find any activities, cleaning and generating")
13 clean_tables()
14 Generator.generate_users(users_max: 10)
15 Generator.generate_tagged_activities()
16 else
17 IO.puts("Found #{activities_count} activities, won't generate new ones")
18 end
19
20 tags = Enum.map(0..20, fn i -> {"For #tag_#{i}", "tag_#{i}"} end)
21
22 Enum.each(tags, fn {_, tag} ->
23 query =
24 from(o in Pleroma.Object,
25 where: fragment("(?)->'tag' \\? (?)", o.data, ^tag)
26 )
27
28 count = Repo.aggregate(query, :count, :id)
29 IO.puts("Database contains #{count} posts tagged with #{tag}")
30 end)
31
32 user = Repo.all(Pleroma.User) |> List.first()
33
34 Benchee.run(
35 %{
36 "Hashtag fetching, any" => fn tags ->
37 Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
38 %{
39 "any" => tags
40 },
41 user,
42 false
43 )
44 end,
45 # Will always return zero results because no overlapping hashtags are generated.
46 "Hashtag fetching, all" => fn tags ->
47 Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
48 %{
49 "all" => tags
50 },
51 user,
52 false
53 )
54 end
55 },
56 inputs:
57 tags
58 |> Enum.map(fn {_, v} -> v end)
59 |> Enum.chunk_every(2)
60 |> Enum.map(fn tags -> {"For #{inspect(tags)}", tags} end),
61 time: 5
62 )
63
64 Benchee.run(
65 %{
66 "Hashtag fetching" => fn tag ->
67 Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
68 %{
69 "tag" => tag
70 },
71 user,
72 false
73 )
74 end
75 },
76 inputs: tags,
77 time: 5
78 )
79 end
80
81 defp clean_tables do
82 IO.puts("Deleting old data...\n")
83 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE users CASCADE;")
84 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE activities CASCADE;")
85 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE objects CASCADE;")
86 end
87 end