From b39ec4983490d43ce9c02e77ae143fd2a33fdc7c Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Sat, 23 Mar 2013 19:57:09 +0800 Subject: [PATCH] convert textile docs to markdown --- Hacking.textile => Hacking.md | 15 +- README.textile => README.md | 277 +++++++++++++++++----------------- 2 files changed, 146 insertions(+), 146 deletions(-) rename Hacking.textile => Hacking.md (95%) rename README.textile => README.md (54%) diff --git a/Hacking.textile b/Hacking.md similarity index 95% rename from Hacking.textile rename to Hacking.md index 88174970..c47bf957 100644 --- a/Hacking.textile +++ b/Hacking.md @@ -1,8 +1,10 @@ -h1. Bullet Overview for Developers +# Bullet Overview for Developers + This file aims to give developers a quick tour of the bullet internals, making it (hopefully) easier to extend or enhance the Bullet gem. -h2. General Control Flow aka. 10000 Meter View +## General Control Flow aka. 10000 Meter View + When Rails is initialized, Bullet will extend ActiveRecord (and if you're using Rails 2.x ActiveController too) with the relevant modules and methods found in lib/bullet/active_recordX.rb and lib/bullet/action_controller2.rb. If you're @@ -36,7 +38,8 @@ So the flow of a request goes like this: 8. Bullet calls end_request for each detector. 9. Goto 1. -h2. Adding Notification Types +## Adding Notification Types + If you want to add more kinds of things that Bullet can detect, a little more work is needed than if you were just adding a Presenter, but the concepts are similar. @@ -55,7 +58,8 @@ Since the detection of pathological associations is a bit hairy, I'd recommend having a look at the counter cache detector and associated notification to get a feel for what is needed to get off the ground. -h3. Detectors +### Detectors + The only things you'll need to consider when building your Detector class is that it will need to supply the .start_request, .end_request and .clear class methods. @@ -63,7 +67,8 @@ methods. Simple implementations are provided by Bullet::Detector::Base for start_request and end_request, you will have to supply your own clear method. -h3. Notifications +### Notifications + For notifications you will want to supply a #title and #body instance method, and check to see if the #initialize and #full_notice methods in the Bullet::Notification::Base class fit your needs. diff --git a/README.textile b/README.md similarity index 54% rename from README.textile rename to README.md index af62c207..9643af58 100644 --- a/README.textile +++ b/README.md @@ -1,4 +1,4 @@ -h1. Bullet +# Bullet !https://secure.travis-ci.org/flyerhzm/bullet.png!:http://travis-ci.org/flyerhzm/bullet @@ -8,72 +8,71 @@ The Bullet gem is designed to help you increase your application's performance b Best practice is to use Bullet in development mode or custom mode (staging, profile, etc.). The last thing you want is your clients getting alerts about how lazy you are. -The Bullet gem now supports **activerecord** 2.1, 2.2, 2.3, 3.0, 3.1, 3.2 and **mongoid** >= 2.4.1. +The Bullet gem now supports **activerecord** 2.1, 2.2, 2.3, 3.0, 3.1, +3.2, 4.0 and **mongoid** >= 2.4.1. -**************************************************************************** +## External Introduction -h2. External Introduction +* [http://railscasts.com/episodes/372-bullet](http://railscasts.com/episodes/372-bullet) +* [http://ruby5.envylabs.com/episodes/9-episode-8-september-8-2009](http://ruby5.envylabs.com/episodes/9-episode-8-september-8-2009) +* [http://railslab.newrelic.com/2009/10/23/episode-19-on-the-edge-part-1](http://railslab.newrelic.com/2009/10/23/episode-19-on-the-edge-part-1) +* [http://weblog.rubyonrails.org/2009/10/22/community-highlights](http://weblog.rubyonrails.org/2009/10/22/community-highlights) -* "http://railscasts.com/episodes/372-bullet":http://railscasts.com/episodes/372-bullet -* "http://ruby5.envylabs.com/episodes/9-episode-8-september-8-2009":http://ruby5.envylabs.com/episodes/9-episode-8-september-8-2009 -* "http://railslab.newrelic.com/2009/10/23/episode-19-on-the-edge-part-1":http://railslab.newrelic.com/2009/10/23/episode-19-on-the-edge-part-1 -* "http://weblog.rubyonrails.org/2009/10/22/community-highlights":http://weblog.rubyonrails.org/2009/10/22/community-highlights - -**************************************************************************** - -h2. Install +## Install You can install it as a gem: -

+
+```
 gem install bullet
-
+ +``` or add it into a Gemfile (Bundler): -

+
+
+```ruby
 gem "bullet", :group => "development"
-
+``` -**************************************************************************** +## Configuration -h2. Configuration +Bullet won't do ANYTHING unless you tell it to explicitly. Append to +`config/environments/development.rb` initializer with the following code: -Bullet won't do ANYTHING unless you tell it to explicitly. Append to config/environments/development.rb initializer with the following code: -

+```ruby
 config.after_initialize do
   Bullet.enable = true
-  Bullet.alert = true
-  Bullet.bullet_logger = true
+  Bullet.alert = true Bullet.bullet_logger = true
   Bullet.console = true
   Bullet.growl = true
-  Bullet.xmpp = { :account => 'bullets_account@jabber.org',
+  Bullet.xmpp = { :account  => 'bullets_account@jabber.org',
                   :password => 'bullets_password_for_jabber',
                   :receiver => 'your_account@jabber.org',
                   :show_online_status => true }
   Bullet.rails_logger = true
   Bullet.airbrake = true
 end
-
+``` -The notifier of bullet is a wrap of "uniform_notifier":https://github.com/flyerhzm/uniform_notifier +The notifier of bullet is a wrap of [uniform_notifier](https://github.com/flyerhzm/uniform_notifier) The code above will enable all seven of the Bullet notification systems: -* Bullet.enable: enable Bullet gem, otherwise do nothing -* Bullet.alert: pop up a JavaScript alert in the browser -* Bullet.bullet_logger: log to the Bullet log file (Rails.root/log/bullet.log) -* Bullet.rails_logger: add warnings directly to the Rails log -* Bullet.airbrake: add notifications to airbrake -* Bullet.console: log warnings to your browser's console.log (Safari/Webkit browsers or Firefox w/Firebug installed) -* Bullet.growl: pop up Growl warnings if your system has Growl installed. Requires a little bit of configuration -* Bullet.xmpp: send XMPP/Jabber notifications to the receiver indicated. Note that the code will currently not handle the adding of contacts, so you will need to make both accounts indicated know each other manually before you will receive any notifications. If you restart the development server frequently, the 'coming online' sound for the bullet account may start to annoy - in this case set :show_online_status to false; you will still get notifications, but the bullet account won't announce it's online status anymore. - -**************************************************************************** +* `Bullet.enable`: enable Bullet gem, otherwise do nothing +* `Bullet.alert`: pop up a JavaScript alert in the browser +* `Bullet.bullet_logger`: log to the Bullet log file (Rails.root/log/bullet.log) +* `Bullet.rails_logger`: add warnings directly to the Rails log +* `Bullet.airbrake`: add notifications to airbrake +* `Bullet.console`: log warnings to your browser's console.log (Safari/Webkit browsers or Firefox w/Firebug installed) +* `Bullet.growl`: pop up Growl warnings if your system has Growl installed. Requires a little bit of configuration +* `Bullet.xmpp`: send XMPP/Jabber notifications to the receiver indicated. Note that the code will currently not handle the adding of contacts, so you will need to make both accounts indicated know each other manually before you will receive any notifications. If you restart the development server frequently, the 'coming online' sound for the bullet account may start to annoy - in this case set :show_online_status to false; you will still get notifications, but the bullet account won't announce it's online status anymore. -h2. Log +## Log -The Bullet log log/bullet.log will look something like this: +The Bullet log `log/bullet.log` will look something like this: * N+1 Query: -

+
+```
 2009-08-25 20:40:17[INFO] N+1 Query: PATH_INFO: /posts;    model: Post => associations: [comments]·
 Add to your finder: :include => [:comments]
 2009-08-25 20:40:17[INFO] N+1 Query: method call stack:·
@@ -81,54 +80,50 @@ Add to your finder: :include => [:comments]
 /Users/richard/Downloads/test/app/views/posts/index.html.erb:8:in `each'
 /Users/richard/Downloads/test/app/views/posts/index.html.erb:8:in `_run_erb_app47views47posts47index46html46erb'
 /Users/richard/Downloads/test/app/controllers/posts_controller.rb:7:in `index'
-
+``` The first two lines are notifications that N+1 queries have been encountered. The remaining lines are stack traces so you can find exactly where the queries were invoked in your code, and fix them. * Unused eager loading: -

+
+```
 2009-08-25 20:53:56[INFO] Unused eager loadings: PATH_INFO: /posts;    model: Post => associations: [comments]·
 Remove from your finder: :include => [:comments]
-
+``` These two lines are notifications that unused eager loadings have been encountered. * Need counter cache: -

+
+```
 2009-09-11 09:46:50[INFO] Need Counter Cache
   Post => [:comments]
-
- -**************************************************************************** - -h2. Growl, XMPP/Jabber and Airbrake Support +``` -see "https://github.com/flyerhzm/uniform_notifier":https://github.com/flyerhzm/uniform_notifier +## Growl, XMPP/Jabber and Airbrake Support -**************************************************************************** +see [https://github.com/flyerhzm/uniform_notifier](https://github.com/flyerhzm/uniform_notifier) -h2. Important +## Important If you find bullet does not work for you, *please disable your browser's cache*. -**************************************************************************** - -h2. Advance +## Advance The bullet gem use rack middleware for http request. If you want to bullet for without http server, such as job server. You can do like this: -

+```ruby
 Bullet.start_request if Bullet.enable?
 # run job
 if Bullet.enable? && Bullet.notification?
   Bullet.perform_out_of_channel_notifications
 end
 Bullet.end_request if Bullet.enable?
-
+``` Or you want to use it in test mode -

+```ruby
 before(:each)
   Bullet.start_request if Bullet.enable?
 end
@@ -139,35 +134,31 @@ after(:each)
   end
   Bullet.end_request if Bullet.enable?
 end
-
+``` Don't forget enabling bullet in test environment. -**************************************************************************** - -h2. Contributors +## Contributors -"https://github.com/flyerhzm/bullet/contributors":https://github.com/flyerhzm/bullet/contributors +[https://github.com/flyerhzm/bullet/contributors](https://github.com/flyerhzm/bullet/contributors) -**************************************************************************** - -h2. Step by step example +## Step by step example Bullet is designed to function as you browse through your application in development. It will alert you whenever it encounters N+1 queries or unused eager loading. 1. setup test environment -

+```
 $ rails new test_bullet
 $ cd test_bullet
 $ rails g scaffold post name:string
 $ rails g scaffold comment name:string post_id:integer
 $ bundle exec rake db:migrate
-
+``` -2. change app/model/post.rb and app/model/comment.rb +2. change `app/model/post.rb` and `app/model/comment.rb` -

+```ruby
 class Post < ActiveRecord::Base
   has_many :comments
 end
@@ -175,22 +166,22 @@ end
 class Comment < ActiveRecord::Base
   belongs_to :post
 end
-
+``` -3. go to rails c and execute +3. go to `rails c` and execute -

+```ruby
 post1 = Post.create(:name => 'first')
 post2 = Post.create(:name => 'second')
 post1.comments.create(:name => 'first')
 post1.comments.create(:name => 'second')
 post2.comments.create(:name => 'third')
 post2.comments.create(:name => 'fourth')
-
+``` -4. change the app/views/posts/index.html.erb to produce a N+1 query +4. change the `app/views/posts/index.html.erb` to produce a N+1 query -

+```
 <% @posts.each do |post| %>
   
     <%= post.name %>
@@ -200,21 +191,24 @@ post2.comments.create(:name => 'fourth')
     <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
   
 <% end %>
-
+``` -5. add bullet gem to Gemfile +5. add bullet gem to `Gemfile` -

+```ruby
 gem "bullet"
-
+``` And run -
bundle install
+``` +bundle install +``` -6. enable the bullet gem in development, add a line to config/environments/development.rb +6. enable the bullet gem in development, add a line to +`config/environments/development.rb` -

+```ruby
 config.after_initialize do
   Bullet.enable = true
   Bullet.alert = true
@@ -223,28 +217,28 @@ config.after_initialize do
 #  Bullet.growl = true
   Bullet.rails_logger = true
 end
-
+``` 7. start server -

+```
 $ rails s
-
+``` 8. input http://localhost:3000/posts in browser, then you will see a popup alert box says -

+```
 The request has unused preload associations as follows:
 None
 The request has N+1 queries as follows:
 model: Post => associations: [comment]
-
+``` which means there is a N+1 query from post object to comments associations. -In the meanwhile, there's a log appended into log/bullet.log file +In the meanwhile, there's a log appended into `log/bullet.log` file -

+```
 2010-03-07 14:12:18[INFO] N+1 Query in /posts
   Post => [:comments]
   Add to your finder: :include => [:comments]
@@ -253,55 +247,57 @@ In the meanwhile, there's a log appended into log/bullet.log file
   /home/flyerhzm/Downloads/test_bullet/app/views/posts/index.html.erb:11:in `each'
   /home/flyerhzm/Downloads/test_bullet/app/views/posts/index.html.erb:11:in `_render_template__600522146_80203160_0'
   /home/flyerhzm/Downloads/test_bullet/app/controllers/posts_controller.rb:7:in `index'
-
+``` The generated SQLs are -

-  Post Load (1.0ms)   SELECT * FROM "posts"
-  Comment Load (0.4ms)   SELECT * FROM "comments" WHERE ("comments".post_id = 1)
-  Comment Load (0.3ms)   SELECT * FROM "comments" WHERE ("comments".post_id = 2)
-
+``` +Post Load (1.0ms) SELECT * FROM "posts" +Comment Load (0.4ms) SELECT * FROM "comments" WHERE ("comments".post_id = 1) +Comment Load (0.3ms) SELECT * FROM "comments" WHERE ("comments".post_id = 2) +``` -9. fix the N+1 query, change app/controllers/posts_controller.rb file +9. fix the N+1 query, change `app/controllers/posts_controller.rb` file -

-  def index
-    @posts = Post.includes(:comments)
+```ruby
+def index
+  @posts = Post.includes(:comments)
 
-    respond_to do |format|
-      format.html # index.html.erb
-      format.xml  { render :xml => @posts }
-    end
+  respond_to do |format|
+    format.html # index.html.erb
+    format.xml  { render :xml => @posts }
   end
-
+end +``` 10. refresh http://localhost:3000/posts page, no alert box and no log appended. The generated SQLs are -

-  Post Load (0.5ms)   SELECT * FROM "posts"
-  Comment Load (0.5ms)   SELECT "comments".* FROM "comments" WHERE ("comments".post_id IN (1,2))
-
+``` +Post Load (0.5ms) SELECT * FROM "posts" +Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE ("comments".post_id IN (1,2)) +``` a N+1 query fixed. Cool! -11. now simulate unused eager loading. Change app/controllers/posts_controller.rb and app/views/posts/index.html.erb +11. now simulate unused eager loading. Change +`app/controllers/posts_controller.rb` and +`app/views/posts/index.html.erb` -

-  def index
-    @posts = Post.includes(:comments)
+```ruby
+def index
+  @posts = Post.includes(:comments)
 
-    respond_to do |format|
-      format.html # index.html.erb
-      format.xml  { render :xml => @posts }
-    end
+  respond_to do |format|
+    format.html # index.html.erb
+    format.xml  { render :xml => @posts }
   end
-
+end +``` -

+```
 <% @posts.each do |post| %>
   
     <%= post.name %>
@@ -310,38 +306,39 @@ a N+1 query fixed. Cool!
     <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
   
 <% end %>
-
+``` 12. refresh http://localhost:3000/posts page, then you will see a popup alert box says -

+```
 The request has unused preload associations as follows:
 model: Post => associations: [comment]
 The request has N+1 queries as follows:
 None
-
+``` -In the meanwhile, there's a log appended into log/bullet.log file +In the meanwhile, there's a log appended into `log/bullet.log` file -

+```
 2009-08-25 21:13:22[INFO] Unused preload associations: PATH_INFO: /posts;    model: Post => associations: [comments]·
 Remove from your finder: :include => [:comments]
-
+``` -13. simulate counter_cache. Change app/controllers/posts_controller.rb and app/views/posts/index.html.erb +13. simulate counter_cache. Change `app/controllers/posts_controller.rb` +and `app/views/posts/index.html.erb` -

-  def index
-    @posts = Post.all
+```ruby
+def index
+  @posts = Post.all
 
-    respond_to do |format|
-      format.html # index.html.erb
-      format.xml  { render :xml => @posts }
-    end
+  respond_to do |format|
+    format.html # index.html.erb
+    format.xml  { render :xml => @posts }
   end
-
+end +``` -

+```
 <% @posts.each do |post| %>
   
     <%= post.name %>
@@ -351,23 +348,21 @@ Remove from your finder: :include => [:comments]
     <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
   
 <% end %>
-
+``` 14. refresh http://localhost:3000/posts page, then you will see a popup alert box says -

+```
 Need counter cache
   Post => [:comments]
-
+``` -In the meanwhile, there's a log appended into log/bullet.log file. +In the meanwhile, there's a log appended into `log/bullet.log` file. -

+```
 2009-09-11 10:07:10[INFO] Need Counter Cache
   Post => [:comments]
-
- -**************************************************************************** +``` Copyright (c) 2009 - 2013 Richard Huang (flyerhzm@gmail.com), released under the MIT license