Monday 26 March 2012

bullet 2.3.0 released


bullet is a gem to help you increase your application's performance by reducing the number of sql requests it makes. Today I released bullet 2.3.0 to better support rails 3.1 and 3.2 and performance improved. It's a long time I didn't do any changes to bullet, let me tell you the story I work for bullet 2.3.0.
bullet is a gem to help we enlarge your application’s opening by shortening a series of sql requests it makes. bullet 2.3.0 got 10% opening softened for activerecord 3.0.12, it functions most quicker, bullet for ar 3.1.4 is 48% quicker than for ar 3.0.12, as well as for ar 3.2.2 is 40% quicker than for ar 3.0.12
At the beginning of this month, bullet got its 1000th watcher on github, I realized it's time to improve it e.g. speed up and compatible with edge rails.
The first thing I did is to refactor tests. Before I created several rspec tests, but they are more like integration tests instead of unit tests, so I move them to spec/integration/ directory. Then I added a bunch of test units to cover all codes, which can promise the correctness of further code refactors. I also use guard instead of watchr to do auto tests, why I preferred guard? It's much easier and has more extensions, like guard-rspec.
Then I moved AR models, which are used for integration tests, from integration tests to spec/models, and I also moved db connection, db schema and db seed to spec/support/, moved test helpers to spec/support/ as well. Now my tests looks much cleaner and run much faster (only connect db once).
After refactoring tests, I tried to improve the bullet performance, I already created a benchmark scriptbefore, bullet 2.2.1 with rails 3.0.12 spent 30s to complete
bullet 2.2.1 with rails 3.0.12
                                                                             user     system      total        real
Querying & Iterating 1000 Posts with 10000 Comments and 100 Users       29.970000   0.270000  30.240000 ( 30.452083)
Then I used perftools.rb to measure cpu time for methods, the result is garbage_collector, String#=~ and Kernel#caller
  1. garbage_collector, it depends on how many objects allocated
  2. String#=~, bullet use regexp to check if caller contains load_target
  3. Kernel#caller, bullet uses caller to tell what codes caused n+1 query
I found the easiest is to mitigate String#=~, as bullet only check regexp with constant string load_target, so I simply used .include?("load_target") instead.
bullet 2.3.0 with rails 3.0.12
                                                                             user     system      total        real
Querying & Iterating 1000 Posts with 10000 Comments and 100 Users       26.120000   0.430000  26.550000 ( 27.179304)
another change is to store object's ar_key instead of object itself.
{<#Post id:1, title:"post1", body:"post body", created_at:..., updated_at:...> => [:comments]}
to
{"Post:1" => [:comments]}
it speeds up hash comparison time and save the hash size.
I also hacked ActiveRecord::Associations::SingularAssociation#reader instead of ActiveRecord::Associations::Association#load_target for rails 3.1 and 3.2, it fixes activerecord 3.1 and 3.2 compatibility, there is no need to call caller in Association#load_target, it runs much faster in rails 3.1 and 3.2, the following is the benchmark result
bullet 2.3.0 with rails 3.2.2
                                                                             user     system      total        real
Querying & Iterating 1000 Posts with 10000 Comments and 100 Users       16.460000   0.190000  16.650000 ( 16.968246)

bullet 2.3.0 with rails 3.1.4
                                                                             user     system      total        real
Querying & Iterating 1000 Posts with 10000 Comments and 100 Users       14.600000   0.130000  14.730000 ( 14.937590)
Enjoy the new bullet gem!

Monday 19 March 2012

How to Install Google Chrome in Ubuntu 11.10(64 bit)

https://www.google.com/chrome?platform=linux

1) Download Chrome fromhttps://www.google.com/chrome?platform=linux  the folder you saved in 

2) Open Terminal by pressing ctrl+alt+t... 

3) Run this 
sudo apt-get install libnspr4-0d libnss3-1d libxss1 libcurl3 

4) Run this mandatory command 
sudo dpkg -i './Downloads/google-chrome-stable_current_amd64.deb' 

Now Google Chrome is successfully installed and you can search it in the search box..... 

Wednesday 7 March 2012

Controlling your Browser with your Phone and Grails


’ve called this blog post ‘Controlling your Browser with your Phone and Grails’ but I perfectly could have named it ‘Building Publish / Subscribe Apps with Tropo and Redis Revisited’ as what I’m going to show you here is basically a remake of Mark Headd’s super-awesome blog post from April 4th that showed how you could create voice-powered publish-subscribe applications using Redis and Tropo.
In Mark’s example you learnt how to create an application that was able to react to your orders from your telephone switching the color of your browser. I thought it was super-cool, and I also thought that it would be very cool to recreate the same example but using WebApi and Grailsinstead of Tropo Scripting.
The architecture of this project is very similar to Mark’s architecture. You can check it out here. The main differences are basically that my application is based in the WebApi model and that it uses Cometd to communicate from the server to the browser. I’ve uploaded a screencast to YouTube that explains the source code and that shows the application in action:
The source code for this sample project is available at Github. I hope you like it!

To pass current_user object to named_scope in model

named_scope is a way to search records from table repeatedly. Its written in model and one of the easiest way to fetch records.
For example to fetch records from projects table with status finish true, we can proceed as follows: 
class Project < ActiveRecord::Base
  named_scope :completed,:conditions=>"finish = true" 
end
And to use it we can call model name dot named_scope method :
Project.completed
Its very simple, but to pass current_user object to this named_scope is a bit tricky. Suppose we have to find all the projects with status finish, of the current user then it can be done by taking user as an object in it passing user.id as displayed in the below code:

class Project < ActiveRecord::Base 
  named_scope :completed,lambda { |user| where("finish=? and user_id = ?", true, user.id)} 
end

And to use we have to pass the current_user object as:
Project.completed(current_user)
Simple, isn't it !

What is the difference between ImageMagick and RMagick

ImageMagick is a software suite to create, edit, compose, or convert bitmap images, resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. It can read and write images in different types of formats including GIF, JPEG,PDF, PNG, TIFF and many more. 

Its functionality can be utilized from the command line or you can use the features from programs like: G2F (Ada), MagickCore (C), MagickWand (C), ChMagick (Ch), ImageMagickObject (COM+), Magick++ (C++), JMagick (Java), L-Magick (Lisp), NMagick (Neko/haXe), MagickNet (.NET), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (PHP), IMagick (PHP), PythonMagick (Python), RMagick (Ruby), or TclMagick (Tcl/TK).



RMagick is a binding from Ruby to the ImageMagick image manipulation library. RMagick is a complete interface to ImageMagick. RMagick 1 and RMagick 2 are the two RMagick versions. RMagick 2 is the future of RMagick which works with the latest Ruby version

Difference between Validations, Callbacks and Observers

Validations allow you to ensure that only valid data is stored in your database.
Example: validates_presence_of :user_name, :password
                 validates_numericality_of :value

We can write custom validation also as

def validate
  errors.add(:price, “should be a positive value”) if price.nil?|| price < 0.01
end

Callbacks and observers allow you to trigger logic before or after an alteration of an object’s state.

Callbacks are methods that get called at certain moments of an object’s life cycle. With callbacks it’s possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.

Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state. This can be used to make sure that associated and dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes before they’re validated (by overwriting before_validation)

Observers are similar to callbacks, but with important differences. Whereas callbacks can pollute a model with code that isn’t directly related to its purpose, observers allow you to add the same functionality outside of a model. For example, it could be argued that a User model should not include code to send registration confirmation emails. Whenever you use callbacks with code that isn’t directly related to your model, you may want to consider creating an observer instead.

Difference between Application server and Web Server

apache, nginx, IIS are web servers
mongrel, webrick, phusion passenger are app servers

App server is something which works with particular programming language and parses and executes the code
since mongrel and webrick can only work with rails, so they are app servers

Web servers are servers which can take the request from the browser.
Web servers normally works on port 80 though we can change the port in configuration 
since mongrel and webrick can take that request directly, so they can be thought of as web servers but web servers do have a lot of other functionality like request pipeline, load balancing etc.
App servers lack these functionalities.

About Mongrel server:
mongrel work as web as well as app server if you are talking about dev environment
but in production, mongrel alone can not work it will be too slow
so we need a web server in front of mongrel

Thanks to Mr. Sumit  Garg & Mr. Sidhant for clarifying these points in details.
I would love to have others view also on this.

Update Rails 3.1 to Rails 3.2


In Gemfile, change following versions of assets and rails

gem 'rails', '3.2.0'

group :assets do
  gem 'sass-rails', "  ~> 3.2.3"
  gem 'coffee-rails', "~> 3.2.1"
  gem 'uglifier', '>= 1.0.3'
end

Run 'bundle update'

Monday 5 March 2012

Installing Ubuntu inside Windows using VirtualBox


Installing Ubuntu inside Windows using VirtualBox

Introduction
VirtualBox allows you to run an entire operating system inside another operating system. Please be aware that you should have a minimum of 512 MB of RAM. 1 GB of RAM or more is recommended.

Installation Process
The first thing you have to do is obtain VirtualBox. Visit the VirtualBox website’s download page.

After you launch VirtualBox from the Windows Start menu, click on New to create a new virtual machine. When the New Virtual Machine Wizard appears, click Next.
You can call the machine whatever you want. If you’re installing Ubuntu, it makes sense to call it Ubuntu, I guess. You should also specify that the operating system is Linux.
VirtualBox will try to guess how much of your memory (or RAM) to allocate for the virtual machine. If you have 1 GB or less of RAM, I would advise you stick with the recommendation. If, however, you have over 1 GB, about a quarter your RAM or less should be fine. For example, if you have 2 GB of RAM, 512 MB is fine to allocate. If you have 4 GB of RAM, 1 GB is fine to allocate. If you have no idea what RAM is or how much of it you have, just go with the default.
Click Next.
If this is your first time using VirtualBox (which it probably is if you need a tutorial on how to use it), then you do want toCreate new hard disk and then click Next.
Click Next again.
Theoretically, a dynamically expanding virtual hard drive is best, because it’ll take up only what you actually use. I have come upon weird situations, though, when installing new software in a virtualized Ubuntu, in which the virtual hard drive just fills up instead of expanding. So I would actually recommend picking a Fixed-size storage.
Ubuntu’s default installation is less than 4 GB. If you plan on adding software or downloading large files in your virtualized UBuntu, you should tack on some buffer.
Click Finish and wait for the virtual hard drive to be created. This is actually just a very large file that lives inside of your Windows installation.
Click Finish
 
The next thing to do to make the (currently blank) virtual hard drive useful is to add the downloaded Ubuntu disk image (the .iso) boot on your virtual machine. Click on Settings and Storage. Then, under CD/DVD Device, next to Empty, you’ll see a little folder icon. Click that, and you can select the Ubuntu .iso you downloaded earlier.
Once you’ve selected it, click OK.
Then double-click your virtual machine to start it up.
Once it’s started up, just follow the regular installation procedure as if you were installing Ubuntu on a real hard drive (instead of a virtual one).

Oracle VirtualBox virtualize


Oracle VirtualBox

VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. Not only is VirtualBox an extremely feature rich, high performance product for enterprise customers, it is also the only professional solution that is freely available as Open Source Software under the terms of the GNU General Public License (GPL) version 2. See "About VirtualBox" for an introduction.
Presently, VirtualBox runs on Windows, Linux, Macintosh, and Solaris hosts and supports a large number of guest operating systems including but not limited to Windows (NT 4.0, 2000, XP, Server 2003, Vista, Windows 7), DOS/Windows 3.x, Linux (2.4 and 2.6), Solaris and OpenSolaris, OS/2, and OpenBSD.
VirtualBox is being actively developed with frequent releases and has an ever growing list of features, supported guest operating systems and platforms it runs on. VirtualBox is a community effort backed by a dedicated company: everyone is encouraged to contribute while Oracle ensures the product always meets professional quality criteria.
VirtualBox platform packages. The binaries are released under the terms of the GPL version 2.
For More