73796b5f9d560ecb9f80b80bc9d4d1bec761644d
[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" => fn tag ->
37 Pleroma.Web.MastodonAPI.TimelineController.hashtag_fetching(
38 %{
39 "tag" => tag
40 },
41 user,
42 false
43 )
44 end
45 },
46 inputs: tags,
47 time: 5
48 )
49 end
50
51 defp clean_tables do
52 IO.puts("Deleting old data...\n")
53 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE users CASCADE;")
54 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE activities CASCADE;")
55 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE objects CASCADE;")
56 end
57 end