Java Timer logic

huangapple go评论65阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月30日 13:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定