This is some code which i found where we need to measure the query execution time for each queries ?
For measuring that metricRegistry.timer
is used but how it works which time it measures as i can't find any line of code which define stop time it just this line?
can anybody help me with that can't find anything on the internet ?
try (PreparedStatement ps = connection.prepareStatement(QUERY.SQL())) {
try (
Timer.Context ignored = metricRegistry.timer(name("query", QUERY.NAME())).time();
ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
successCounter.inc();
} else {
failedCounter.inc();
}
}
}
A Timer.Context
is a Closeable
, which means that when you use it in a try
block like you have here, its close()
method is called when the try
block is exited. Furthermore, per the Javadocs for Timer.Context
(https://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/Timer.Context.html), calling close()
is like calling stop()
. So basically, stop()
is called on the timer when the try
block is exited.