Skip to content
Open

Task4 #133

Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/dubnium
lts/erbium
19 changes: 16 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ gem "sdoc", "~> 1.0", group: :doc
gem "serviceworker-rails", "~> 0.5"
gem "share_meow_client", "~> 0.1"
gem "sitemap_generator", "~> 6.0"
gem "skylight", "~> 3.1"
gem "slack-notifier", "~> 2.3"
gem "sprockets", "~> 3.7"
gem "staccato", "~> 0.5"
Expand All @@ -103,6 +102,22 @@ gem "validate_url", "~> 1.0"
gem "webpacker", "~> 3.5"
gem "webpush", "~> 0.3"

# gem "skylight", "~> 3.1"
# gem 'ddtrace', require: 'ddtrace/auto_instrument'
gem "newrelic_rpm"
gem "scout_apm"

group :development, :local_production do
gem "meta_request"
gem "prometheus_exporter"
gem "rack-mini-profiler", require: false
end

group :development, :local_production, :test do
gem "memory_profiler", "~> 0.9"
gem "stackprof", "~> 0.2", require: false, platforms: :ruby
end

group :development do
gem "better_errors", "~> 2.5"
gem "binding_of_caller", "~> 0.8"
Expand All @@ -123,7 +138,6 @@ group :development, :test do
gem "erb_lint", "~> 0.0", require: false
gem "faker", git: "https://github.com/stympy/faker.git", branch: "master"
gem "fix-db-schema-conflicts", github: "jakeonrails/fix-db-schema-conflicts", branch: "master"
gem "memory_profiler", "~> 0.9"
gem "parallel_tests", "~> 2.27"
gem "pry-byebug", "~> 3.7"
gem "rspec-rails", "~> 3.8"
Expand All @@ -149,7 +163,6 @@ group :test do
gem "shoulda-matchers", "4.0.0.rc1", require: false
gem "simplecov", "~> 0.16", require: false
gem "sinatra", "~> 2.0"
gem "stackprof", "~> 0.2", require: false, platforms: :ruby
gem "stripe-ruby-mock", "~> 2.5", require: "stripe_mock"
gem "test-prof", "~> 0.7"
gem "timecop", "~> 0.9"
Expand Down
37 changes: 25 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ GEM
archive-zip (0.11.0)
io-like (~> 0.3.0)
arel (8.0.0)
ast (2.4.0)
ast (2.4.2)
autoprefixer-rails (9.4.3)
execjs
aws-eventstream (1.0.1)
Expand Down Expand Up @@ -212,7 +212,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.1.5)
concurrent-ruby (1.2.2)
connection_pool (2.2.2)
counter_culture (2.1.2)
activerecord (>= 3.0.0)
Expand Down Expand Up @@ -536,7 +536,7 @@ GEM
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
httpclient (2.8.3)
i18n (1.6.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
inflecto (0.0.2)
Expand Down Expand Up @@ -592,13 +592,16 @@ 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)
mime-types-data (3.2018.0812)
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
minitest (5.18.0)
momentjs-rails (2.20.1)
railties (>= 3.1)
msgpack (1.2.4)
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 @@ -647,10 +651,12 @@ GEM
parallel (1.13.0)
parallel_tests (2.27.1)
parallel
parser (2.6.0.0)
ast (~> 2.4.0)
parser (3.2.2.0)
ast (~> 2.4.1)
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 @@ -813,6 +823,8 @@ GEM
addressable (>= 2.3.5, < 2.6)
faraday (~> 0.8, < 1.0)
sax-machine (1.3.2)
scout_apm (5.3.3)
parser
sdoc (1.0.0)
rdoc (>= 5.0)
selectize-rails (0.12.6)
Expand Down Expand Up @@ -843,10 +855,6 @@ GEM
tilt (~> 2.0)
sitemap_generator (6.0.2)
builder (~> 3.0)
skylight (3.1.4)
skylight-core (= 3.1.4)
skylight-core (3.1.4)
activesupport (>= 4.2.0)
slack-notifier (2.3.2)
smart_properties (1.13.1)
spring (2.0.2)
Expand Down Expand Up @@ -895,7 +903,7 @@ GEM
multipart-post (~> 2.0)
naught (~> 1.0)
simple_oauth (~> 0.3.0)
tzinfo (1.2.5)
tzinfo (1.2.11)
thread_safe (~> 0.1)
uber (0.1.0)
uglifier (4.1.20)
Expand Down Expand Up @@ -932,6 +940,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 +1021,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 +1041,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 All @@ -1049,6 +1062,7 @@ DEPENDENCIES
s3_direct_upload (~> 0.1)
sail (~> 1.5)
sass-rails (~> 5.0)
scout_apm
sdoc (~> 1.0)
selenium-webdriver (~> 3.141)
serviceworker-rails (~> 0.5)
Expand All @@ -1057,7 +1071,6 @@ DEPENDENCIES
simplecov (~> 0.16)
sinatra (~> 2.0)
sitemap_generator (~> 6.0)
skylight (~> 3.1)
slack-notifier (~> 2.3)
spring (~> 2.0)
spring-commands-rspec (~> 1.0)
Expand Down
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,25 @@

run:
docker-compose build && docker-compose run --rm web rails db:setup

siege:
siege -c 10 -t180s http://localhost:3000
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍


siege-short:
siege -c 10 -t30s http://localhost:3000

setup:
bin/setup

dev:
bin/startup

clean:
bin/rails jobs:clear

lprod:
RAILS_ENV=local_production bin/startup

ab: siege-short
sleep 5
ab -n 100 http://127.0.0.1:3000/
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
web: bin/rails s -p 3000
webpacker: ./bin/webpack-dev-server
job: bin/rake jobs:work
prometheus_exporter: bundle exec prometheus_exporter
3 changes: 3 additions & 0 deletions Procfile.lprod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: bin/rails s -p 3000 -e local_production
job: bin/rake jobs:work
prometheus_exporter: bundle exec prometheus_exporter
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class ApplicationController < ActionController::Base
include Pundit
include Instrumentation

before_action do
Rack::MiniProfiler.authorize_request if Rails.env.local_production?
end

def require_http_auth
authenticate_or_request_with_http_basic do |username, password|
username == ApplicationConfig["APP_NAME"] && password == ApplicationConfig["APP_PASSWORD"]
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
11 changes: 9 additions & 2 deletions bin/startup
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def system!(*args)
end

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

if env == 'local_production'
system! 'bundle exec rails assets:precompile'
system! 'foreman start -f Procfile.lprod'
else
system! 'foreman start -f Procfile.dev'
end
end
81 changes: 81 additions & 0 deletions case-study-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Case-study оптимизации

## Подготовка

- Удалось накатить 2.6.3 на M1 с помощью RUBY_CFLAGS="-w", поднял `dev.to` локально (node 12)
- `Skylight` запустить не удалось (не добавляется свое окружение), дальше - без него
- Настроил `NewRelic` для 'development', заюзал siege
- Настроил `Scout` для 'development', понравился больше за счет дружественного UI.
- `Datadog` победить не смог, опять проблемы с arm64 - не стал терять время ибо имеющихся средств достаточно
- Настроил `Prometheus` + `Grafana` для мониторинга локального `dev.to`
- Настроил `local_production` для проекта и подкючил `NewRelic`, `Scout`, `Prometheus` + `Grafana`
- Настроил `rack-mini-profiler` и `rails-panel` для `local_production`

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

Использовались ab, RMP и newrelic
_Initial state_

1. benchmark с помощью `ab` (100 requests)
`development`:
Time taken for tests: 33.005 seconds
Requests per second: 3.03 [#/sec](mean)

`local_production`:
Concurrency Level: 1
Time taken for tests: 18.913 seconds
Complete requests: 100
Failed requests: 14
(Connect: 0, Receive: 0, Length: 14, Exceptions: 0)
Total transferred: 14026571 bytes
HTML transferred: 13922285 bytes
Requests per second: 5.29 [#/sec](mean)
Time per request: 189.132 [ms](mean)
Time per request: 189.132 [ms] (mean, across all concurrent requests)
Transfer rate: 724.25 [Kbytes/sec] received

`Базовая метрика`:
Time taken for tests: 18.913 seconds

2. RMP
"stories#index" - 218.9 ms
проблема - много вызовов паршла `_single_story.html.erb`
Rendering: articles/\_single_story.html.erb (по ~1,5ms отъедает)

3. newrelic
Controller/stories/index: ~240 ms

_Actions_
Корень проблемы: root "stories#index" -> articles/index -> stories/main_stories_feed -> articles/single_story
Кэшируется с юзером, но не без. Добавляем кэширование single_story

_Filnal state_

1. ab

Concurrency Level: 1
Time taken for tests: 11.005 seconds
Complete requests: 100
Failed requests: 20
(Connect: 0, Receive: 0, Length: 20, Exceptions: 0)
Total transferred: 14031359 bytes
HTML transferred: 13927079 bytes
Requests per second: 9.09 [#/sec](mean)
Time per request: 110.053 [ms](mean)
Time per request: 110.053 [ms] (mean, across all concurrent requests)
Transfer rate: 1245.09 [Kbytes/sec] received

2. RMP
"stories#index" - 131.2 ms

3. newrelic
Controller/stories/index: ~120 ms

`Базовая метрика`:
Time taken for tests: 11.005 seconds (- 9 sec)

Данные new relic - в png:
замерялись на `siege -c 10 -t180s http://localhost:3000` + 1 мин. фона

Не сказать что большой прогресс, но ОК.
В целом хорошо зарекомендовали newlelic, RMP и ab. Сборка кривоватая - не все работает, особенно на M1, но инструменты изучить позволяет
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

3 changes: 3 additions & 0 deletions config/cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ production:
adapter: redis
url: redis://localhost:6379/1
channel_prefix: practical_developer_production

local_production:
adapter: async
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def yarn_integrity_enabled?
config.after_initialize do
Bullet.enable = true
Bullet.console = true
Rack::MiniProfiler.config.authorization_mode = :allow_all
end
end

Expand Down
Loading