<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mission Data Blog &#187; chuckf</title>
	<atom:link href="http://www.missiondata.com/blog/author/chuckf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.missiondata.com/blog</link>
	<description>Louisville-based Web Development &#038; Software Engineering</description>
	<lastBuildDate>Tue, 18 May 2010 14:24:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby&#8217;s DBI or ActiveRecord</title>
		<link>http://www.missiondata.com/blog/web-development/41/rubys-dbi-or-activerecord/</link>
		<comments>http://www.missiondata.com/blog/web-development/41/rubys-dbi-or-activerecord/#comments</comments>
		<pubDate>Sun, 09 Apr 2006 18:35:18 +0000</pubDate>
		<dc:creator>chuckf</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=41</guid>
		<description><![CDATA[While creating a small CGI web application in Ruby the question came up about how much slower ActiveRecord is than DBI. Considering that ActiveRecord is initialized each time the CGI runs the performance hit could be significant.
So why not use Rails instead? The server that I am running the application on has limited resources. Rails [...]]]></description>
			<content:encoded><![CDATA[<p>While creating a small CGI web application in Ruby the question came up about how much slower ActiveRecord is than DBI. Considering that ActiveRecord is initialized each time the CGI runs the performance hit could be significant.</p>
<p>So why not use Rails instead? The server that I am running the application on has limited resources. Rails is reported to need about 20 MB of memory and the node I have is already paging out memory to disk.</p>
<p>To emulate the webserver getting hit repeatedly with connections I created a connect_dbi.rb and and connect_ar.rb that insert the same row into a Tests database table. The ActiveRecord test also has the model file test.rb</p>
<p>The test was run using a PostGreSQL database on Ubuntu 5.10 with Ruby 1.8.4. The table was truncated between tests. I just wanted to get an idea of the performance difference between ActiveRecord and DBI. So this isn&#8217;t the strictest performance test but does provide an idea of the magnitude of difference.</p>
<p><span id="more-41"></span><br />
The test was executed from the command line using &#8220;ruby -e&#8221; to call the connect_dbi.rb and connect_ar.rb files 100 times each. This creates a new database connection each time the script is executed. ActiveRecord generates a series of error messages about unavailable database adapters each time it is started so these messages were capatured in the activeError file. DBI doesn&#8217;t produce any errors but I wanted to keep the execution as similar as possible so there is also a dbiError file created.</p>
<h3>connect_dbi.rb</h3>
<pre><code>#! /usr/bin/ruby -w
require 'rubygems'
require 'dbi'

dbh = DBI.connect('dbi:Pg:tmp_test:localhost','ruby_user','ruby_pass')

begin

dbh.transaction do |dbh|
dbh.prepare("INSERT INTO tests(rank, created_at, message) VALUES(?,?,?)") do |sth|
sth.execute(5, Time.now, 'abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz ')
end
end

rescue DBI::ProgrammingError =&gt; exception
puts "Exception: #{exception.message}"
end
</code></pre>
<h3>connect_ar.rb</h3>
<pre><code>#! /usr/bin/ruby -w

require 'rubygems'
require_gem 'activerecord'

ActiveRecord::Base.establish_connection(
:adapter  =&gt; "postgresql",
:host     =&gt; "localhost",
:username =&gt; "ruby_user",
:password =&gt; "ruby_pass",
:database =&gt; "tmp_test"
)

test = Test.new("rank" =&gt; 5, "created_at" =&gt; "#{Time.now}", "message" =&gt; 'abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz ')
test.save</code></pre>
<h3>test.rb</h3>
<pre><code>class Test &lt; ActiveRecord::Base

end
</code></pre>
<p>The GNU/Linux time command was used to obtain timings.</p>
<p>As the results show DBI appears to be about three times faster than using ActiveRecord. This is most likely because of the initialization ActiveRecord does each time the connection is established. Considering that the test is only using one table and one ActiveRecord model class this is a fairly significant difference.</p>
<pre><code>~/projects/dbi_test $ time ruby -e '100.times { %x{./connect_ar.rb}}' 2&gt; activeError

real    0m25.916s
user    0m21.575s
sys     0m2.807s

~/projects/dbi_test $ time ruby -e '100.times { %x{./connect_dbi.rb}}' 2&gt; dbiError

real    0m8.154s
user    0m6.620s
sys     0m0.733s</code></pre>
<pre>All results are in seconds.  Each test was run ten times.
<h3>DBI Results</h3>
<table cellspacing="0" cellpadding="2" border="1">
<tr>
<th></th>
<th>Mean</th>
<th>Standard Deviation</th>
</tr>
<tr>
<td>Real</td>
<td>7.7133</td>
<td>0.9448</td>
</tr>
<tr>
<td>User</td>
<td>6.2027</td>
<td>0.8530</td>
</tr>
<tr>
<td>Sys</td>
<td>6.2027</td>
<td>0.2760</td>
</tr>
</table>
<h3>ActiveRecord Results</h3>
<table cellspacing="0" cellpadding="2" border="1">
<tr>
<th></th>
<th>Mean</th>
<th>Standard Deviation</th>
</tr>
<tr>
<td>Real</td>
<td>26.9003</td>
<td>1.8849</td>
</tr>
<tr>
<td>User</td>
<td>21.9256</td>
<td>1.4959</td>
</tr>
<tr>
<td>Sys</td>
<td>2.8038</td>
<td>0.4610</td>
</tr>
</table>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/web-development/41/rubys-dbi-or-activerecord/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
