Benchmarking ORM associations
- by barerd
I am trying to benchmark two cases of self referential many to many as described in datamapper associations. Both cases consist of an Item clss, which may require many other items. In both cases, I required the ruby benchmark library and source file, created two items and benchmarked require/unrequie functions as below:
Benchmark.bmbm do |x|
x.report("require:") { item_1.require_item item_2, 10 }
x.report("unrequire:") { item_1.unrequire_item item_2 }
end
To be clear, both functions are datamapper add/modify functions like:
componentMaps.create :component_id => item.id, :quantity => quantity
componentMaps.all(:component_id => item.id).destroy!
and
links_to_components.create :component_id => item.id, :quantity => quantity
links_to_components.all(:component_id => item.id).destroy!
The results are variable and in the range of 0.018001 to 0.022001 for require function in both cases, and 0.006 to 0.01 for unrequire function in both cases. This made me suspicious about the correctness of my test method.
Edit
I went ahead and compared a "get by primary key case" to a "finding first matching record case" by:
(1..10000).each do |i|
Item.create :name => "item_#{i}"
end
Benchmark.bmbm do |x|
x.report("Get") { item = Item.get 9712 }
x.report("First") { item = Item.first :name => "item_9712" }
end
where the results were very different like 0 sec compared to 0.0312, as expected. This suggests that the benchmarking works.
I wonder whether I benchmarked the two types of associations correctly, and whether a difference between 0.018 and 0.022 sec significant?