39
http://AgileArtisans.co Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Embed Size (px)

Citation preview

Page 1: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

http://AgileArtisans.com

Use C to Tune Your Rails Application

by Jared RichardsonPragmatic author, process consultant, rails developer, and speaker

Page 2: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Why Bother?

• Existing Libraries

• Legacy Hardware Libraries

• Re-write Hot Spots

Page 3: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Three Parts

• Ruby C Extensions

• InlineRuby

• Rails and C

Page 4: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Tools We’ll Use

• ruby

• rails

• gcc

• mkmf

• Ruby Inline

Page 5: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby C Extension

• Fairly easy

• Use a few conventions

• Can even share variables

Page 6: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

What We Need

• C code

• Make file

• Ruby code

Page 7: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

C Code#include "ruby.h"

static VALUE cSampleC;

static VALUE exec_me(VALUE self) {

printf("\n\n exec_me, written in C, run from Ruby! \n\n");

return Qnil;

}

void Init_SampleC() {

cSampleC = rb_define_class("SampleC", rb_cObject);

rb_define_method(cSampleC, "exec", exec, 0);

}

Page 8: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

C Code#include "ruby.h"

static VALUE cSampleC;

static VALUE exec_me(VALUE self) {

printf("\n\n exec_me, written in C, run from Ruby! \n\n");

return Qnil;

}

void Init_SampleC() {

cSampleC = rb_define_class("SampleC", rb_cObject);

rb_define_method(cSampleC, "exec", exec, 0);

}

Page 9: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

C Code#include "ruby.h"

static VALUE cSampleC;

static VALUE exec_me(VALUE self) {

printf("\n\n exec_me, written in C, run from Ruby! \n\n");

return Qnil;

}

void Init_SampleC() {

cSampleC = rb_define_class("SampleC", rb_cObject);

rb_define_method(cSampleC, "exec", exec, 0);

}

Page 10: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

C Code#include "ruby.h"

static VALUE cSampleC;

static VALUE exec_me(VALUE self) {

printf("\n\n exec_me, written in C, run from Ruby! \n\n");

return Qnil;

}

void Init_SampleC() {

cSampleC = rb_define_class("SampleC", rb_cObject);

rb_define_method(cSampleC, "exec", exec, 0);

}

Page 11: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

C Code#include "ruby.h"

static VALUE cSampleC;

static VALUE exec_me(VALUE self) {

printf("\n\n exec_me, written in C, run from Ruby! \n\n");

return Qnil;

}

void Init_SampleC() {

cSampleC = rb_define_class("SampleC", rb_cObject);

rb_define_method(cSampleC, "exec_me", exec_me, 0);

}

Page 12: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

extconf.rb

require 'mkmf'create_makefile("SampleC")

To create your Makefile…

ruby -r mkmf extconf.rb

Page 13: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Practical Tip

• If mkmf isn't installed, you'll see "ruby: no such file to load -- mkmf (LoadError)"

• "sudo apt-get install ruby1.8-dev"

Page 14: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Code

require "SampleC“

handle = SampleC.new

handle.exec_me

Page 15: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Converting C to Ruby Objects

• INT2NUM(int) => Fixnum

• INT2NUM(long) => Fixnum

• CHR2FIX(char) => Fixnum

• rb_str_new2(char *) => String

• rb_float_new(double) => Float

Page 16: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Objects to C

• int => NUM2INT(Numeric)

• long => NUM2LONG(Numeric)

• char * => STR2CSTR(String)

• double => NUM2DBL(Numeric)

Page 17: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Quick Demo

Page 18: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Inline C

• Ruby gem

• gem install RubyInline

Page 19: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 20: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 21: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 22: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 23: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 24: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 25: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendt = RubyToC.new()t.exec_me

Page 26: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Ruby Inline Coderequire "rubygems"require "inline"class RubyToC inline do |builder| builder.c %Q{ public void exec_me() { printf("Written in C, run from Ruby."); } } endendhandle = RubyToC.new()handle.exec_me

Page 27: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

That’s the Ruby

• What about Rails?

• Find the hot spots

Page 28: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Rails LoggingProcessing MainController#search_students (for 127.0.0.1 at 2007-07-03 15:29:26) [GET] Session ID: b4c3826607bd5ff51f3fa34b45d96c76 Parameters: {"action"=>"search_students", "controller"=>"main"} Student Columns (0.001862) SHOW FIELDS FROM students Student Load (0.000149) SELECT * FROM students WHERE (students.`user_name` = 'jared') LIMIT 1 Student Load (0.000171) SELECT * FROM students WHERE (students.`city` = 'Gotham' AND students. Found 13 matches

Rendering main/indexRendered main/_logo (0.00008)Rendered main/_tabs (0.00012)Rendered main/_top_of_page (0.00167)Rendered main/_search_students (0.00639)Rendered main/_nav_left_hand_side (0.00088)Rendered main/_footer (0.00010)Completed in 0.02753 (36 reqs/sec) | Rendering: 0.01113 (40%) | DB: 0.00298 (10%) | 200 OK

[http://blah_mongrel/main/search_students]

Page 29: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Rails LoggingProcessing MainController#search_students (for 127.0.0.1 at 2007-07-03 15:29:26) [GET] Session ID: b4c3826607bd5ff51f3fa34b45d96c76 Parameters: {"action"=>"search_students", "controller"=>"main"} Student Columns (0.001862) SHOW FIELDS FROM students Student Load (0.000149) SELECT * FROM students WHERE (students.`user_name` = 'jared') LIMIT 1 Student Load (0.000171) SELECT * FROM students WHERE (students.`city` = 'Gotham' AND students. Found 13 matches

Rendering main/indexRendered main/_logo (0.00008)Rendered main/_tabs (0.00012)Rendered main/_top_of_page (0.00167)Rendered main/_search_students (0.00639)Rendered main/_nav_left_hand_side (0.00088)Rendered main/_footer (0.00010)Completed in 0.02753 (36 reqs/sec) | Rendering: 0.01113 (40%) | DB: 0.00298 (10%) | 200 OK

[http://blah_mongrel/main/search_students]

Page 30: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Rails Logging

Completed in 0.02753 (36 reqs/sec) | Rendering: 0.01113 (40%) | DB: 0.00298 (10%) | 200 OK [http://blah_mongrel/main/search_students]

Page 31: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Quick and Dirty

start = Time.now

# run code in here …

done = Time.now

elapsed_time = done - start

logger.info("Spent #{elapsed_time} secconds ")

Page 32: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Tuning Algorithm

• Watch the log

• Find slow controllers

• Zero in with timing statements

• Convert to C

Page 33: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

More on Tuning Rails

http://www.rubyinside.com/how-to-profile-your-rails-application-and-make-rails-go-vroom-565.html

Page 34: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Demo

• Native Ruby on Rails

• Rails with Inline C

• Rails with Ruby C Extension

Page 35: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Rails and Ruby Inline

• Reloads break Inline

• Put code in /lib

• Or run in production mode

Page 36: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Rails and C Extensions

• Be sure to "make install"

• Then require "Sample_C"

Page 37: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Existing C Extensions

• Rexml (ruby) vs. libxml (C)

• 10x faster

Page 38: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker

Web Resources

• http://www.rubycentral.com/pickaxe/ext_ruby.html

• http://www.zenspider.com/ZSS/Products/RubyInline/

• http://www.jaredrichardson.net/blog/2006/03/25/

• http://libxml.rubyforge.org/

• http://agileartisans.com/main/blog/41

• http://www.rubyinside.com/how-to-profile-your-rails-application-and-make-rails-go-vroom-565.html

Page 39: Http://AgileArtisans.com Use C to Tune Your Rails Application by Jared Richardson Pragmatic author, process consultant, rails developer, and speaker