Skip to content

Commit 871b432

Browse files
committed
Add Rails optimization notes & examples
1 parent e64ed50 commit 871b432

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+895
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
# Ignore data files
88
*.csv
99

10+
# Ignore profiler output
11+
*.prof
12+

010_rp_1.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'date'
2+
require 'ruby-prof'
3+
4+
GC.disable # for CPU profiling
5+
6+
RubyProf.start
7+
Date.parse('2014-07-01')
8+
result = RubyProf.stop
9+
10+
printer = RubyProf::FlatPrinter.new(result)
11+
12+
File.open('010_rp_1.prof', 'w+') do |file|
13+
printer.print(file)
14+
end
15+

010_rp_2.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'date'
2+
3+
GC.disable # for CPU profiling
4+
Date.parse('2014-07-01')
5+
6+
# run: `ruby-prof -p flat -m 1 -f 010_rp_2.prof 010_rp_2.rb`
7+

010_rp_rails/Gemfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source 'https://rubygems.org'
2+
3+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4+
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
5+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
6+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
7+
8+
gem 'ruby-prof'
9+

010_rp_rails/Gemfile.lock

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
actioncable (5.0.0)
5+
actionpack (= 5.0.0)
6+
nio4r (~> 1.2)
7+
websocket-driver (~> 0.6.1)
8+
actionmailer (5.0.0)
9+
actionpack (= 5.0.0)
10+
actionview (= 5.0.0)
11+
activejob (= 5.0.0)
12+
mail (~> 2.5, >= 2.5.4)
13+
rails-dom-testing (~> 2.0)
14+
actionpack (5.0.0)
15+
actionview (= 5.0.0)
16+
activesupport (= 5.0.0)
17+
rack (~> 2.0)
18+
rack-test (~> 0.6.3)
19+
rails-dom-testing (~> 2.0)
20+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
21+
actionview (5.0.0)
22+
activesupport (= 5.0.0)
23+
builder (~> 3.1)
24+
erubis (~> 2.7.0)
25+
rails-dom-testing (~> 2.0)
26+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
27+
activejob (5.0.0)
28+
activesupport (= 5.0.0)
29+
globalid (>= 0.3.6)
30+
activemodel (5.0.0)
31+
activesupport (= 5.0.0)
32+
activerecord (5.0.0)
33+
activemodel (= 5.0.0)
34+
activesupport (= 5.0.0)
35+
arel (~> 7.0)
36+
activesupport (5.0.0)
37+
concurrent-ruby (~> 1.0, >= 1.0.2)
38+
i18n (~> 0.7)
39+
minitest (~> 5.1)
40+
tzinfo (~> 1.1)
41+
arel (7.1.1)
42+
builder (3.2.2)
43+
concurrent-ruby (1.0.2)
44+
erubis (2.7.0)
45+
globalid (0.3.7)
46+
activesupport (>= 4.1.0)
47+
i18n (0.7.0)
48+
loofah (2.0.3)
49+
nokogiri (>= 1.5.9)
50+
mail (2.6.4)
51+
mime-types (>= 1.16, < 4)
52+
method_source (0.8.2)
53+
mime-types (3.1)
54+
mime-types-data (~> 3.2015)
55+
mime-types-data (3.2016.0521)
56+
mini_portile2 (2.1.0)
57+
minitest (5.9.0)
58+
nio4r (1.2.1)
59+
nokogiri (1.6.8)
60+
mini_portile2 (~> 2.1.0)
61+
pkg-config (~> 1.1.7)
62+
pkg-config (1.1.7)
63+
rack (2.0.1)
64+
rack-test (0.6.3)
65+
rack (>= 1.0)
66+
rails (5.0.0)
67+
actioncable (= 5.0.0)
68+
actionmailer (= 5.0.0)
69+
actionpack (= 5.0.0)
70+
actionview (= 5.0.0)
71+
activejob (= 5.0.0)
72+
activemodel (= 5.0.0)
73+
activerecord (= 5.0.0)
74+
activesupport (= 5.0.0)
75+
bundler (>= 1.3.0, < 2.0)
76+
railties (= 5.0.0)
77+
sprockets-rails (>= 2.0.0)
78+
rails-dom-testing (2.0.1)
79+
activesupport (>= 4.2.0, < 6.0)
80+
nokogiri (~> 1.6.0)
81+
rails-html-sanitizer (1.0.3)
82+
loofah (~> 2.0)
83+
railties (5.0.0)
84+
actionpack (= 5.0.0)
85+
activesupport (= 5.0.0)
86+
method_source
87+
rake (>= 0.8.7)
88+
thor (>= 0.18.1, < 2.0)
89+
rake (11.2.2)
90+
ruby-prof (0.15.9)
91+
sprockets (3.7.0)
92+
concurrent-ruby (~> 1.0)
93+
rack (> 1, < 3)
94+
sprockets-rails (3.1.1)
95+
actionpack (>= 4.0)
96+
activesupport (>= 4.0)
97+
sprockets (>= 3.0.0)
98+
thor (0.19.1)
99+
thread_safe (0.3.5)
100+
tzinfo (1.2.2)
101+
thread_safe (~> 0.1)
102+
websocket-driver (0.6.4)
103+
websocket-extensions (>= 0.1.0)
104+
websocket-extensions (0.1.2)
105+
106+
PLATFORMS
107+
ruby
108+
109+
DEPENDENCIES
110+
rails (>= 5.0.0.beta3, < 5.1)
111+
ruby-prof
112+
tzinfo-data
113+
114+
BUNDLED WITH
115+
1.11.2

010_rp_rails/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## README
2+
3+
This README would normally document whatever steps are necessary to get the
4+
application up and running.
5+
6+
Things you may want to cover:
7+
8+
* Ruby version
9+
10+
* System dependencies
11+
12+
* Configuration
13+
14+
* Database creation
15+
16+
* Database initialization
17+
18+
* How to run the test suite
19+
20+
* Services (job queues, cache servers, search engines, etc.)
21+
22+
* Deployment instructions
23+
24+
* ...

010_rp_rails/Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require File.expand_path('../config/application', __FILE__)
5+
6+
Rails.application.load_tasks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//= link_tree ../images
2+
//= link_directory ../stylesheets .css
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This is a manifest file that'll be compiled into application.css, which will include all the files
3+
* listed below.
4+
*
5+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7+
*
8+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
9+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10+
* files in this directory. Styles in this file should be added after the last require_* statement.
11+
* It is generally better to create a new file per style scope.
12+
*
13+
*= require_tree .
14+
*= require_self
15+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ApplicationController < ActionController::Base
2+
# Prevent CSRF attacks by raising an exception.
3+
# For APIs, you may want to use :null_session instead.
4+
protect_from_forgery with: :exception
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ApplicationHelper
2+
end
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationJob < ActiveJob::Base
2+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>RpRails</title>
5+
<%= csrf_meta_tags %>
6+
7+
<%= stylesheet_link_tag 'application', media: 'all' %>
8+
</head>
9+
10+
<body>
11+
<%= yield %>
12+
</body>
13+
</html>

010_rp_rails/bin/bundle

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env ruby
2+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3+
load Gem.bin_path('bundler', 'bundle')

010_rp_rails/bin/rails

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env ruby
2+
APP_PATH = File.expand_path('../../config/application', __FILE__)
3+
require_relative '../config/boot'
4+
require 'rails/commands'

010_rp_rails/bin/rake

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env ruby
2+
require_relative '../config/boot'
3+
require 'rake'
4+
Rake.application.run

010_rp_rails/bin/setup

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env ruby
2+
require 'pathname'
3+
require 'fileutils'
4+
include FileUtils
5+
6+
# path to your application root.
7+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
8+
9+
def system!(*args)
10+
system(*args) || abort("\n== Command #{args} failed ==")
11+
end
12+
13+
chdir APP_ROOT do
14+
# This script is a starting point to setup your application.
15+
# Add necessary setup steps to this file.
16+
17+
puts '== Installing dependencies =='
18+
system! 'gem install bundler --conservative'
19+
system('bundle check') or system!('bundle install')
20+
21+
# puts "\n== Copying sample files =="
22+
# unless File.exist?('config/database.yml')
23+
# cp 'config/database.yml.sample', 'config/database.yml'
24+
# end
25+
26+
puts "\n== Preparing database =="
27+
system! 'bin/rails db:setup'
28+
29+
puts "\n== Removing old logs and tempfiles =="
30+
system! 'bin/rails log:clear tmp:clear'
31+
32+
puts "\n== Restarting application server =="
33+
system! 'bin/rails restart'
34+
end

010_rp_rails/bin/update

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby
2+
require 'pathname'
3+
require 'fileutils'
4+
include FileUtils
5+
6+
# path to your application root.
7+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
8+
9+
def system!(*args)
10+
system(*args) || abort("\n== Command #{args} failed ==")
11+
end
12+
13+
chdir APP_ROOT do
14+
# This script is a way to update your development environment automatically.
15+
# Add necessary update steps to this file.
16+
17+
puts '== Installing dependencies =='
18+
system! 'gem install bundler --conservative'
19+
system 'bundle check' or system! 'bundle install'
20+
21+
puts "\n== Updating database =="
22+
system! 'bin/rails db:migrate'
23+
24+
puts "\n== Removing old logs and tempfiles =="
25+
system! 'bin/rails log:clear tmp:clear'
26+
27+
puts "\n== Restarting application server =="
28+
system! 'bin/rails restart'
29+
end

010_rp_rails/config.ru

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file is used by Rack-based servers to start the application.
2+
3+
require ::File.expand_path('../config/environment', __FILE__)
4+
5+
run Rails.application
6+

010_rp_rails/config/application.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require File.expand_path('../boot', __FILE__)
2+
3+
require "rails"
4+
# Pick the frameworks you want:
5+
require "active_model/railtie"
6+
# require "active_job/railtie"
7+
# require "active_record/railtie"
8+
require "action_controller/railtie"
9+
# require "action_mailer/railtie"
10+
require "action_view/railtie"
11+
# require "action_cable/engine"
12+
# require "sprockets/railtie"
13+
# require "rails/test_unit/railtie"
14+
15+
# Require the gems listed in Gemfile, including any gems
16+
# you've limited to :test, :development, or :production.
17+
Bundler.require(*Rails.groups)
18+
19+
class GCDisabler
20+
def initialize(app)
21+
@app = app
22+
end
23+
24+
def call(env)
25+
GC.start
26+
GC.disable
27+
response = @app.call(env)
28+
GC.enable
29+
response
30+
end
31+
end
32+
33+
module RpRails
34+
class Application < Rails::Application
35+
# Settings in config/environments/* take precedence over those specified here.
36+
# Application configuration should go into files in config/initializers
37+
# -- all .rb files in that directory are automatically loaded.
38+
39+
# To profile only Rails App:
40+
# config.middleware.use Rack::RubyProf, path: '/tmp/rails_profile'
41+
# To profile middleware also:
42+
config.middleware.insert_before Rack::Runtime, Rack::RubyProf, path: '/tmp/rails_profile'
43+
config.middleware.insert_before Rack::RubyProf, GCDisabler
44+
end
45+
end

010_rp_rails/config/boot.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
2+
3+
require 'bundler/setup' # Set up gems listed in the Gemfile.

010_rp_rails/config/environment.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Load the Rails application.
2+
require File.expand_path('../application', __FILE__)
3+
4+
# Initialize the Rails application.
5+
Rails.application.initialize!

0 commit comments

Comments
 (0)