diff --git a/.gitignore b/.gitignore index 53c5b710..e8d2c25f 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,8 @@ latest.dump /public/packs-test /node_modules config/database.yml +/config/newrelic.yml + # Ignore storybook static site generation storybook-static/ diff --git a/Gemfile b/Gemfile index b107eb92..2c197203 100644 --- a/Gemfile +++ b/Gemfile @@ -62,6 +62,7 @@ gem "jquery-rails", "~> 4.3" gem "kaminari", "~> 1.1" gem "libhoney", "~> 1.11" gem "liquid", "~> 4.0" +gem 'newrelic_rpm' gem "nokogiri", "~> 1.10" gem "octokit", "~> 4.13" gem "omniauth", "~> 1.9" @@ -75,6 +76,7 @@ gem "pundit", "~> 2.0" gem "pusher", "~> 1.3" gem "pusher-push-notifications", "~> 1.0" gem "rack-host-redirect", "~> 1.3" +gem 'rack-mini-profiler' gem "rack-timeout", "~> 0.5" gem "rails", "~> 5.1.6" gem "rails-assets-airbrake-js-client", "~> 1.5", source: "https://rails-assets.org" diff --git a/Gemfile.lock b/Gemfile.lock index 130d7472..42de0915 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -646,9 +646,12 @@ GEM net-smtp (0.5.1) net-protocol netrc (0.11.0) + newrelic_rpm (9.17.0) nio4r (2.7.4) nokogiri (1.15.7-aarch64-linux) racc (~> 1.4) + nokogiri (1.15.7-x86_64-linux) + racc (~> 1.4) notiffany (0.1.3) nenv (~> 0.1) shellany (~> 0.0) @@ -726,6 +729,8 @@ GEM rack (2.2.11) rack-host-redirect (1.3.0) rack + rack-mini-profiler (3.3.1) + rack (>= 1.2.0) rack-protection (2.2.4) rack rack-proxy (0.7.7) @@ -1011,6 +1016,7 @@ GEM PLATFORMS aarch64-linux + x86_64-linux DEPENDENCIES actionpack-action_caching (~> 1.2) @@ -1081,6 +1087,7 @@ DEPENDENCIES liquid (~> 4.0) memory_profiler (~> 0.9) nakayoshi_fork + newrelic_rpm nokogiri (~> 1.10) octokit (~> 4.13) omniauth (~> 1.9) @@ -1098,6 +1105,7 @@ DEPENDENCIES pusher (~> 1.3) pusher-push-notifications (~> 1.0) rack-host-redirect (~> 1.3) + rack-mini-profiler rack-timeout (~> 0.5) rails (~> 5.1.6) rails-assets-airbrake-js-client (~> 1.5)! diff --git a/Screenshot_2025-03-11_14-14-41.png b/Screenshot_2025-03-11_14-14-41.png new file mode 100644 index 00000000..a975d962 Binary files /dev/null and b/Screenshot_2025-03-11_14-14-41.png differ diff --git a/app/views/stories/_main_stories_feed.html.erb b/app/views/stories/_main_stories_feed.html.erb index b6cd0a65..1bb23c18 100644 --- a/app/views/stories/_main_stories_feed.html.erb +++ b/app/views/stories/_main_stories_feed.html.erb @@ -55,7 +55,9 @@ <% if !user_signed_in? && i == 4 %> <%= render "stories/sign_in_invitation" %> <% end %> - <%= render "articles/single_story", story: story %> + <%= cache ['single_story', story], expires_in: 24.hours do %> + <%= render "articles/single_story", story: story %> + <% end %> <% end %> <% end %> <% if @stories.size > 1 %> diff --git a/case-study.md b/case-study.md new file mode 100644 index 00000000..1e896542 --- /dev/null +++ b/case-study.md @@ -0,0 +1,41 @@ +# Case-study оптимизации + +ApacheBench: +``` +Concurrency Level: 5 +Time taken for tests: 47.600 seconds +Complete requests: 100 +Failed requests: 0 +Total transferred: 15984100 bytes +HTML transferred: 15937400 bytes +Requests per second: 2.10 [#/sec] (mean) +Time per request: 2380.021 [ms] (mean) +Time per request: 476.004 [ms] (mean, across all concurrent requests) +Transfer rate: 327.93 [Kbytes/sec] received + +``` +В задаче имеется подсказка, что заметное время занимает рендеринг partial-ов single_story.html.erb. Однако я этого не увидела + +**rack-mini-profiler** показывает первую загрузку страницы за 7480ms, повторные же в пределах 700ms из-за встроенных кэшей +Наибольшее время (2250ms) `articles/_sidebar.html.erb` + +**New Relic** +Web transactions time - 2600ms из них ruby занимает 2008ms. +most time consuming - stories#index +stories#index - 790ms +layouts/application.html.erb - 624ms +single_story.html.erb - 206ms +articles/sidebar.html.erb - 120ms + +Я закэшировала single_story, тогда +ApacheBench +``` +Requests per second: 7.16 [#/sec] (mean) +Time per request: 698.126 [ms] (mean) +Time per request: 139.625 [ms] (mean, across all concurrent requests) +Transfer rate: 1121.84 [Kbytes/sec] received +``` + +**rack-mini-profiler** показывает время загрузки 350ms +Время загрузки по **NewRelic** тоже уменьшилось, см скриншот. +Но почему это работает - я не понимаю, ведь single_story не выглядела как основная точка роста.