how to measure performance in hibernate? I want to know that how much time hibernate takes to execute a query?
问题:
回答1:
JProfiler 7.1 has a JPA/Hibernate probe:
http://www.ej-technologies.com/products/jprofiler/whatsnew71.html
Here's a screen cast that shows how it works:
http://blog.ej-technologies.com/2012/01/profiling-jpahibernate.html
The hot spots view looks like this:
Disclaimer: My company develops JProfiler
回答2:
I would recommend one or several of the following options:
- Database profiler (e.q. SqlServer Profiler)
- Hibernate's built-in statistics (see: http://www.javalobby.org/java/forums/t19807.html)
- Hibernate profiler (see: http://hibernateprofiler.com, http://nhprof.com/)
回答3:
Do you mean literally the query or the query + the munging of the data into Java objects?
Either way, using a profiling tool such as JProbe is probably the way to go if you want to be scientific. You don't necesserily need to spend money, there are some free tools in Eclipse too.
In the simplest case, just adding a few print statements around the code in question can be enough, if the the thing you are measuring is the dominant processing.
Getting really thorough in performance analysis takes great care. Often you need to run many repeats of the thing you're testing in order to make sure you don't get mislead by initial overheads (such as opening the connection to the Db). And don't forget that the database and performance is likely to be critical, hibernate itself can't do anything about a badly tuned database.
回答4:
When I do development with Spring Hibernate
I usually use following properties
in my application.properties file to see
query execution time and how query is executed:
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=true
Example log output looks like below:
Hibernate:
select
order0_.OrderID as OrderID1_4_0_,
order0_.customerID as customer2_4_0_,
order0_.employeeID as employee3_4_0_,
order0_.freight as freight4_4_0_,
order0_.orderDate as orderDat5_4_0_,
order0_.requiredDate as required6_4_0_,
order0_.shipAddress as shipAddr7_4_0_,
order0_.shipCity as shipCity8_4_0_,
order0_.shipCountry as shipCoun9_4_0_,
order0_.shipName as shipNam10_4_0_,
order0_.shipPostalCode as shipPos11_4_0_,
order0_.shipRegion as shipReg12_4_0_,
order0_.shipVia as shipVia13_4_0_,
order0_.shippedDate as shipped14_4_0_,
customer1_.customerID as customer1_2_1_,
customer1_.address as address2_2_1_,
customer1_.city as city3_2_1_,
customer1_.companyName as companyN4_2_1_,
customer1_.contactName as contactN5_2_1_,
customer1_.contactTitle as contactT6_2_1_,
customer1_.country as country7_2_1_,
customer1_.fax as fax8_2_1_,
customer1_.phone as phone9_2_1_,
customer1_.postalCode as postalC10_2_1_,
customer1_.region as region11_2_1_
from
orders order0_
left outer join
customers customer1_
on order0_.customerID=customer1_.customerID
where
order0_.OrderID=?
Hibernate:
select
orderdetai0_.OrderID as OrderID6_3_0_,
orderdetai0_.ID as ID1_3_0_,
orderdetai0_.ID as ID1_3_1_,
orderdetai0_.discount as discount2_3_1_,
orderdetai0_.OrderID as OrderID6_3_1_,
orderdetai0_.productID as productI3_3_1_,
orderdetai0_.quantity as quantity4_3_1_,
orderdetai0_.unitPrice as unitPric5_3_1_
from
order_details orderdetai0_
where
orderdetai0_.OrderID=?
Session Metrics {
281879 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
276015 nanoseconds spent preparing 2 JDBC statements;
1702274 nanoseconds spent executing 2 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)}