1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package driver;
import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import shared.Parse;
import shared.PreParser;
import shared.Shared;
import shared.Timer;
/**
* @author Brian Bushnell
* @date Sep 25, 2014
*
*/
public class TestLockSpeed {
public static void main(String[] args){
{//Preparse block for help, config files, and outstream
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
args=pp.args;
//outstream=pp.outstream;
}
int mode=0;
long max=1000000000;
int threads=Shared.threads();
for(int i=0; i<args.length; i++){
String arg=args[i];
String[] split=arg.split("=");
String a=split[0].toLowerCase();
String b=split.length>1 ? split[1] : null;
if(a.equals("mode")){
mode=Integer.parseInt(b);
}else if(a.equals("threads") || a.equals("t")){
threads=Integer.parseInt(b);
}else if(a.equals("max")){
max=Parse.parseKMG(b);
}else{
System.err.println("Unknown parameter "+args[i]);
assert(false) : "Unknown parameter "+args[i];
// throw new RuntimeException("Unknown parameter "+args[i]);
}
}
CountBox box;
if(mode==UNLOCKED || mode==LOCKED){
box=new LockBox();
}else if(mode==ATOMIC){
box=new AtomBox();
}else if(mode==VOLATILE || mode==FIELD || mode==STATICFIELD){
box=new VolatileBox();
}else{
throw new RuntimeException("Unknown mode "+mode);
}
ArrayList<CountThread> list=new ArrayList<CountThread>(threads);
for(int i=0; i<threads; i++){
list.add(new CountThread(box, max, mode));
}
Timer t=new Timer();
for(CountThread ct : list){ct.start();}
for(CountThread ct : list){
while(ct.getState()!=State.TERMINATED){
try {
ct.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
t.stop();
System.out.println("Time: \t"+t);
System.out.println("Count: \t"+box.value());
System.out.println("Speed: \t"+String.format(Locale.ROOT, "%.3f", (threads*max*1.0)/(t.elapsed))+" giga per second");
}
static class CountThread extends Thread{
public CountThread(CountBox box_, long max_, int mode_){
box0=box_;
max=max_;
mode=mode_;
}
@Override
public void run(){
if(mode==UNLOCKED){
final LockBox box=(LockBox)box0;
for(long i=0; i<max; i++){
box.counter++;
}
}else if(mode==LOCKED){
final LockBox box=(LockBox)box0;
for(long i=0; i<max; i++){
box.increment();
}
}else if(mode==ATOMIC){
final AtomBox box=(AtomBox)box0;
for(long i=0; i<max; i++){
box.increment();
}
}else if(mode==VOLATILE){
final VolatileBox box=(VolatileBox)box0;
for(long i=0; i<max; i++){
box.counter++;
}
}else if(mode==FIELD){
final VolatileBox box=(VolatileBox)box0;
final AtomicLongFieldUpdater<VolatileBox> updater=AtomicLongFieldUpdater.newUpdater(VolatileBox.class, "counter");
for(long i=0; i<max; i++){
updater.incrementAndGet(box);
}
}else if(mode==STATICFIELD){
final VolatileBox box=(VolatileBox)box0;
for(long i=0; i<max; i++){
box.increment();
}
}
}
final CountBox box0;
final long max;
final int mode;
}
abstract static class CountBox{
abstract void increment();
abstract long value();
}
static class LockBox extends CountBox{
@Override
synchronized void increment(){counter++;}
@Override
long value(){return counter;}
long counter;
}
static class AtomBox extends CountBox{
@Override
void increment(){counter.incrementAndGet();}
@Override
long value(){return counter.longValue();}
AtomicLong counter=new AtomicLong(0);
}
static class VolatileBox extends CountBox{
@Override
void increment(){updater.incrementAndGet(this);}
@Override
long value(){return counter;}
volatile long counter;
static final AtomicLongFieldUpdater<VolatileBox> updater=AtomicLongFieldUpdater.newUpdater(VolatileBox.class, "counter");
}
static final int UNLOCKED=0, LOCKED=1, ATOMIC=2, VOLATILE=3, FIELD=4, STATICFIELD=5;
}
|