Skip to content

Commit

Permalink
Add performance configuration for Lighthouse testing (decidim#11244)
Browse files Browse the repository at this point in the history
Implements a new environment variable configuration for the
development environment to support performance improvements at
the development environment for performance testing.
  • Loading branch information
ahukkanen committed Jul 21, 2023
1 parent 6203795 commit 8b61be0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci_performance_metrics_monitoring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ env:
RUBY_VERSION: 3.1.1
NODE_VERSION: 16.13.0
RAILS_ENV: development
RAILS_BOOST_PERFORMANCE: "true"
WEBPACKER_RUNTIME_COMPILE: "false"

concurrency:
Expand Down Expand Up @@ -84,6 +85,9 @@ jobs:
- run: bundle exec rails server -b localhost -d
name: Run Rails server
working-directory: ./development_app/
- run: bundle exec rails decidim:lighthouse:warmup
name: Warmup the cache at the configured lighthouse urls
working-directory: ./development_app/
- name: Audit URLs using Lighthouse
uses: treosh/lighthouse-ci-action@v10
with:
Expand Down
3 changes: 3 additions & 0 deletions decidim-dev/lib/decidim/dev/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Engine < ::Rails::Engine
engine_name "decidim_dev"

initializer "decidim_dev.tools" do
# Disable if the boost performance mode is enabled
next if Rails.application.config.try(:boost_performance)

ActiveSupport.on_load(:action_controller) { include Decidim::Dev::NeedsDevelopmentTools }
end

Expand Down
36 changes: 29 additions & 7 deletions decidim-dev/lib/tasks/lighthouse_report.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,39 @@ namespace :decidim do
# Because seeds make urls dynamic, this task updates the lighthouse configuration
# to add dynamically the urls to check.

host = "http://localhost:3000"
urls = ["/"]
urls << Decidim::ResourceLocatorPresenter.new(Decidim::ParticipatoryProcess.published.first).path
urls << Decidim::ResourceLocatorPresenter.new(Decidim::Meetings::Meeting.published.first).path
urls << Decidim::ResourceLocatorPresenter.new(Decidim::Proposals::Proposal.published.first).path

# Update lighthouse configuration with the urls
lighthouse_rc_path = Rails.root.join("../.lighthouserc.json")
lighthouserc = JSON.parse(File.read(lighthouse_rc_path))
lighthouserc["ci"]["collect"]["url"] = urls.map { |url| "#{host}#{url}" }
lighthouserc["ci"]["collect"]["url"] = lighthouse_urls
File.write(lighthouse_rc_path, lighthouserc.to_json)
end

desc "Warms up the URLs to be requested"
task warmup: :environment do
lighthouse_urls.each do |url|
uri = URI.parse(url)
connection = Net::HTTP.new(uri.host, uri.port)
connection.start do |http|
puts "Warming up #{uri.path}"
response = http.get(uri.path)
puts "--HTTP STATUS: #{response.code}"
end
end
end

private

def lighthouse_urls
host = "http://localhost:3000"
lighthouse_paths.map { |path| "#{host}#{path}" }
end

def lighthouse_paths
["/"].tap do |urls|
urls << Decidim::ResourceLocatorPresenter.new(Decidim::ParticipatoryProcess.published.first).path
urls << Decidim::ResourceLocatorPresenter.new(Decidim::Meetings::Meeting.published.first).path
urls << Decidim::ResourceLocatorPresenter.new(Decidim::Proposals::Proposal.published.first).path
end
end
end
end
20 changes: 20 additions & 0 deletions decidim-generators/lib/decidim/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ def decidim_initializer
"# config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]"
end

def dev_performance_config
gsub_file "config/environments/development.rb", /^end\n$/, <<~CONFIG
# Performance configs for local testing
if ENV.fetch("RAILS_BOOST_PERFORMANCE", false).to_s == "true"
# Indicate boost performance mode
config.boost_performance = true
# Enable caching and eager load
config.eager_load = true
config.cache_classes = true
# Logging
config.log_level = :info
config.action_view.logger = nil
# Compress the HTML responses with gzip
config.middleware.use Rack::Deflater
end
end
CONFIG
end

def authorization_handler
return unless options[:demo]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

if defined?(Bullet)
if defined?(Bullet) && !Rails.application.config.try(:boost_performance)
Rails.application.config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

if Rails.env.development?
if Rails.env.development? && !Rails.application.config.try(:boost_performance)
require "rack-mini-profiler"

# initialization is skipped so trigger it
Expand Down

0 comments on commit 8b61be0

Please sign in to comment.