Created
July 2, 2014 12:15
-
-
Save pingw33n/eca2cf009cdad4c5dbc4 to your computer and use it in GitHub Desktop.
VolatileVsSynchronized Caliper benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package benchmark; | |
import com.google.caliper.runner.CaliperMain; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.atomic.AtomicLong; | |
public class VolatileVsSynchronized { | |
public interface Common { | |
public Integer get(); | |
} | |
public static class VolatileWithSynchronized implements Common { | |
private volatile Integer field; | |
@Override | |
public Integer get() { | |
Integer f = field; | |
if (f == null) { | |
synchronized (this) { | |
field = 1; | |
return field; | |
} | |
} | |
return f; | |
} | |
} | |
public static class Synchronized implements Common { | |
private Integer field; | |
@Override | |
public synchronized Integer get() { | |
if (field == null) { | |
field = 1; | |
} | |
return field; | |
} | |
} | |
public static void main(String[] args) { | |
CaliperMain.main(VolatileVsSynchronized.class, args); | |
} | |
public void timeSynchronized_001(int iterations) { | |
doTime(new Synchronized(), iterations, 1); | |
} | |
public void timeSynchronized_004(int iterations) { | |
doTime(new Synchronized(), iterations, 4); | |
} | |
public void timeSynchronized_016(int iterations) { | |
doTime(new Synchronized(), iterations, 16); | |
} | |
public void timeSynchronized_064(int iterations) { | |
doTime(new Synchronized(), iterations, 64); | |
} | |
public void timeSynchronized_128(int iterations) { | |
doTime(new Synchronized(), iterations, 128); | |
} | |
public void timeVolatileWithSynchronized_001(int iterations) { | |
doTime(new VolatileWithSynchronized(), iterations, 1); | |
} | |
public void timeVolatileWithSynchronized_004(int iterations) { | |
doTime(new VolatileWithSynchronized(), iterations, 4); | |
} | |
public void timeVolatileWithSynchronized_016(int iterations) { | |
doTime(new VolatileWithSynchronized(), iterations, 16); | |
} | |
public void timeVolatileWithSynchronized_064(int iterations) { | |
doTime(new VolatileWithSynchronized(), iterations, 64); | |
} | |
public void timeVolatileWithSynchronized_128(int iterations) { | |
doTime(new VolatileWithSynchronized(), iterations, 128); | |
} | |
private long doTime(final Common instance, final int iterations, int concurrency) { | |
final AtomicLong result = new AtomicLong(); | |
List<Thread> threads = new ArrayList<Thread>(); | |
for (int threadIdx = 0; threadIdx < concurrency; threadIdx++) { | |
Thread t = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
long sum = 0; | |
for (int iter = 0; iter < iterations; iter++) { | |
sum += instance.get(); | |
} | |
result.set(sum); | |
} | |
}); | |
t.start(); | |
threads.add(t); | |
} | |
for (Thread t: threads) { | |
try { | |
t.join(); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
return result.get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment