Ruby Benchmarks

Posted by Gian Zas on June 18, 2009

One of the most popular rants against ruby are based on its not so good performance.

Actually has been made improvements on this topic, specially in the new Ruby implementation (Ruby 1.9) based on the YARV virtual machine.

JRuby (Ruby over JVM implementation) has been focused in its performance from its lastest releases. Here at moove-it we are exploring the posibility of use Rails over JRuby in some JEE application servers, so we need some facts about JRuby performance (and other topics like gems compatibility, etc)

The Ruby community has put a set of benchmarks at ruby1.9 trunk: http://svn.ruby-lang.org/repos/ruby/trunk/benchmark/

So, we test with these benchmarks and here are the results!.

The code that run the benchmarks is like that:

block_to_benchmark = lambda { load BENCHMARKS_DIR + '/' + filename}
Benchmark.measure &block_to_benchmark

(yes, we are using the benchmark module bundled with the ruby standard lib)

Benchmark

Ruby1.8

Ruby1.9

JRuby1.3.0

Ruby1.8 /

Ruby1.9

Ruby1.8 /

JRuby1.3.0

bm_app_fib.rb 9.02 4.00 3.83 2.25 2.36
bm_app_mandelbrot.rb 3.36 0.81 1.49 4.14 2.26
bm_app_pentomino.rb 144.24 91.97 105.64 1.57 1.37
bm_app_raise.rb 6.94 6.98 1.63 -1.01 4.27
bm_app_strconcat.rb 2.94 1.63 1.30 1.81 2.27
bm_app_tak.rb 12.27 5.66 4.06 2.17 3.02
bm_app_tarai.rb 9.81 4.83 3.27 2.03 3.00
bm_app_uri.rb 6.59 3.77 3.69 1.75 1.79
bm_io_file_create.rb 7.17 3.12 7.70 2.30 -1.07
bm_io_file_read.rb 2.27 0.61 0.64 3.71 3.54
bm_io_file_write.rb 1.77 10.98 0.31 -6.22 5.64
bm_loop_for.rb 2.97 7.81 7.50 -2.63 -2.53
bm_loop_generator.rb 149.64 3.00 10.95 49.88 13.66
bm_loop_times.rb 4.88 6.88 7.95 -1.41 -1.63
bm_loop_whileloop.rb 11.23 3.67 9.42 3.06 1.19
bm_loop_whileloop2.rb 2.33 0.72 1.89 3.24 1.23
bm_so_array.rb 8.41 7.33 16.17 1.15 -1.92
bm_so_binary_trees.rb 4.50 2.14 2.84 2.10 1.58
bm_so_concatenate.rb 2.49 1.95 3.03 1.27 -1.22
bm_so_exception.rb 7.62 10.42 2.63 -1.37 2.90
bm_so_fasta.rb 13.59 11.77 16.03 1.16 -1.18
bm_so_lists.rb 2.27 1.38 1.48 1.65 1.53
bm_so_mandelbrot.rb 44.55 32.49 49.28 1.37 -1.11
bm_so_matrix.rb 2.69 2.13 1.84 1.26 1.46
bm_so_meteor_contest.rb 52.55 25.17 22.02 2.09 2.39
bm_so_nbody.rb 35.80 26.61 16.78 1.35 2.13
bm_so_nested_loop.rb 6.09 6.88 8.64 -1.13 -1.42
bm_so_nsieve.rb 26.89 13.11 24.69 2.05 1.09
bm_so_nsieve_bits.rb 62.50 46.05 42.55 1.36 1.47
bm_so_object.rb 11.56 11.52 3.11 1.00 3.72
bm_so_partial_sums.rb 80.13 228.91 31.22 -2.86 2.57
bm_so_pidigits.rb 10.33 10.44 7.03 -1.01 1.47
bm_so_random.rb 4.59 12.88 1.86 -2.80 2.47
bm_so_sieve.rb 0.84 0.34 0.63 2.45 1.35
bm_so_spectralnorm.rb 41.86 92.88 20.03 -2.22 2.09
bm_vm1_block.rb 26.22 13.44 27.44 1.95 -1.05
bm_vm1_const.rb 19.02 6.27 17.64 3.03 1.08
bm_vm1_ensure.rb 20.06 5.11 16.19 3.93 1.24
bm_vm1_ivar.rb 17.95 9.50 18.78 1.89 -1.05
bm_vm1_ivar_set.rb 19.22 9.83 21.67 1.96 -1.13
bm_vm1_length.rb 22.95 7.44 18.16 3.09 1.26
bm_vm1_neq.rb 20.81 6.58 14.98 3.16 1.39
bm_vm1_not.rb 14.91 5.58 12.64 2.67 1.18
bm_vm1_rescue.rb 15.72 4.64 21.38 3.39 -1.36
bm_vm1_simplereturn.rb 23.84 9.66 16.05 2.47 1.49
bm_vm1_swap.rb 50.25 5.73 24.38 8.76 2.06
bm_vm2_array.rb 10.72 19.27 5.42 -1.80 1.98
bm_vm2_case.rb 5.06 1.66 4.00 3.06 1.27
bm_vm2_eval.rb 32.20 200.98 69.41 -6.24 -2.16
bm_vm2_method.rb 15.45 9.45 11.36 1.63 1.36
bm_vm2_mutex.rb 5.47 6.34 7.30 -1.16 -1.33
bm_vm2_poly_method.rb 20.61 12.16 21.05 1.70 -1.02
bm_vm2_poly_method_ov.rb 5.00 1.66 4.53 3.02 1.10
bm_vm2_proc.rb 12.00 3.86 6.75 3.11 1.78
bm_vm2_regexp.rb 5.89 19.25 5.63 -3.27 1.05
bm_vm2_send.rb 5.05 2.11 4.38 2.39 1.15
bm_vm2_super.rb 5.75 3.17 4.44 1.81 1.30
bm_vm2_unif1.rb 5.20 1.99 3.61 2.62 1.44
bm_vm2_zsuper.rb 6.87 3.48 5.49 1.97 1.25
bm_vm3_thread_create_join.rb 1.95 7.70 19.13 -3.94 -9.79
bm_vm3_gc.rb 292.30 266.14 0.36 1.10 814.20

.

A looser conclusion may be that Ruby 1.9 is 95% faster than Ruby1.8, and JRuby 1.3.0 is 10% faster than 1.8, in general the new implementations are faster than Ruby1.8,  especially 1.9 (twice as faster).

The benchmarks were under WindowsXP  SP3, 4GB RAM, and a Intel Core 2 duo 2.0GHz. Happy hacking!

[Post to Twitter] Tweet This Post 

Steps to include lightweight cache in java applications - WhirlyCache

Posted by Martin Cabrera on May 28, 2009

Data caching is a very important consideration for JEE applications.

A classic problem for any application is to detect and solve the recurrent calls for optimizing the applications. Some cases can be:

  • database calls
  • business logic calls
  • remote method invocations

Data caching limits the number of remote invocations in distributed applications and improves performance of web applications by reducing the number of calls.

Several solutions exist to include a framework for implementing a cache.

Check out this interesting comparative table.

WhirlyCache (https://whirlycache.dev.java.net)

I use whirlycache in two projects and works really good. I recommend it to incorporate in a simple lightweight Web application
Whirlycache is a fast, configurable in-memory object cache for Java. It can be used, for example, to speed up a website or an application by caching objects that would otherwise have to be created by querying a database or by another expensive procedure. From the testing that we have done, it appears to be faster than any other Java cache that we have been able to inspect.

Steps to include WhirlyCache

(Step I) Donwload whirlycache and include it in your project -> https://whirlycache.dev.java.net/files/documents/1995/34601/whirlycache-1.0.1.zip

(Step II) Create a cache instance

CacheConfiguration cc = new CacheConfiguration();

cc.setName("KBCache");

cc.setBackend("com.whirlycott.cache.impl.ConcurrentHashMapImpl");

cc.setTunerSleepTime(60);

cc.setPolicy("com.whirlycott.cache.policy.LFUMaintenancePolicy");

cc.setMaxSize(10000);

Cache c = CacheManager.getInstance().createCache(cc);

(Step III) Use cache instance

c.store(key, object);
c.remove(key);
c.retrieve(key);

Example of java class (is a jboss seam entity class) - StockCache.java


[Post to Twitter] Tweet This Post 

Using Facebooker to make a Rails site with Facebook Connect (Part 3)

Posted by Augusto Guido on May 25, 2009

Welcome to part 3 of the facebooker trilogy

I’m kind of tyred of writing the same title over and over again and changing just the last number, problem is there are so many things we can do with this stuff we could write tons of this. I could put in the title what we will talk about specifically in this post, but the idea is to follow this post/tutorials in order. This will probably be the last part of this set of posts, the following ones will refer in it’s specifically to what will be done.

So far we explained how to configure facebooker, sign up, invite friends and bit of the theory. So today we’ll publish something in the users dashboard, so him and all his friends can see it his wall. So go to
developers.facebook.com/tools.php and select “Feed Template Console”, select the application you want and click next.

Give me a minute to explain you what we are doing.

We are going to register our feeds in facebook, and then we are going to call them by a number they will give us.
You can do this using facebooker, or you can register them in the link I gave you above. I think that using the facebook tools is way easier and also facebooker may be a bit out of date regarding this area due to all the legal and business changes in facebook. Also not event facebook has yet clear what they want: “At this time, the policy on automatically publishing one line stories has not been finalized”.

Anyway, complete the step guide for feeds in facebook and keep the number of the template you created. BTW while completing it you will find this kind of stuff “{*actor*}” without quotes. These are tokens that allow to put variable stuff (like names, links, etc.), you can read about them while creating the templates since they are well explained over there.

Now, you have the story. You need the users approval to publish it, so what facebook does is shows the story to the user and gives him otions to skip or publish. If we are in luck the users hits “publish”.

So we now obviously want to show it to the user so he can choose. Here’s the code

FB.ensureInit(function() {
var body_general = "Join them in my facebook connect site"
var template_data =
{
"actor":  "<%= @_logged_user.name %>",
"friends":"<%= @event.users.collect{|u| u.name_or_alias}.join(', ') %>",
"event":  "<%= @event.description %>",
"place":  "<%= @event.place %>",
"time" :  "<%= @event.start.strftime('%m/%d/%Y %I:%M %p') %>"
};
var user_message_prompt = "<%= @match.comment %>";
FB.Connect.showFeedDialog('the_number_of_your_template', template_data, [], body_general, null, FB.RequireConnect.require, FB.RequireConnect.promptConnect, user_message_prompt);
});

In the template data you have to complete the tokens you created when publishing your story. You can everything pretty well explained in this couple of links:

http://wiki.developers.facebook.com/index.php/JS_API_M_FB.Connect.ShowFeedDialog
http://wiki.developers.facebook.com/index.php/Feed.publishUserAction
http://wiki.developers.facebook.com/index.php/Publishing_Feed_Stories_to_Facebook

Pay attention to the “FB.ensureInit(function(){”, I don’t know why I couldn’t find anywhere in facebook that mention this should be there. If you don’t add this nothing will happen, maybe it’s obvios for some people, but it wasn’t for me. I’m sory I can’t remember where I find this, but you can read here what it does.

Well, that’s kind of the big picture of what you have to do to publish stories, remember this isn’t supposed to be a complete tutorial of everything you can do, just a guide based on my personal experience to get started.

Your facebook connect site should be quite complete now that you can signup, invite your friends and write stuff that will appear directly in facebook. Not to mention if you have used all the other resources facebook give us and facebooker facilitate us.

Thanks for reading!

[Post to Twitter] Tweet This Post 

Using Facebooker to make a Rails site with Facebook Connect (Part 2)

Posted by Augusto Guido on April 29, 2009

Hey, thanks for coming back for part 2. I know it took some time to start writing this second part, it’s just that facebook keeps getting better and keeps taking my time away (don’t tell Conrado).

If I remember correctly in Part 1 we ended up with facebooker installed, configured and running. We even added the facebook connect button and explained how to use some of the great facebooker helpers. As promised in my last post we are going to explain a bit how the magic happens with XFBML, invite friends and publish feed items.

XFBML

Facebook uses XFBML as a way for you to incorporate FBML (Facebook Markup Language, an extension to HTML) into an HTML page on a Facebook Connect site or an iframe application. read more here.

This is a typical XFBML tag, it brings up the profile picture of the user with the uid=”12345″.

<fb:profile-pic uid="12345" facebook-logo="true" linked="false" width="300" height="400"></fb:profile-pic>

What’s happening here? Facebook is turning this into a typicall HTML <img> tag. They do this using a Javascript cross-domain communications library. You can read more here if you are interested. This is all done for you when using Facebooker.

Boring right? Let’s invite some friends to our connect site to keep things more interesting.

Inviting Friends

If you went through the facebooker helpers you are probably thinking about using fb_multi_friend_selector to select friends. Well you are right! We’ll be using that helper, but we will need it inside another helper that is fb_request_form. There’s also another helper that could help us that is fb_multi_friend_request, which is basically the first two together, but we are going to use the first option. Here’s the resulting code:

<% fb_serverfbml do %>
<script type="text/fbml">
<fb:fbml>
<% content_for("invite_user") do %>
<%= "Check out my brand new FB Connect site.  Lots of good stuff in there! #{fb_req_choice('Check it out!', login_users_url)}" %>
<% end %>
<% fb_request_form("GetUnbored","invite_user", login_users_url) do %>
<%= fb_multi_friend_selector("Invite your friends to check out this site", :showborder => true,
:exclude_ids => facebook_session.user.friends_with_this_app.map(&:id).join(","), :condensed => false) %>
<% end %>
</fb:fbml>
</script>
<% end %>

I don’t want to explain something that’s already out there in many places, I will just describe a bit what’s going on. We put things inside a fb_serverfbml, because we are in a facebook connect site and need users interaction with facebook directly (that is when selecting their friends). The content_for(”invite_user”) is the content that will be show in the fb_request_form. The fb_request_form is a facebook form used when we need to submit information to facebook.

And the fb_multi_friend_selector is the nice facebook like friend selector. You can choose condensed => true to show an ugly smaller one. The exclude_ids => facebook_session.user.friends_with_this_app.map(&:id).join(”,”) is pretty great, it makes the friend selector not to show the friends who are already using our facebook connect site.

Anyway, you can get much more things done, here are some of the facebooker helpers to do anything you like with them ;). And of course the facebook developers wiki is a great place for starting and becoming a guru. I will add in other post the publishing feeds part.

Enjoy!

[Post to Twitter] Tweet This Post 

Transferring directory trees over ftp

Posted by Gian Zas on April 19, 2009

Imagine all the people having to live without ssh and an assigned job related to hosting migrations with low bandwidth access, what a painfull world…

Ok, get up! it was only a nightmare, but if a strange reason causes you must accomplish that task you may wish get/put directories recursively from/to a ftp server.

The bad news are that the FTP protocol doesn’t supports this operation, you can get only an individual file or a group of files that expands some wildcard expression, but you can’t get/put recursively an entire directory.

Luckily ncftp saves the day, it’s a free ftp client (free as in beer and free as non-private), that supports many features like background processing and directory tree copy.

So, to GET the contents a whole directory tree just invoke ncftpget command:

$> ncftpget -R -u <user_account> ftp.moove-it.com /home/gian/migration_h /remote_directory

* -R copy a whole directory
* ftp.xxx.x is the remote ftp server
* /home/yyy is the local destination
* and /remote_xx is the ftp directory to be transfered

PUT a directory is trivial too, just invoke the ncftpput command:

$> ncftput -R -u <user_account> ftp2.moove-it.com /home/gian/migration_h /remote_directory

be happy!

[Post to Twitter] Tweet This Post 

Trick to improve performance in rails, less requests with static resources

Posted by Pablo Ifran on April 08, 2009

Reducing the number of request made to the server improves the performance of a web application in about 80%.

There are many techniques that allow us to reduce the amount of requests that are made on a page, among them are: the sprites, put the stylesheets on top of the page, javascripts compress, among others.

But what’s offered by Rails to improve the performance of our web application?

It offers a great plugin called bundle_fu (http://code.google.com/p/bundle-fu/)
It allows us with a single request obtain all the javascripts and with another request all the stylesheets  (it also offers the possibility of compress javascripts).
Using this plugin is really easy but it’s very powerfull

<% bundle do -%>
  <%= javascript_include_tag :default -%>
  <%= javascript_include_tag "javascript1" -%>
  <%= javascript_include_tag "javascript2" -%>
  <%= javascript_include_tag "javascript3" -%>
  <%= stylesheet_link_tag "style1" -%>
  <%= stylesheet_link_tag "style2" -%>
  <%= stylesheet_link_tag "style3" -%>
  ...
<% end %>

All these javascripts and stylesheets are converted in only two files when the request is processed.

[Post to Twitter] Tweet This Post 

moove-iT@locosxrails

Posted by Martin Cabrera on April 07, 2009

Locos x Rails is the first conference in the Southern Cone dedicated to the ground-breaking Ruby on Rails framework. Locos por Rails Conference 2009 will be held on April 3rd and 4th in Buenos Aires, Argentina. South America’s most popular travel destination is the perfect backdrop for two days of local and international presentations, networking, and fun.

Part of the moove-iT development team attended to this event. Bellow you will find some nice pictures!

See more photos on facebook group and keep the thread news at locosxrails twitter.

[Post to Twitter] Tweet This Post 

Using Facebooker to make a Rails site with Facebook Connect (Part 1)

Posted by Augusto Guido on April 06, 2009

I think that from the title you can pretty much guess what this post will be about, specially if you are familiar with these magic words: Rails, Facebooker, Facebook Connect. Ok they are not that magic, but you can do lots of fun stuff with them. In case you don’t live in the facebook planet I will briefly explain them, since there is a lot of info out there about them I wont get into detail. I won’t explain Rails for obvios reaons.

Facebook Connect

It’s something (who knows what and who cares anyway?) from Facebook that allows you to use your facebook login to login into other web sites.

Facebooker

It’s a gem for Ruby, and also a plugin for Rails that converts the results from the facebook API into ruby objects so you can interact with the API using just ruby.

Let the magic begin…

We want to have a site that handles users like we would normally do, except we won’t handle nor model them. We will get them from facebook. The first thing we want to do is join the developers group in facebook, go to http://www.facebook.com/developers/ and create a new application. You will then have to configure some couple of things, the main one is “Connect URL” in which you should put the url of your site like: “http://my_new_fb_app.com/”. Do not forgett the last slash, it won’t work if it’s not there (and you can spend hours trying to figure out what’s wrong). You have many other setting but we won’t get to them here.

As an advice, you may want to create two apps so that you can have one for development and one for production. the development one for example can point to http://localhost:3000/

Other thing you may see is that facebook gives you some strange numbers after you create your app, now is when we start with facebooker. Follow this tutorial until point 5, we won’t care too much for the other stuff, but you can read it. I want you to know how to install facebooker (and to actually do it),  and then generate and complete the facebooker.yml file.

Now what?

After you have that completed the first thing we wan’t is to show this little pretty blue button  facebook connect. This is the login button, and when you click it a pop up from facebook asking your password and email should appear. So, let’s make that happen.

Add this to your application controller

before_filter :set_facebook_session
helper_method :facebook_session

Add the followng lines to one of your views (typically a login page)

<%= fb_connect_javascript_tag %>
<%= init_fb_connect "XFBML"%>
<%= fb_login_and_redirect(facebook_login_users_path) %>

Now you should be seeing the button after you refresh. The facebook_login_users_path is the url you want to redirect your users after they login. As usual in Rails facebook_login is the action and users the controller.

After the user logges in you will have a facebook_session variable abailable to do almost whatever you want. For example you can:

facebook_session.user, this will return you the facebook user and all of it’s methods. Everything you can do with it it’s here http://facebooker.rubyforge.org/classes/Facebooker/User.html. The typicall things you may want are facebook_session.user.first_name, facebook_session.user.last_name, facebook_session.user.first_name.id (this will return an id facebook provides. You can access with it the user any other time).

Other interesting thins you can do is facebook_session.friends to get all the user friends or  facebook_session.user.friends_with_this_app will return all the users that use this application and are already your friends in facebook.

Well, that’s it for now. In part 2 we will discuss a bit about XFBML (facebook markup language) and show you how to invite friends to your app, show stories in their dashboard, etc. Also a little example of using javascript to call the Facebook API.

[Post to Twitter] Tweet This Post 

Improving the ruby Array class without degrade performance

Posted by Pablo Ifran on April 06, 2009

Improving the ruby Array class

Many times, when you are iterating over an array you want to know the last or the first element to do something. Unfortunately ruby don’t provide any method to help you doing this task.
So, if you want a trick to do this, you can add this few lines in a rails project inside an initalizer.

Put this code in initializers/array.rb

class Array

  #     array.each_with_first {|item, first| block }   ->   array
  #
  #
  # Call a block ones per element, passing
  # the element and the first element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_first {|x, f| print "#{x} -- #{f} | " }
  #
  # produces:
  #
  #    a -- a | b -- a | c -- a |
  #
  def each_with_first(&block)
    first = self.first

    self.each do |element|
      yield element, first
    end
  end

  #     array.each_with_last {|item, last| block }   ->   array
  #
  #
  # Call a block ones per element, passing
  # the element and the last element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_last {|x, f| print "#{x} -- #{f} | " }
  #
  # produces:
  #
  #    a -- c | b -- c | c -- c |
  #
  def each_with_last(&block)
    last = self.last

    self.each do |element|
      yield element, last
    end
  end

  #     array.each_with_first_and_last {|item, first, last| block }   ->   array
  #
  #
  # Call a block ones per element, passing
  # the element, the first and the last element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_last {|x, f, l| print "#{x} -- #{f} -- #{l} | " }
  #
  # produces:
  #
  #    a -- a -- c | b -- a -- c | c -- a -- c |
  #
  def each_with_first_and_last(&block)
    first = self.first
    last = self.last

    self.each do |element|
      yield element, first, last
    end
  end

end

[Post to Twitter] Tweet This Post 

Hotmail sends my mails to junk! why?

Posted by Ariel Ludueña on April 03, 2009

spamOk, first of all i’m a good guy and I’m not a spammer. I’m simply developing a SN and I want to send mails between its members.
I have everything nicely configurated (SPF, DomainKeys, reverse DNS for the mail server, all the stuff). Why do my mails go to the junk folder?

Hotmail has as “smart” guy called the SmartScreen filter. This guy is responsible for filtering the mails based on different patterns: email content, IP reputation, … if this guy marks you as a possible spammer you’re in trouble.
The only way that you have is to talk with the hotmail guys.

To do that, submit a form with your mail server information => https://support.msn.com/eform.aspx?productKey=senderid&ct=eformts
Hotmail guys will contact you for further information regarding your site and your mail policy.
After 4 or 5 mails between you and hotmail support, if it’s all okay, they will  help you with a mitigation to your IP :)
“I am pleased to inform you that we have taken steps to implement a temporary mitigation to your mail delivery problem on your IP (206.212.246.210). The mitigation will take 24-48 hours to reflect fully within our system.”

Congrats!

I also suggest to enroll to the Sender Score program at http://www.senderscorecertified.com (the only white list that Hotmail uses).

Good luck!

[Post to Twitter] Tweet This Post 


Tweet This Post links powered by Tweet This v1.3.9, a WordPress plugin for Twitter.