英文:
Java Timer logic
问题
这是一些代码,我找到了其中需要为每个查询测量查询执行时间的部分。
为了测量,使用了 metricRegistry.timer
,但是它是如何工作的?它测量哪个时间?因为我找不到任何定义停止时间的代码行,只有这一行。
有人能帮助我吗?在互联网上找不到任何信息。
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();
}
}
}
英文:
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();
}
}
}
答案1
得分: 0
A Timer.Context
是一个 Closeable
,这意味着当你在像你这里一样的 try
块中使用它时,它的 close()
方法会在退出 try
块时被调用。此外,根据 Timer.Context
的 Javadocs(https://metrics.dropwizard.io/3.1.0/apidocs/com/codahale/metrics/Timer.Context.html),调用 close()
就相当于调用 stop()
。因此,基本上在退出 try
块时会在计时器上调用 stop()
。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论