5.8 KiB · text History bbb2a48
defmodule VigilWeb.PerformanceLiveTest do
  use VigilWeb.ConnCase, async: true

  import Phoenix.LiveViewTest
  import Vigil.OrganizationsFixtures
  import Vigil.PerformanceFixtures

  alias Vigil.Performance

  setup :register_and_log_in_user

  setup %{user: user} do
    org = organization_fixture(user)
    project = project_fixture(org_scope(user, org), %{"name" => "web"})
    base = ~p"/orgs/#{org.slug}/projects/web/performance"
    %{org: org, project: project, base: base}
  end

  defp group(project, name),
    do: project.id |> Performance.list_groups(query: name) |> Enum.find(&(&1.name == name))

  describe "list" do
    test "is linked from the project's issues page", %{conn: conn, org: org} do
      {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/projects/web")
      assert has_element?(view, "#project-performance")
    end

    test "explains how to turn tracing on when there's nothing", %{conn: conn, base: base} do
      {:ok, view, _} = live(conn, base)
      assert has_element?(view, "#performance-empty")
      assert has_element?(view, "#setup-elixir", "traces_sample_rate")
      assert has_element?(view, "#setup-python", "traces_sample_rate")
      refute has_element?(view, "#txn-search")
    end

    test "shows transactions with their stats", %{conn: conn, base: base, project: project} do
      transaction_fixture(project, ms: 100)
      transaction_fixture(project, ms: 300, status: "internal_error")
      transaction_fixture(project, name: "GET /other", ms: 20)
      g = group(project, "GET /users/:id")

      {:ok, view, _} = live(conn, base)
      refute has_element?(view, "#performance-empty")
      assert has_element?(view, "#txn-#{g.id}", "GET /users/:id")
      assert has_element?(view, "#txn-#{g.id}", "http.server")
      assert has_element?(view, "#txn-#{g.id} .badge", "GET")
      assert has_element?(view, "#txn-#{g.id} .txn-count", "2")
      assert has_element?(view, "#txn-#{g.id} .txn-avg", "200ms")
      assert has_element?(view, "#txn-#{g.id} .txn-errors", "50.0%")
      assert has_element?(view, "#txn-#{g.id} a[href$='/performance/#{g.id}']")
    end

    test "sorts and searches through the URL", %{conn: conn, base: base, project: project} do
      transaction_fixture(project, name: "GET /fast", ms: 5)
      transaction_fixture(project, name: "GET /fast", ms: 5)
      transaction_fixture(project, name: "GET /slow", ms: 900)
      fast = group(project, "GET /fast")
      slow = group(project, "GET /slow")

      {:ok, view, _} = live(conn, base)
      assert order(view) == [fast.id, slow.id]

      view |> element("#sort-p95") |> render_click()
      assert_patch(view, base <> "?sort=p95")
      assert has_element?(view, "#sort-p95.tab-active")
      assert order(view) == [slow.id, fast.id]

      view |> form("#txn-search", %{q: "fast"}) |> render_change()
      assert_patch(view, base <> "?q=fast&sort=p95")
      assert order(view) == [fast.id]

      view |> form("#txn-search", %{q: "nothing"}) |> render_change()
      assert has_element?(view, "#transactions-empty", "nothing")
    end

    test "refreshes live as transactions arrive", %{conn: conn, base: base, project: project} do
      transaction_fixture(project, name: "GET /old")
      {:ok, view, _} = live(conn, base)

      transaction_fixture(project, name: "GET /new")
      new = group(project, "GET /new")
      refute has_element?(view, "#txn-#{new.id}")

      send(view.pid, {:transactions, project.id})
      send(view.pid, :refresh)
      assert has_element?(view, "#txn-#{new.id}")
    end
  end

  defp order(view) do
    view
    |> render()
    |> LazyHTML.from_fragment()
    |> LazyHTML.query("#transactions > li[id^='txn-']")
    |> LazyHTML.attribute("id")
    |> Enum.map(&String.replace_prefix(&1, "txn-", ""))
  end

  describe "transaction detail" do
    setup %{project: project} do
      transaction_fixture(project, ms: 100)
      transaction_fixture(project, ms: 150)
      slow_trace = transaction_fixture(project, ms: 900, status: "internal_error")
      %{group: group(project, "GET /users/:id"), slow_trace: slow_trace}
    end

    test "stats and charts", %{conn: conn, base: base, group: g} do
      {:ok, view, _} = live(conn, base <> "/#{g.id}")
      assert has_element?(view, "#txn-name", "GET /users/:id")
      assert has_element?(view, "#stat-count", "3")
      assert has_element?(view, "#stat-avg", "383ms")
      assert has_element?(view, "#stat-p50")
      assert has_element?(view, "#stat-p95")
      assert has_element?(view, "#stat-p99")
      assert has_element?(view, "#stat-tpm")
      assert has_element?(view, "#stat-error-rate", "33.3%")

      assert has_element?(view, "#throughput-total", "3")
      assert has_element?(view, "#throughput-bars #throughput-bar-23")
      assert has_element?(view, "#throughput-table")
      assert has_element?(view, "#duration-bars")
      assert has_element?(view, "#duration-table")
    end

    test "samples, recent or slowest, link to their traces", ctx do
      %{conn: conn, base: base, group: g, slow_trace: slow_trace, org: org} = ctx
      {:ok, view, _} = live(conn, base <> "/#{g.id}")
      assert has_element?(view, "#samples-recent.tab-active")
      assert samples(view) |> length() == 3

      view |> element("#samples-slowest") |> render_click()
      assert_patch(view, base <> "/#{g.id}?samples=slowest")
      assert has_element?(view, "#samples-slowest.tab-active")
      [first | _] = samples(view)
      assert first =~ "/orgs/#{org.slug}/traces/#{slow_trace}"
    end

    test "an unknown group goes back to the list", %{conn: conn, base: base} do
      assert {:error, {:live_redirect, %{to: ^base}}} =
               live(conn, base <> "/#{Ecto.UUID.generate()}")
    end
  end

  defp samples(view) do
    view
    |> render()
    |> LazyHTML.from_fragment()
    |> LazyHTML.query("#samples > li[id^='sample-'] a")
    |> LazyHTML.attribute("href")
  end
end