I am using incrementAndGet
method of AtomicLong
in my multithreaded code to measure the performance of some of our client side code.
@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();
}
}
In the above code, I am trying to measure how much time-
client.getAttributes(columnsList);
is taking.
As far as I know incrementAndGet
method will increment the current value by one in an atomic way. Which means it might be possible each thread will wait for other threads to increment the value. Am I right? Meaning it will get blocked?
And also does this affect the way I am measuring performance of any method? Meaning it will add some extra time to that measurement as well?
Why I am asking this is because I am trying to benchmark most of our client side code and the server side code and if I need to measure how much time each method is taking, then I am doing it simply like this-
Whatever code I want to measure, I usually put the below line just above that method
long start = System.nanoTime();
And these two lines after the same method but with different ConcurrentHashMap
long end = System.nanoTime() - start;
final AtomicLong before = select.putIfAbsent(end / 1000000L, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
So if I am using incrementAndGet
method and if it is adding extra time to my performance measurement then it might be possible that I am not getting accurate result?
Update:-
This is the below method, I got when I did F3
on the incrementAndGet
in eclipse
.
So there is a synchronized block here. That means each thread will wait here for other threads. And it's a blocking call.
/**
* 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
}
Aaah. I just checked my JVM and I am running IBM JVM
as compared to SUN JVM
. As I am working in a company where I can't changed this particular thing.
So is there any way to avoid this lock based solution to measure the performance/benchmark of any method? Keeping in mind I am running IBM JVM.
Thanks for the help.