如何减少秒表或系统的调用。
我使用的是秒表(内部使用system.currenttimemillis())
来计算每个API的执行和完成时间。我看到与system.currenttimemillis()
有很多问题。是否有任何其他或更好的方法来计算每个API的时间,并替代System.CurrentTimeMillis()
。 system.nanotime()
使用system.currenttimemillis()
在Linux中进行比较较慢,因此它无法正常工作。
秒表代码: -
public stopwatch{
private final long start;
public Stopwatch() {
start = System.currentTimeMillis();
}
public double elapsedTime() {
long now = System.currentTimeMillis();
return (now - start) / 1000.0;
}}
API代码:
@GetMapping("/endpoint")
public String endPoint(){
StopWatch stopWatch = new StopWatch ();
//Some code
long timeTakenForTApi = stopWatch.elapsedTime();
}
I am using Stopwatch (internally using System.currentTimeMillis())
to calculate every API time for execution and completion. I see that there is lots of issues related to System.currentTimeMillis()
. Is there any alternate or better way to calculate time for every API and alternate to System.currentTimeMillis()
. System.nanotime()
is slower compared using System.currentTimeMillis()
in Linux so it won't work.
StopWatch code:-
public stopwatch{
private final long start;
public Stopwatch() {
start = System.currentTimeMillis();
}
public double elapsedTime() {
long now = System.currentTimeMillis();
return (now - start) / 1000.0;
}}
API code:
@GetMapping("/endpoint")
public String endPoint(){
StopWatch stopWatch = new StopWatch ();
//Some code
long timeTakenForTApi = stopWatch.elapsedTime();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会告诉我们任何具体要求,因此我只能做出有根据的猜测。
我认为,您想为您的API呼叫收集性能指标,并将其提供给某种指标数据分析工具,例如Prometheus,dynatrace或AWS CloudWatch?
如果是这样,我将不会开始重新发明轮子。您是否正在查看 https://micrometer.io ?
然后,您要么按程序上使用
计时器
,要么在控制器方法上使用@Timed
so:so:You don't tell us any specific requirements, so I can only make educated guesses.
I reckon, you want to collect performance metrics for your API calls and feed them to some kind of metrics data analyzing tool like Prometheus, Dynatrace or AWS Cloudwatch?
If that's the case, I wouldn't start reinventing the wheel. Did you have a look at https://micrometer.io?
You would then either use a
Timer
programatically or use the baked in@Timed
annotation on your controller methods like so: