Skip to content

Conversation

@beglov
Copy link

@beglov beglov commented May 14, 2024

Время выполнения импорта файла fixtures/large.json менее 30 секунд:

# bin/rake reload_json[fixtures/large.json]
Finish in 28.93

Время рендера страницы автобусы/Самара/Москва после загрузки файла large.json около 200 мс:

Completed 200 OK in 204ms (Views: 69.6ms | ActiveRecord: 9.1ms)

Copy link
Collaborator

@spajic spajic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@@ -0,0 +1,3 @@
DB_HOST=db
DB_USER=postgres
DB_PASSWORD=1q2w3e4r No newline at end of file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬😬😬

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это файл с примером необходимых переменных окружения, значения из него не грузятся))

@from = City.find_by_name!(params[:from])
@to = City.find_by_name!(params[:to])
@trips = Trip.where(from: @from, to: @to).order(:start_time)
@trips = Trip.where(from: @from, to: @to).order(:start_time).preload(bus: :services).load
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

</h1>
<h2>
<%= "В расписании #{@trips.count} рейсов" %>
<%= "В расписании #{@trips.size} рейсов" %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

<% end %>
</ul>
<%= render "delimiter" %>
====================================================
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: есть API для рендеринга коллекций, там можно даже указать шаблон spacer параметром.
https://guides.rubyonrails.org/layouts_and_rendering.html#spacer-templates

Это позволяет находить компромисс между удобством разбиения на partial’ы и не настолько медленно работает как наивный рендеринг в цикле.

Соответственно импорт файла `large.json` будет выполняться сильно дольше и мы не уложимся в бюджет, следовательно
необходимо оптимизировать этот процесс.

Перед внесением изменений я решил покрыть имеющийся функционал тестами чтобы ничего не сломать (см
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

```

`rack-mini-profiler` показывает что наибоьшее время тратится на запрос трипов (выполняется Seq Scan),
ускорить этот запрос можно добавлением индексов на поля `from_id` и `to_id`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 с точки зрения рендеринга именно этой страницы это не супер заметно, но если бы мы подходили с точки зрения снижения нагрузки на БД, это было бы очень полезно

@@ -0,0 +1,5 @@
class AddIndexToTrips < ActiveRecord::Migration[5.2]
def change
add_index :trips, [:from_id, :to_id]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше не забывать concurrently (strong_migrations не даёт забывать)

get URI.encode('/автобусы/Самара/Москва')
assert_response :success

assert_includes @response.body, 'Автобусы Самара – Москва'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants