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

"Оптимизация" бэкенда #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ group :production do
end

gem "psych", "< 4"
gem "skylight"
# gem "skylight"

gem "actionpack-action_caching", "~> 1.2"
gem "active_record_union", "~> 1.3"
Expand Down Expand Up @@ -68,6 +68,7 @@ gem "omniauth", "~> 1.9"
gem "omniauth-github", "~> 1.3"
gem "omniauth-twitter", "~> 1.4"
gem "pg", "~> 1.1"
gem "pghero", "2.8.3"
gem "pry", "~> 0.12"
gem "pry-rails", "~> 0.3"
gem "puma", "~> 3.12"
Expand Down Expand Up @@ -115,6 +116,7 @@ group :development do
gem "guard", "~> 2.15", require: false
gem "guard-livereload", "~> 2.5", require: false
gem "guard-rspec", "~> 4.7", require: false
gem "rack-mini-profiler", "3.3.1", require: false
gem "rb-fsevent", "~> 0.10", require: false
gem "web-console", "~> 3.7"
end
Expand Down
14 changes: 9 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ GEM
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)
Expand Down Expand Up @@ -697,6 +699,8 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.9)
pghero (2.8.3)
activerecord (>= 5)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -726,6 +730,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)
Expand Down Expand Up @@ -908,10 +914,6 @@ GEM
tilt (~> 2.0)
sitemap_generator (6.3.0)
builder (~> 3.0)
skylight (4.3.2)
skylight-core (= 4.3.2)
skylight-core (4.3.2)
activesupport (>= 4.2.0)
slack-notifier (2.4.0)
smart_properties (1.17.0)
snaky_hash (2.0.1)
Expand Down Expand Up @@ -1011,6 +1013,7 @@ GEM

PLATFORMS
aarch64-linux
x86_64-linux

DEPENDENCIES
actionpack-action_caching (~> 1.2)
Expand Down Expand Up @@ -1088,6 +1091,7 @@ DEPENDENCIES
omniauth-twitter (~> 1.4)
parallel_tests (~> 2.27)
pg (~> 1.1)
pghero (= 2.8.3)
pry (~> 0.12)
pry-byebug (~> 3.7)
pry-rails (~> 0.3)
Expand All @@ -1098,6 +1102,7 @@ DEPENDENCIES
pusher (~> 1.3)
pusher-push-notifications (~> 1.0)
rack-host-redirect (~> 1.3)
rack-mini-profiler (= 3.3.1)
rack-timeout (~> 0.5)
rails (~> 5.1.6)
rails-assets-airbrake-js-client (~> 1.5)!
Expand Down Expand Up @@ -1126,7 +1131,6 @@ DEPENDENCIES
simplecov (~> 0.16)
sinatra (~> 2.0)
sitemap_generator (~> 6.0)
skylight
slack-notifier (~> 2.3)
sprockets (~> 3.7)
staccato (~> 0.5)
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("single-story-#{story.cache_key}") do %>
<%= render "articles/single_story", story: story %>
<% end %>
<% end %>
<% end %>
<% if @stories.size > 1 %>
Expand Down
37 changes: 37 additions & 0 deletions case_study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Оптимизация бэкенда

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

Если использовать `story.cache_key`, то по идее не заморозятся, т.к. счётчик берётся из поля модели (`cache_counter`).
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍


Без кэша:

```
Concurrency Level: 5
Time taken for tests: 19.728 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 15922500 bytes
HTML transferred: 15875800 bytes
Requests per second: 5.07 [#/sec] (mean)
Time per request: 986.417 [ms] (mean)
Time per request: 197.283 [ms] (mean, across all concurrent requests)
Transfer rate: 788.17 [Kbytes/sec] received
```

С кэшем:

```
Concurrency Level: 5
Time taken for tests: 11.927 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 15788300 bytes
HTML transferred: 15741600 bytes
Requests per second: 8.38 [#/sec] (mean)
Time per request: 596.328 [ms] (mean)
Time per request: 119.266 [ms] (mean, across all concurrent requests)
Transfer rate: 1292.77 [Kbytes/sec] received
```

Как будто бы помогло. Проверяла с отключеным rack-mini-profiler (и т.д. штуками)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

8 changes: 8 additions & 0 deletions config/initializers/rack_mini_profiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

if Rails.env.development?
require "rack-mini-profiler"

# The initializer was required late, so initialize it manually.
Rack::MiniProfilerRails.initialize!(Rails.application)
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
registrations: "registrations"
}

mount PgHero::Engine, at: "pghero"

authenticated :user, ->(user) { user.admin? } do
mount DelayedJobWeb, at: "/delayed_job"
end
Expand Down