I'm writing unit tests for a DAO class which I just did some changes with. Since the performance of this DAO is very important, so I want to do some checking about the performance in the unit tests. And because my changes for this DAO could cause multiple times of querying, I want to check if the DAO queries the DB only once. If not, the unit test should fail.
I don't know if this is possible. Please help:)
Testing the performance in unit tests is going to be useless unless you replicate the real DB and it's payload. Of course you could measure the time it takes to execute the query, but that is going to the result of what? The query that you execute in unit tests, how is that going to translate to the ones for the real DB?
What you can measure is how your new query changed against the old one in unit tests. I would use a framework for that (you have to take in account the JVM warm-up here and may be the framework has some caching also that needs to warm up), I would recommend : Caliper
Also if you have queries that need to be executed only once against the DB, sounds very much like you need a cache. If it's not too complicated you could look at
Cache
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With