0

I am using datamapper and want to benchmark some queries using ruby benchmark library. To see if it's working I started with a set, comparing methods described in datamapper docs as given below:

(1..1000).each do |i|
  i = Item.create :name => "item_#{i}", :weight => 0.5, :volume => 1.2, :price => 15
end

Benchmark.bmbm do |x|
  x.report("all:") { Item.all }
  x.report("name_only:") { Item.all :fields => [:name] }
  x.report("direct_talk_all:") { repository(:default).adapter.select('SELECT * FROM items') }
  x.report("direct_talk_name_only:") { repository(:default).adapter.select('SELECT name FROM items') }

end

and surprisingly (to me), the real results were 0, 0.001, 0.024 and 0.014, respectively. It sounds wrong to me that a selective SELECT query costs more time in case I use datamapper-specific code, and that queries take longer when talking directly to the data store.

So, should I go on benchmarking queries this way, or is ruby benchmark library a wrong choice for this?

barerd
  • 835
  • 3
  • 11
  • 31

1 Answers1

2

Item.all doesn't actually do the query, it's delayed until needed. Use Item.all.to_a to force the query.

ujifgc
  • 2,215
  • 2
  • 19
  • 21
  • Thank you! Now that the results are 0.43, 0.19, 0.03 and 0.02, respectively, I see that a selective query is 2 times faster, and that a direct talk is much much faster. – barerd Nov 14 '12 at 10:38