-
Notifications
You must be signed in to change notification settings - Fork 92
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
refact: Stories rendering optimize #146
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,4 @@ package-lock.json | |
|
||
#sitemap | ||
/public/sitemap.xml.gz | ||
.env |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.3.1 | ||
3.4.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
web: bin/rails s -p 3000 | ||
webpacker: ./bin/webpack-dev-server | ||
job: bin/rake jobs:work |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
## Актуальная проблема | ||
Cтраница `StoriesController#index` загружается очень медленно. Необходимо исследовать проблему на возможность оптимизации. | ||
|
||
## Формирование метрики | ||
В качестве метрики я использовал данные из `ab` бенмарка, а именно среднее значение времени затраченного на выполнение запроса | ||
на тесте `ab -n 100 -c 5 http://localhost:3000/` | ||
|
||
## Статистика до оптимизации | ||
Выполнение бенчмарка `ab -n 100 -c 5 http://localhost:3000/` показало значение `Time per request: 4792.132 [ms]` | ||
``` | ||
Concurrency Level: 5 | ||
Time taken for tests: 95.843 seconds | ||
Complete requests: 100 | ||
Failed requests: 62 | ||
(Connect: 0, Receive: 0, Length: 62, Exceptions: 0) | ||
Total transferred: 16150036 bytes | ||
HTML transferred: 16048916 bytes | ||
Requests per second: 1.04 [#/sec] (mean) | ||
Time per request: 4792.132 [ms] (mean) | ||
Time per request: 958.426 [ms] (mean, across all concurrent requests) | ||
Transfer rate: 164.56 [Kbytes/sec] received | ||
|
||
Connection Times (ms) | ||
min mean[+/-sd] median max | ||
Connect: 0 0 0.2 0 2 | ||
Processing: 2766 4644 285.4 4675 4950 | ||
Waiting: 2765 4643 285.4 4674 4950 | ||
Total: 2766 4644 285.4 4675 4950 | ||
|
||
Percentage of the requests served within a certain time (ms) | ||
50% 4675 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4,5с это как-то чересчур медленно, возможно что-то пошло не так |
||
66% 4712 | ||
75% 4757 | ||
80% 4797 | ||
90% 4852 | ||
95% 4915 | ||
98% 4947 | ||
99% 4950 | ||
100% 4950 (longest request) | ||
``` | ||
|
||
## Поиск точек роста | ||
Для поиска точек роста я воспользовался инструментами rack-mini-profiler и Newrelic. | ||
|
||
Newrelic подтверждает значения полученные в ab: | ||
 | ||
|
||
rack-mini-profiler показал большое количество рендеринга `_single_story.html.erb` | ||
 | ||
|
||
## Оптимизация | ||
|
||
Кэшируем проблемный паршиал: | ||
``` | ||
<% cache ["single_story", story] do %> | ||
<%= render "articles/single_story", story: story %> | ||
<% end %> | ||
``` | ||
Проверяем `ab` бенчмарком насколько изменилась метрика: | ||
``` | ||
Server Software: | ||
Server Hostname: localhost | ||
Server Port: 3000 | ||
|
||
Document Path: / | ||
Document Length: 158982 bytes | ||
|
||
Concurrency Level: 5 | ||
Time taken for tests: 20.597 seconds | ||
Complete requests: 100 | ||
Failed requests: 15 | ||
(Connect: 0, Receive: 0, Length: 15, Exceptions: 0) | ||
Total transferred: 15999369 bytes | ||
HTML transferred: 15898184 bytes | ||
Requests per second: 4.86 [#/sec] (mean) | ||
Time per request: 1029.829 [ms] (mean) | ||
Time per request: 205.966 [ms] (mean, across all concurrent requests) | ||
Transfer rate: 758.59 [Kbytes/sec] received | ||
|
||
Connection Times (ms) | ||
min mean[+/-sd] median max | ||
Connect: 0 1 2.0 0 13 | ||
Processing: 240 1010 111.1 1018 1266 | ||
Waiting: 240 1009 111.1 1015 1262 | ||
Total: 241 1011 110.9 1018 1266 | ||
|
||
Percentage of the requests served within a certain time (ms) | ||
50% 1018 | ||
66% 1041 | ||
75% 1060 | ||
80% 1073 | ||
90% 1125 | ||
95% 1158 | ||
98% 1229 | ||
99% 1266 | ||
100% 1266 (longest request) | ||
``` | ||
Получаем значение Time per request: 1029.829 [ms], что почти в 5 раз быстрее первоначальных цифр. | ||
rack-mini-profiler теперь показывает что данные кэшируется и лишний раз паршиал `_single_story.html.erb` не рендерится. | ||
При этом показатели количества комментариев и количества лайков показывают актальное значение, тк сам объект story не закэширован. | ||
|
||
Попробовал профилирование с помощью Stackprof в `around_action` режиме и Ruby-prof с `callgrind`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. чтобы убрать рельсовую инфу можно профилировать какой-то просто кусок бизнес-логики |
||
|
||
## Local_Production env | ||
Создал новый энв `profile`, который максимально приближен к production. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
`ab` бенчмарк показал Time per request: 92.812 [ms], что в 10 раз быстрее последнего оптимизированного варианта. | ||
``` | ||
ab -n 100 -c 5 http://localhost:3000/ | ||
This is ApacheBench, Version 2.3 <$Revision: 1913912 $> | ||
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ | ||
Licensed to The Apache Software Foundation, http://www.apache.org/ | ||
|
||
Benchmarking localhost (be patient).....done | ||
|
||
|
||
Server Software: | ||
Server Hostname: localhost | ||
Server Port: 3000 | ||
|
||
Document Path: / | ||
Document Length: 136409 bytes | ||
|
||
Concurrency Level: 5 | ||
Time taken for tests: 1.856 seconds | ||
Complete requests: 100 | ||
Failed requests: 97 | ||
(Connect: 0, Receive: 0, Length: 97, Exceptions: 0) | ||
Non-2xx responses: 97 | ||
Total transferred: 602228 bytes | ||
HTML transferred: 584118 bytes | ||
Requests per second: 53.87 [#/sec] (mean) | ||
Time per request: 92.812 [ms] (mean) | ||
Time per request: 18.562 [ms] (mean, across all concurrent requests) | ||
Transfer rate: 316.83 [Kbytes/sec] received | ||
|
||
Connection Times (ms) | ||
min mean[+/-sd] median max | ||
Connect: 0 0 0.9 0 7 | ||
Processing: 6 63 123.9 18 760 | ||
Waiting: 6 62 123.8 17 760 | ||
Total: 6 64 123.9 18 760 | ||
|
||
Percentage of the requests served within a certain time (ms) | ||
50% 18 | ||
66% 24 | ||
75% 41 | ||
80% 53 | ||
90% 207 | ||
95% 272 | ||
98% 759 | ||
99% 760 | ||
100% 760 (longest request) | ||
``` |
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.
👍