Why is the destroy benchmark Activerecord/sqlite3 destroy bench creating

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.

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:

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

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

1 Like