neelrotno
neelrotno

Reputation: 169

How to benchmark DB operations using JMH?

Sometimes we have to perform same DB operation multiple times within a loop. How can I compute the execution time for each operation using JMH?

public void applyAll(ArrayList<parameter_type> lists) {
    for(parameter_type param : lists) {
        saveToDB(param);
    }
}

How can I compute the execution time for saveToDB(param) for each time it is being executed/called?

Upvotes: 1

Views: 356

Answers (2)

Imaskar
Imaskar

Reputation: 2939

As @RafaelWinterhalter said, this type of calls are prone to give misleading results in benchmarks. But if you still want to try, then:

  1. Serialize and save a reference list of calls.
  2. Then in a benchmark use a @State(Scope.Thread) object to restore this list to an array and have a loop counter variable there.
  3. Then @Benchmark public int test1_saveToDB(MyState state) { saveToDB(state.params[state.i]); return state.i++; }

Upvotes: 0

Rafael Winterhalter
Rafael Winterhalter

Reputation: 43997

DB operations are really nothing to microbenchmark. Their will depend on multiple things that are quite impossible to isolate.

As for using parameters, have a look at this answer that explains the use of the @Param annotation.

Upvotes: 1

Related Questions