From: Brian Adkins Date: 2007-03-08T13:45:08+09:00 Subject: Re: High ActiveRecord CPU Utilization zdennis wrote: > On Mar 6, 10:30 pm, Brian Adkins 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; > > > >> Has this been other folks' experience? Is running in the test >> environment incredibly different than production with respect to CPU >> utilization? > > I think depends on more of what you're doing and how you're doing it. > I've seen CPU and memory issues with AR before, but these have all > been fixed by understanding how and when to do things in AR (and in > ruby). This also depends on the hardware differences between your > development, test and production environments. Disk speed, memory and > CPU(s) can have alot to do with the change between the environments. > >> I suppose my next step is to run in production to see what >> kind of results I get. > > >> I'm running the test from the root of my Rails project via: >> > > What your program is doing and how it is doing it will give more > insight into your issue then just profiler output. Have you isolated > the problem to a particular block of code? I respectfully disagree. It was the profiler output that showed me where the time was being spent. I truncated the profiler output before it even got to my code - it was all Rails code. > > Zach >