generating remote activities
authorAlex S <alex.strizhakov@gmail.com>
Thu, 19 Sep 2019 09:59:36 +0000 (12:59 +0300)
committerAlex S <alex.strizhakov@gmail.com>
Thu, 19 Sep 2019 09:59:36 +0000 (12:59 +0300)
lib/load_testing/generator.ex
lib/mix/tasks/pleroma/load_testing.ex

index 61e3d8686869d6fc9f94703ebeda8552a53f48e5..5c5a5c122948b0aae2b5f2cae6b883511eab8a64 100644 (file)
@@ -28,7 +28,8 @@ defmodule Pleroma.LoadTesting.Generator do
       name: "Test ใƒ†ใ‚นใƒˆ User #{i}",
       email: "user#{i}@example.com",
       nickname: "nick#{i}",
-      password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
+      password_hash:
+        "$pbkdf2-sha512$160000$bU.OSFI7H/yqWb5DPEqyjw$uKp/2rmXw12QqnRRTqTtuk2DTwZfF8VR4MYW2xMeIlqPR/UX1nT1CEKVUx2CowFMZ5JON8aDvURrZpJjSgqXrg",
       bio: "Tester Number #{i}",
       info: %{},
       local: remote
@@ -165,6 +166,81 @@ defmodule Pleroma.LoadTesting.Generator do
     end)
   end
 
+  def generate_remote_activities(user, users) do
+    do_generate_remote_activities(user, users)
+  end
+
+  defp do_generate_remote_activities(user, users) do
+    IO.puts("Starting generating 10000 remote activities...")
+
+    {time, _} =
+      :timer.tc(fn ->
+        Task.async_stream(
+          1..10_000,
+          fn i ->
+            do_generate_remote_activity(i, user, users)
+          end,
+          max_concurrency: 10,
+          timeout: 30_000
+        )
+        |> Stream.run()
+      end)
+
+    IO.puts("Inserting remote activities take #{to_sec(time)} sec.\n")
+  end
+
+  defp do_generate_remote_activity(i, user, users) do
+    actor = Enum.random(users)
+    %{host: host} = URI.parse(actor.ap_id)
+    date = Date.utc_today()
+    datetime = DateTime.utc_now()
+
+    map = %{
+      "actor" => actor.ap_id,
+      "cc" => [actor.follower_address, user.ap_id],
+      "context" => "tag:mastodon.example.org,#{date}:objectId=#{i}:objectType=Conversation",
+      "id" => actor.ap_id <> "/statuses/#{i}/activity",
+      "object" => %{
+        "actor" => actor.ap_id,
+        "atomUri" => actor.ap_id <> "/statuses/#{i}",
+        "attachment" => [],
+        "attributedTo" => actor.ap_id,
+        "bcc" => [],
+        "bto" => [],
+        "cc" => [actor.follower_address, user.ap_id],
+        "content" =>
+          "<p><span class=\"h-card\"><a href=\"" <>
+            user.ap_id <>
+            "\" class=\"u-url mention\">@<span>" <> user.nickname <> "</span></a></span></p>",
+        "context" => "tag:mastodon.example.org,#{date}:objectId=#{i}:objectType=Conversation",
+        "conversation" =>
+          "tag:mastodon.example.org,#{date}:objectId=#{i}:objectType=Conversation",
+        "emoji" => %{},
+        "id" => actor.ap_id <> "/statuses/#{i}",
+        "inReplyTo" => nil,
+        "inReplyToAtomUri" => nil,
+        "published" => datetime,
+        "sensitive" => true,
+        "summary" => "cw",
+        "tag" => [
+          %{
+            "href" => user.ap_id,
+            "name" => "@#{user.nickname}@#{host}",
+            "type" => "Mention"
+          }
+        ],
+        "to" => ["https://www.w3.org/ns/activitystreams#Public"],
+        "type" => "Note",
+        "url" => "http://#{host}/@#{actor.nickname}/#{i}"
+      },
+      "published" => datetime,
+      "to" => ["https://www.w3.org/ns/activitystreams#Public"],
+      "type" => "Create"
+    }
+
+    Pleroma.Web.ActivityPub.ActivityPub.insert(map, false)
+  end
+
   def generate_dms(user, users, opts) do
     IO.puts("Starting generating #{opts[:dms_max]} DMs")
     {time, _} = :timer.tc(fn -> do_generate_dms(user, users, opts) end)
index 4fed6bf74ed648f24075eff2082eb3b7d44fb6ae..a7057395d2830549383e3085cfd9695c61dff47e 100644 (file)
@@ -53,32 +53,53 @@ defmodule Mix.Tasks.Pleroma.LoadTesting do
     generate_users(opts)
 
     # main user for queries
-    IO.puts("Fetching main user...")
+    IO.puts("Fetching local main user...")
 
     {time, user} =
       :timer.tc(fn ->
-        Repo.one(from(u in User, order_by: fragment("RANDOM()"), limit: 1))
+        Repo.one(
+          from(u in User, where: u.local == true, order_by: fragment("RANDOM()"), limit: 1)
+        )
       end)
 
     IO.puts("Fetching main user take #{to_sec(time)} sec.\n")
 
-    IO.puts("Fetching users...")
+    IO.puts("Fetching local users...")
 
     {time, users} =
       :timer.tc(fn ->
         Repo.all(
           from(u in User,
             where: u.id != ^user.id,
+            where: u.local == true,
+            order_by: fragment("RANDOM()"),
+            limit: 10
+          )
+        )
+      end)
+
+    IO.puts("Fetching local users take #{to_sec(time)} sec.\n")
+
+    IO.puts("Fetching remote users...")
+
+    {time, remote_users} =
+      :timer.tc(fn ->
+        Repo.all(
+          from(u in User,
+            where: u.id != ^user.id,
+            where: u.local == false,
             order_by: fragment("RANDOM()"),
             limit: 10
           )
         )
       end)
 
-    IO.puts("Fetching users take #{to_sec(time)} sec.\n")
+    IO.puts("Fetching remote users take #{to_sec(time)} sec.\n")
 
     generate_activities(user, users)
 
+    generate_remote_activities(user, remote_users)
+
     generate_dms(user, users, opts)
 
     {:ok, activity} = generate_long_thread(user, users, opts)