package manybrain.bench; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicBoolean; /** * Author: Paul Tyma. (paul@manybrain.com) * */ public class VariableStorage { public static final Object someObject = new Object(); public static long staticStore; public static AtomicLong atomicStore = new AtomicLong(); public static volatile long staticVolatileStore; public static void main(String[] args) throws Exception { // this parameter is a general metric on the speed of your machine // if the benchmark is long base = 500000; if (args != null && args.length > 0) { base = Long.parseLong(args[0]); } System.out.println("Local store - (likely optimized away)"); main1(base * 2000L, new Benchie() { public void doThis(long orig) { long z = 10; long a = orig; long now = System.nanoTime(); while (a > 0) { a = a - 1; z = a; } now = (System.nanoTime() - now) / 1000000; totalPerSecond.addAndGet(orig / now); junk1 = a; junk2 = z; } }); System.out.println("Instance store"); main1(base * 2000L, new Benchie() { long instanceStore; public void doThis(long orig) { long a = orig; long now = System.nanoTime(); while (a > 0) { a = a - 1; instanceStore = a; } now = (System.nanoTime() - now) / 1000000; totalPerSecond.addAndGet(orig / now); junk1 = a; junk2 = staticStore; } }); System.out.println("Static store"); main1(base * 2000L, new Benchie() { public void doThis(long orig) { long a = orig; long now = System.nanoTime(); while (a > 0) { a = a - 1; staticStore = a; } now = (System.nanoTime() - now) / 1000000; totalPerSecond.addAndGet(orig / now); junk1 = a; junk2 = staticStore; } }); System.out.println("Static Volatile Store"); main1(base * 500L, new Benchie() { public void doThis(long orig) { long a = orig; long now = System.nanoTime(); while (a > 0) { a = a - 1; staticVolatileStore = a; } now = (System.nanoTime() - now) / 1000000; totalPerSecond.addAndGet(orig / now); junk1 = a; junk2 = staticVolatileStore; } }); System.out.println("AtomicLong store"); main1(base * 500L, new Benchie() { public void doThis(long orig) { long a = orig; long now = System.nanoTime(); while (a > 0) { a = a - 1; atomicStore.set(a); } now = (System.nanoTime() - now) / 1000000; totalPerSecond.addAndGet((long) (orig / now)); junk1 = a; junk2 = staticStore; } }); System.out.println("synchronized Static store"); main1(base * 500L, new Benchie() { public void doThis(long orig) { long a = orig; long now = System.nanoTime(); while (a > 0) { a = a - 1; synchronized (someObject) { staticStore = a; } } now = (System.nanoTime() - now) / 1000000; totalPerSecond.addAndGet((long) (orig / now)); junk1 = a; junk2 = staticStore; } }); } public static void main1(long its, Benchie benchie) { CountDownLatch startRunning; CountDownLatch iAmReady; CountDownLatch allDone; int total = 0; int[] threadnums = new int[]{1, 2, 4, 5}; for (int g = 0; g < threadnums.length; ++g) { startRunning = new CountDownLatch(1); iAmReady = new CountDownLatch(threadnums[g]); allDone = new CountDownLatch(threadnums[g]); for (int xxx = 0; xxx < threadnums[g]; ++xxx) { (new Thread(new Goer(benchie, its, iAmReady, allDone, startRunning))).start(); } System.out.print("threads = " + threadnums[g] + " "); try { iAmReady.await(); } catch (Exception e) { } totalPerSecond.set(0); startRunning.countDown(); try { allDone.await(); } catch (Exception e) { } System.out.println(" per ms avg = " + (totalPerSecond.get())); } } private static AtomicLong totalPerSecond = new AtomicLong(); public static interface Benchie { public void doThis(long its); } public static class Goer implements Runnable { static volatile AtomicBoolean warmer = new AtomicBoolean(false); final long orig; final CountDownLatch ready; final CountDownLatch finished; final CountDownLatch go; Benchie benchie; public Goer(Benchie benchie, long xxx, CountDownLatch ready, CountDownLatch finished, CountDownLatch go) { orig = xxx; this.ready = ready; this.finished = finished; this.go = go; this.benchie = benchie; } public void run() { if (warmer.compareAndSet(false, true)) benchie.doThis(orig); // warm ready.countDown(); try { go.await(); } catch (Exception e) { } benchie.doThis(orig); finished.countDown(); } } public static long junk1; public static long junk2; }