Monday, November 03, 2008

Hibernate Performance

(This is one of my old Google Notes.)

I have used YourKit to benchmark the performance of Hibernate some time ago.

In my database, I have two tables: Project and Release. Project is associated with release in a one-to-many relationship. In my setting, a SQL statement using JDBC costs from several milliseconds to ~30 milliseconds; serving a JSP costs from 1 second to 3 seconds in the first run, then from tens of milliseconds to ~250 milliseconds in the later run.

In Hibernate,
  • Getting a single project (as well as its releases) costs 234ms in the first run, and 14ms in average (excluding the first run);
  • Getting all projects costs 547ms in the first run, and 96ms in average;
  • Getting some partial information of all projects, costs 282ms in the first run, and 15ms in average.
The reason for that the first run is much slower, is because Hibernate does bytecode instrumentation to generate proxy object on the first touch of ORM, which is quite expensive. However, the late runs approximate JDBC's performance.

Some Hibernate tips:
  • Chapter 19 of the Hibernate reference describes how to improve its performance.
  • Use set instead of list. List uses the id as index, thus creates a large data structure with lots of empty cells.
  • Associated objects are initialized in a lazy way. They need to be touched to bring into memory.

No comments: