<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <generator>Persumi - Level up your writing and blogging with AI</generator>
  <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
  <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/protip"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/protip/feed/rss"/>
  <link rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/protip/feed/atom"/>
  <author>
    <name>Fred Wu</name>
    <email>ifredwu@gmail.com</email>
    <uri>http://persumi.com/u/fredwu</uri>
  </author>
  <subtitle/>
  <id>http://persumi.com/u/fredwu/tech/e/blog/t/protip</id>
  <title>Blog (protip) - Fred Wu&apos;s Tech</title>
  <updated>2026-05-25T21:08:53.451021Z</updated>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
&lt;em&gt;Please also see &lt;a href=&quot;/blog/2013-09-06-protip-ruby-devs-please-tweak-your-gc-settings/&quot;&gt;this blog post on tweaking your ruby GC settings&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
I use and love &lt;a href=&quot;https://github.com/bmabey/database_cleaner&quot;&gt;DatabaseCleaner&lt;/a&gt;, although historically I had never paid too much attention on the performance of its varies cleaning strategies - I’d always used &lt;code class=&quot;inline&quot;&gt;truncation&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;
We use Postgres, and after digging around and finding out &lt;a href=&quot;http://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886&quot;&gt;the difference between DELETE and TRUNCATE&lt;/a&gt;, I ended up improving our test suite speed by about 30-40% simply by tweaking the cleaning strategies.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;RSpec.configure do |config|
  config.before :suite do
    DatabaseCleaner.clean_with :truncation
    DatabaseCleaner.strategy = :transaction
  end

  config.before do
    if example.metadata[:js] || example.metadata[:type] == :feature
      DatabaseCleaner.strategy = :deletion
    else
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.start
    end
  end

  config.after do
    DatabaseCleaner.clean
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Essentially, we want to &lt;code class=&quot;inline&quot;&gt;truncate&lt;/code&gt; the DB only once before the whole suite runs to ensure a clean slate DB, then we only want to use &lt;code class=&quot;inline&quot;&gt;deletion&lt;/code&gt; on Capybara tests, everything else should just use &lt;code class=&quot;inline&quot;&gt;transaction&lt;/code&gt; which is the fastest strategy.&lt;/p&gt;
&lt;p&gt;
Now, as a bonus, I have just discovered @amatsuda’s &lt;a href=&quot;https://github.com/amatsuda/database_rewinder&quot;&gt;DatabaseRewinder&lt;/a&gt; which is a lightweight alternative that supports only ActiveRecord. It offers comparable performance with a much similar API.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;RSpec.configure do |config|
  config.before :suite do
    DatabaseRewinder.clean_all
  end

  config.after do
    DatabaseRewinder.clean
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
By the way, we also use &lt;a href=&quot;https://github.com/grosser/parallel_tests&quot;&gt;parallel_tests&lt;/a&gt; to scale our test suite to multiple processes, even on Travis CI and Wercker.&lt;/p&gt;
&lt;p&gt;
Hooray to faster tests! :)&lt;/p&gt;
]]&gt;</content>
    <published>2013-09-18T06:50:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/protip-faster-ruby-tests-with-databasecleaner-and-databaserewinder"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/protip-faster-ruby-tests-with-databasecleaner-and-databaserewinder</id>
    <title>Protip: Faster Ruby Tests with DatabaseCleaner and DatabaseRewinder</title>
    <updated>2013-09-18T06:50:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
It was made apparent to me that many ruby devs either aren’t aware or couldn’t be bothered to tweak their ruby garbage collector settings.&lt;/p&gt;
&lt;p&gt;
Well, if you are using MRI, please start tweaking your GC settings. Here’s what I use (on my 15” Macbook Pro Retina):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;shell language-shell&quot;&gt;export RUBY_GC_MALLOC_LIMIT=90000000
export RUBY_FREE_MIN=200000&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Not only can you tweak it for your local dev machine, you can also tweak Jenkins, &lt;a href=&quot;http://travis-ci.com/&quot;&gt;Travis CI&lt;/a&gt;, &lt;a href=&quot;http://wercker.com/&quot;&gt;Wercker&lt;/a&gt; and other CI solutions, making instant speed gain for your test suite!&lt;/p&gt;
&lt;p&gt;
Here’s what we get:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;shell language-shell&quot;&gt;            Before After
Local:      8m22s 5m52s
Travis CI:  10m10s 6m0s
Wercker:    8m45s 6m24s&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
YMMV depending on your system and available RAM.&lt;/p&gt;
]]&gt;</content>
    <published>2013-09-06T11:13:37.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/protip-ruby-devs-please-tweak-your-gc-settings-for-tests"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/protip-ruby-devs-please-tweak-your-gc-settings-for-tests</id>
    <title>Protip: Ruby Devs, Please Tweak Your GC Settings for Tests!</title>
    <updated>2013-09-06T11:13:37.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
I haven’t really used &lt;a href=&quot;http://sequel.rubyforge.org/&quot;&gt;Sequel&lt;/a&gt; much therefore I am definitely a newbie. However, after days and nights of frustration, endless debugging and some search-fu during the development of &lt;a href=&quot;https://github.com/fredwu/datamappify&quot;&gt;Datamappify&lt;/a&gt;, I have finally arrived at the conclusion that Sequel is a capable library, as long as you are aware of the gotchas.&lt;/p&gt;
&lt;h3&gt;
Gotcha 1: Always use “&lt;code class=&quot;inline&quot;&gt;select&lt;/code&gt;“/“&lt;code class=&quot;inline&quot;&gt;select_all&lt;/code&gt;“, or your data records will mysteriously have wrong IDs!&lt;/h3&gt;
&lt;p&gt;
In ActiveRecord, joining an associated model couldn’t be simpler:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.joins(:author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
In Sequel, despite having a similar API for models to declare associations and their corresponding primary and foreign keys, you cannot do a &lt;code class=&quot;inline&quot;&gt;join&lt;/code&gt; without specifying the keys:&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Not good:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.join(:authors)
# or
Post.join(Author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;em&gt;Better:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.join(:authors, :id =&gt; :author_id)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
You would think the version above works - it doesn’t. Even worse, the above example &lt;strong&gt;will give you incorrect data&lt;/strong&gt; - the IDs of the Post records will now contain the IDs from their corresponding Author records! This is because upon a &lt;code class=&quot;inline&quot;&gt;join&lt;/code&gt;, Sequel merges attributes from both models into a single hash.&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;The correct version:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.join(:authors, :id =&gt; :author_id).select(:posts __id, :posts__ title, :posts__body)
# or
Post.join(:authors, :id =&gt; :author_id).select_all(:posts)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_inline_mrvsa2sBB81qz4rgp.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;h3&gt;
Gotcha 2: Always call “&lt;code class=&quot;inline&quot;&gt;all&lt;/code&gt;“ at the end of the chain, or the chain will present data in a different format.&lt;/h3&gt;
&lt;p&gt;
In ActiveRecord, all of the below examples return an &lt;code class=&quot;inline&quot;&gt;ActiveRecord::Relation&lt;/code&gt; collection:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;)
Post.joins(:author)
Post.includes(:author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
And indeed, calling &lt;code class=&quot;inline&quot;&gt;first&lt;/code&gt; on any of them returns an object of class &lt;code class=&quot;inline&quot;&gt;Post&lt;/code&gt; (assuming the result collection is not empty).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;).first.class #=&gt; Post
Post.joins(:author).first.class #=&gt; Post
Post.includes(:author).first.class #=&gt; Post&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
In Sequel, the below examples all return a &lt;code class=&quot;inline&quot;&gt;Sequel::DataSet&lt;/code&gt; collection:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;)
Post.eager(:author)
Post.eager_graph(:author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
But let’s see what we get from calling &lt;code class=&quot;inline&quot;&gt;first.class&lt;/code&gt; on them:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;).first.class #=&gt; Post
Post.eager(:author).first.class #=&gt; Post
Post.eager_graph(:author).first.class #=&gt; Hash&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Huh? Last one is a &lt;code class=&quot;inline&quot;&gt;Hash&lt;/code&gt;? It turns out, if you call &lt;code class=&quot;inline&quot;&gt;all&lt;/code&gt; at the end of chains to convert them to &lt;code class=&quot;inline&quot;&gt;Array&lt;/code&gt;s, then the returned collections are consistent:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;).all.first.class #=&gt; Post
Post.eager(:author).all.first.class #=&gt; Post
Post.eager_graph(:author).all.first.class #=&gt; Post&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_inline_mrvsa2sBB81qz4rgp.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
]]&gt;</content>
    <published>2013-08-21T12:39:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/gotchas-in-the-ruby-sequel-gem"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/gotchas-in-the-ruby-sequel-gem</id>
    <title>Gotchas in the Ruby Sequel Gem</title>
    <updated>2013-08-21T12:39:00.000000Z</updated>
  </entry>
</feed>