-
Notifications
You must be signed in to change notification settings - Fork 114
Optimization trips import and render #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
spajic
left a comment
There was a problem hiding this 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 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬😬😬
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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} рейсов" %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
| <% end %> | ||
| </ul> | ||
| <%= render "delimiter" %> | ||
| ==================================================== |
There was a problem hiding this comment.
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` будет выполняться сильно дольше и мы не уложимся в бюджет, следовательно | ||
| необходимо оптимизировать этот процесс. | ||
|
|
||
| Перед внесением изменений я решил покрыть имеющийся функционал тестами чтобы ничего не сломать (см |
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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] | |||
There was a problem hiding this comment.
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, 'Автобусы Самара – Москва' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Время выполнения импорта файла
fixtures/large.jsonменее 30 секунд:Время рендера страницы
автобусы/Самара/Москвапосле загрузки файлаlarge.jsonоколо 200 мс: