On 3/8/07, Jeremy Kemper <jeremy / bitsweat.net> wrote:
> On 3/7/07, Brian Adkins <lojicdotcomNOSPAM / gmail.com> wrote:
> > zdennis wrote:
> > > On Mar 6, 10:30 pm, Brian Adkins <lojicdotcomNOS... / gmail.com> wrote:
> > >> When running a test that primarily involves loading up a few MySQL
> > >> tables with ActiveRecord objects, I was surprised to see the Ruby CPU
> > >> utilization at 93% and the MySQL CPU utilization at 7%. I would expect
> > >> this workload to be heavier on MySQL than that.
> > >
> > > What is your script doing? Can you post it?
> >
> > I created a smaller test that I could post that exhibits the same
> > characteristics:
> >
> > class PerfTestController < ApplicationController
> >    def index
> >      t1 = Time.now
> >      3000.times do
> >        member = Member.new
> >        member.first_name = 'Fred'
> >        member.last_name = 'Flintstone'
> >        member.address1 = '123 High St.'
> >        member.city = 'Reykjavik'
> >        member.state = 'Michigan'
> >        member.email = 'fred / flintstone.com'
> >        member.save!
> >      end
> >      t2 = Time.now
> >      puts "Time elapsed = #{t2-t1}"
> >    end
> > end
> >
> > That took 35.7 seconds (84 inserts per second) on a dual core 2 GHz AMD
> > Opteron. It pegged Mongrel and MySQL didn't break a sweat.
> >
> > I just ran another test with a short ruby program inserting records
> > directly using the mysql gem and it only took 1.6 seconds (1,875 inserts
> > per second!), and the CPU utilization was as it should be - the MySQL
> > CPU was ten times as much as Ruby. So it definitely appears that
> > Rails/ActiveRecord is about 22 times as slow than a straight Ruby
> > program - wow!
> >
> > This result makes me feel much better since the performance of Ruby
> > seems fine. The fact that Rails/ActiveRecord is way slow isn't hurting
> > me yet, and there is hope it can be sped up since it doesn't appear to
> > be an inherent problem with Ruby.
> >
> > Here's the schema for Member:
> >
> > create table members (
> >    id                    int           not null auto_increment,
> >    created_at            datetime      not null,
> >    updated_at            datetime      not null,
> >    first_name            varchar(30)   null,
> >    last_name             varchar(30)   null,
> >    address1              varchar(50)   null,
> >    address2              varchar(50)   null,
> >    city                  varchar(30)   null,
> >    state                 varchar(5)    null,
> >    email                 varchar(100)  null,
> >    home_phone            varchar(25)   null,
> >    primary key(id)
> > ) engine=InnoDB;
>
>
> Hi Brian,
>
> I wrapped this up in a simple script that anyone with MySQL or SQLite
> and the AR gem can run. It benchmarks AR create vs using the db
> connection directly. See attached.
>
> Excerpted results on a new MacBook Pro:
>                           user     system      total        real
> raw quoted            0.460000   0.000000   0.460000 (  0.480184)
> create                2.760000   0.080000   2.840000 (  3.225227)
>
> (Nearly 7 times slower.)  I haven't tried profiling the methods yet.
>
> In my experience with typical Rails apps, you'll hit a wall with ERB
> template rendering much sooner than with Active Record creation. This
> is an interesting pursuit nonetheless -- I'm interested to see what
> you all come up with.
>
> Best regards,
> jeremy


Jeremy

Are there, or could there be, performance tests like this added to the
Rails test suite?  It would be great to be able to track performance
like this over Rails releases.

- Rob