Why is the destroy benchmark Activerecord/sqlite3 destroy bench creating


#1
Benchmark.rails("activerecord/sqlite3_destroy", time: 10) do
   user = User.create!(attributes)
   user.destroy
 end

This looks really weird to me. Why is the destroy benchmark creating, its not benchmarking the correct operation.


#2

I created the user within the benchmark because on the first iteration, the user would have already been destroyed from the database and the second destroy does not end up hitting the DB…I might be wrong :frowning:


#3

Either:

user = nil
Benchmark.rails("activerecord/sqlite3_destroy", before: lambda {
 user = User.create!(attributes)
}) do
    user.destroy
 end

Or:

iterations = 10000

users = [0..(iterations + warmup)].map{ User.create!(attributes)}

Benchmark.rails("activerecord/sqlite3_destroy", iterations: iterations) do
    users.first.destroy
end

#4

Great suggestion, I’ll improve the helper in this way.