-
Notifications
You must be signed in to change notification settings - Fork 115
Task-3 #96
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
Open
sSmok
wants to merge
6
commits into
hardcode-dev:master
Choose a base branch
from
sSmok:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Task-3 #96
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
<h1> | ||
<%= "Автобусы #{@from.name} – #{@to.name}" %> | ||
</h1> | ||
<h2> | ||
<%= "В расписании #{@trips.count} рейсов" %> | ||
</h2> | ||
<%= cache([@from.name, @to.name]) do %> | ||
<h1> | ||
<%= "Автобусы #{@from.name} – #{@to.name}" %> | ||
</h1> | ||
<h2> | ||
<%= "В расписании #{@trips.count} рейсов" %> | ||
</h2> | ||
|
||
<% @trips.each do |trip| %> | ||
<ul> | ||
<%= render "trip", trip: trip %> | ||
<% if trip.bus.services.present? %> | ||
<%= render "services", services: trip.bus.services %> | ||
<% end %> | ||
</ul> | ||
<%= render "delimiter" %> | ||
<% end %> | ||
<% @trips.each do |trip| %> | ||
<ul> | ||
<li><%= "Отправление: #{trip.start_time}" %></li> | ||
<li><%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %></li> | ||
<li><%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %></li> | ||
<li><%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %></li> | ||
<li><%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %></li> | ||
<% if trip.bus.services.present? %> | ||
<li>Сервисы в автобусе:</li> | ||
<ul> | ||
<% trip.bus.services.each do |service| %> | ||
<li><%= "#{service.name}" %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
</ul> | ||
==================================================== | ||
<% end %> | ||
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Case-study оптимизации | ||
|
||
## Оптимизация импорта | ||
Изначально время импорта файла large.json занимало ~450s | ||
Я переписал импорт сразу с потоковой записью в `Postgres`. После рефакторинга время обарботки файла large.json стало ~13s | ||
|
||
## Оптимизация рендеринга страницы | ||
Изначальное время рендеринга страницы с данными из файла large.json было ~34000ms. | ||
|
||
### Правка №1 | ||
bullet и rack mini profiler указывали на проблему n+1 | ||
Добавил includes к формированию списка поездок: | ||
@trips = Trip.includes(bus: :services).where(from: @from, to: @to).order(:start_time) | ||
После рефакторинга время рендера страницы изменилось с ~34000ms до ~24000ms | ||
|
||
### Правка №2 | ||
Rails panel указвыл, что практически все время тратиться на рендеринг шаблонов. Решил убрать все шаблоны и формировать html сразу на одной странице, тк он достаточно простой. | ||
После рефакторинга время рендера страницы изменилось с ~24000ms до ~6000ms | ||
|
||
### Правка №3 | ||
Решил установить pg_hero и проанализировать запросы. pg_hero не предлавагал добавлять никакие индексы и все было в норме. Но я добавил индыксы на имя города, и составной индекс для трипов по from_id, to_id. Какой-то прибавки к скорости рендера это не дало, все осталось в пределах погрешности. | ||
|
||
### Правка №4 | ||
Принял решение добавить кэширование. Закэшировал сразу всю страницу index. Время рендера закешированной версии страницы изменилось с ~6000ms до ~250ms |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
В данном случае да, так как почти всё время уходит на рендеринг паршлов
Но если смотреть с точки зрения оптимизации БД - очень полезно
В принципе можно оставлять паршлы, но юзать рендеринг коллекций, и там можно даже задать шаблон разделителя. Всё в одном файле быстрее работает, но всё-таки не так удобно для разработки (в реальном мире)
https://guides.rubyonrails.org/layouts_and_rendering.html#spacer-templates