我在我的多线程代码中使用incrementAndGet
方法AtomicLong
来测量我们的一些客户端代码的性能。
@Override
public void run() {
long start = System.nanoTime();
attributes = client.getAttributes(columnsList);
long end = System.nanoTime() - start;
final AtomicLong before = select.putIfAbsent(end / 1000000L, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
}
在上面的代码中,我试图测量多少时间-
client.getAttributes(columnsList);
正在服用。
据我所知,incrementAndGet
方法将以原子方式将当前值增加一。这意味着每个线程可能会等待其他线程增加该值。我对吗?意思是会被屏蔽?
这是否会影响我测量任何方法的性能的方式?这意味着它还会为该测量增加一些额外的时间吗?
为什么我问这个是因为我正在尝试对我们的大多数客户端代码和服务器端代码进行基准测试,如果我需要测量每种方法所花费的时间,那么我就是这样做 -
无论我想测量什么代码,我通常将下面的行放在该方法的正上方
long start = System.nanoTime();
这两行采用相同的方法但不同ConcurrentHashMap
long end = System.nanoTime() - start;
final AtomicLong before = select.putIfAbsent(end / 1000000L, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
因此,如果我正在使用incrementAndGet
方法并且如果它为我的性能测量增加了额外的时间,那么我可能没有得到准确的结果?
更新:-
这是下面的方法,我F3
在incrementAndGet
in上做的时候得到的eclipse
。
所以这里有一个同步块。这意味着每个线程都会在这里等待其他线程。这是一个阻塞呼叫。
/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final synchronized long incrementAndGet() { //IBM-perf_AtomicLong
++value; //IBM-perf_AtomicLong
return value; //IBM-perf_AtomicLong
}
啊。我刚刚检查了我的 JVM,IBM JVM
与SUN JVM
. 因为我在一家公司工作,我无法改变这个特殊的事情。
那么有什么方法可以避免这种基于锁的解决方案来衡量任何方法的性能/基准?请记住,我正在运行 IBM JVM。
谢谢您的帮助。