Skip to content
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

HW 4 #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

HW 4 #132

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Envfile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ variable :TRENDING_TAGS, :String, default: "git,beginners"
variable :FACEBOOK_PIXEL_ID, :String, default: "Optional"
variable :SMARTY_STREETS_WEB_KEY, :String, default: "Optional"

variable :SKYLIGHT_AUTHENTICATION, :String, default: "Optional"
variable :NEWRELIC_AUTHENTICATION, :String, default: "Optional"

variable :MINI_PROFILER, :String, default: "Optional"


group :production do
variable :SECRET_KEY_BASE, :String
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ gem "uglifier", "~> 4.1"
gem "validate_url", "~> 1.0"
gem "webpacker", "~> 3.5"
gem "webpush", "~> 0.3"
gem 'newrelic_rpm'
gem 'rack-mini-profiler'
gem "prometheus_exporter"

group :development do
gem "better_errors", "~> 2.5"
Expand All @@ -115,6 +118,7 @@ group :development do
gem "guard-rspec", "~> 4.7", require: false
gem "rb-fsevent", "~> 0.10", require: false
gem "web-console", "~> 3.7"
gem 'meta_request'
end

group :development, :test do
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ GEM
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
memory_profiler (0.9.12)
meta_request (0.7.4)
rack-contrib (>= 1.1, < 3)
railties (>= 3.0.0, < 7.1)
method_source (0.9.2)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
Expand All @@ -612,6 +615,7 @@ GEM
net-http-persistent (3.0.0)
connection_pool (~> 2.2)
netrc (0.11.0)
newrelic_rpm (9.1.0)
nio4r (2.3.1)
nokogiri (1.10.1)
mini_portile2 (~> 2.4.0)
Expand Down Expand Up @@ -651,6 +655,8 @@ GEM
ast (~> 2.4.0)
pg (1.1.4)
powerpack (0.1.2)
prometheus_exporter (2.0.8)
webrick
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand All @@ -675,8 +681,12 @@ GEM
pusher-signature (0.1.8)
raabro (1.1.6)
rack (2.0.6)
rack-contrib (2.3.0)
rack (~> 2.0)
rack-host-redirect (1.3.0)
rack
rack-mini-profiler (3.0.0)
rack (>= 1.2.0)
rack-protection (2.0.4)
rack
rack-proxy (0.6.5)
Expand Down Expand Up @@ -932,6 +942,7 @@ GEM
webpush (0.3.2)
hkdf (~> 0.2)
jwt
webrick (1.8.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down Expand Up @@ -1012,14 +1023,17 @@ DEPENDENCIES
libhoney (~> 1.11)
liquid (~> 4.0)
memory_profiler (~> 0.9)
meta_request
nakayoshi_fork
newrelic_rpm
nokogiri (~> 1.10)
octokit (~> 4.13)
omniauth (~> 1.9)
omniauth-github (~> 1.3)
omniauth-twitter (~> 1.4)
parallel_tests (~> 2.27)
pg (~> 1.1)
prometheus_exporter
pry (~> 0.12)
pry-byebug (~> 3.7)
pry-rails (~> 0.3)
Expand All @@ -1029,6 +1043,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)!
Expand Down
3 changes: 3 additions & 0 deletions Procfile.profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: bin/rails s -p 3000 -e profile
job: bin/rake jobs:work
prometheus_exporter: bundle exec prometheus_exporter -a prometheus/custom_collector.rb
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception, prepend: true

before_action do
Rack::MiniProfiler.authorize_request if cookies[:mini_profiler] == ApplicationConfig["MINI_PROFILER"]
end

include Pundit
include Instrumentation

Expand Down
4 changes: 3 additions & 1 deletion app/views/stories/_main_stories_feed.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
<% if !user_signed_in? && i == 4 %>
<%= render "stories/sign_in_invitation" %>
<% end %>
<%= render "articles/single_story", story: story %>
<% cache(story) do %>
<%= render "articles/single_story", story: story %>
<% end %>
<% end %>
<% end %>
<% if @stories.size > 1 %>
Expand Down
10 changes: 8 additions & 2 deletions bin/startup
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def system!(*args)
end

chdir APP_ROOT do
puts "== STARTING UP =="
system! "foreman start -f Procfile.dev"
env = ENV.fetch('RAILS_ENV', 'development')
puts "== STARTING UP #{env}=="

if env == 'profile'
system! "foreman start -f Procfile.profile"
else
system! "foreman start -f Procfile.dev"
end
end
128 changes: 128 additions & 0 deletions case-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
## Цели

- Попрактиковаться в настройке мониторинга
- Попрактиковаться в поиске возможностей для оптимизации

- Попрактиковаться в проверке гипотез и обосновании предложений по оптимизации
- Познакомиться с интересным живым Rails open-source проектом

## Настройка систем профилирования

- Подключил NewRelic
- Подключил Skylight
- Подключил Prometheus + Grafana

- Настроил rack-mini-profiler
- Настроил rails-panel

## Настройка local_production окружения

Добавил новый environment profile - для профилирования в среде максимально приближенной к Production

## Нагрузочное тестирование

Для проверки гипотиз и выстраивания максимально эффективного фидбэк лупа benchmark осуществлял при помощи утилиты ApacheBench.

## Оптимизация

Все инструменты мониторинга показывают, что самой горячей точкой является главная страница, StoriesController#index.

В частности, заметное время занимает рендеринг partial-ов \_single_story.html.erb.

Статистика 200 запросов в ab:

```Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000

Document Path: /
Document Length: 142263 bytes

Concurrency Level: 5
Time taken for tests: 10.545 seconds
Complete requests: 200
Failed requests: 0
Total transferred: 28537200 bytes
HTML transferred: 28452600 bytes
Requests per second: 18.97 [#/sec] (mean)
Time per request: 263.633 [ms] (mean)
Time per request: 52.727 [ms] (mean, across all concurrent requests)
Transfer rate: 2642.73 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 3
Processing: 84 258 92.6 260 536
Waiting: 84 247 89.2 246 498
Total: 85 258 92.6 260 536

Percentage of the requests served within a certain time (ms)
50% 260
66% 301
75% 328
80% 339
90% 378
95% 412
98% 455
99% 489
100% 536 (longest request)
```

Скрин NewRelic до оптимизации: https://disk.yandex.ru/i/ocwtqArpKiEQbQ

Рассмотрел гипотезу о том, что можно закешировать <%= render "articles/single_story", story: story %> в \_main_stories_feed.html.erb и это даст заметный эффект.

Статистика 200 запросов в ab после отимизации:

```Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:
Server Hostname: 127.0.0.1
Server Port: 3000

Document Path: /
Document Length: 142311 bytes

Concurrency Level: 5
Time taken for tests: 6.603 seconds
Complete requests: 200
Failed requests: 0
Total transferred: 28546800 bytes
HTML transferred: 28462200 bytes
Requests per second: 30.29 [#/sec] (mean)
Time per request: 165.066 [ms] (mean)
Time per request: 33.013 [ms] (mean, across all concurrent requests)
Transfer rate: 4222.22 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 2
Processing: 57 162 59.0 154 384
Waiting: 55 153 56.2 143 374
Total: 57 162 59.0 154 384

Percentage of the requests served within a certain time (ms)
50% 154
66% 173
75% 184
80% 203
90% 254
95% 289
98% 319
99% 347
100% 384 (longest request)
```

Изменение графика в NewRelic помле оптимизации https://disk.yandex.ru/i/d40doyOEVx7P2Q

Время ответа для 200 запросов уменьшилось с 10.545 до 6.603 секунд
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def yarn_integrity_enabled?

Rails.application.configure do
# Verifies that versions and hashed value of the package contents in the project's package.json
config.webpacker.check_yarn_integrity = yarn_integrity_enabled?
config.webpacker.check_yarn_integrity = false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Лучше не выключать. Если падает, значит что-то не совсем так


# Settings specified here will take precedence over those in config/application.rb.

Expand Down
Loading