BAEL-3855
This commit is contained in:
parent
0e3a4221fe
commit
5065a7353d
@ -18,36 +18,37 @@ import org.openjdk.jmh.annotations.Warmup;
|
||||
@Fork(value = 2)
|
||||
@Warmup(iterations = 0)
|
||||
public class ConcurrentAccessBenchmark {
|
||||
ConcurrentAccessExperiment accessMyMap;
|
||||
static final int SLOTS = 4;
|
||||
static final int THREADS = 10000;
|
||||
static final int BUCKETS = Runtime.getRuntime().availableProcessors() * SLOTS;
|
||||
SingleLock singleLock = new SingleLock();
|
||||
StripedLock stripedLock = new StripedLock(BUCKETS);
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
public Map<String,String> singleLockHashMap() throws InterruptedException {
|
||||
return (new SingleLock()).doWork(new HashMap<String,String>(), THREADS, SLOTS);
|
||||
return singleLock.doWork(new HashMap<String,String>(), THREADS, SLOTS);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
public Map<String,String> stripedLockHashMap() throws InterruptedException {
|
||||
return (new StripedLock(BUCKETS)).doWork(new HashMap<String,String>(), THREADS, SLOTS);
|
||||
return stripedLock.doWork(new HashMap<String,String>(), THREADS, SLOTS);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
public Map<String,String> singleLockConcurrentHashMap() throws InterruptedException {
|
||||
return (new SingleLock()).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
|
||||
return singleLock.doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
public Map<String,String> stripedLockConcurrentHashMap() throws InterruptedException {
|
||||
return (new StripedLock(BUCKETS)).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
|
||||
return stripedLock.doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
|
||||
}
|
||||
}
|
||||
|
@ -17,12 +17,8 @@ public class SingleLock extends ConcurrentAccessExperiment {
|
||||
try {
|
||||
lock.lock();
|
||||
map.put("key" + key, "value" + key);
|
||||
} catch (Exception e) {
|
||||
this.putSupplier(map, key);
|
||||
} finally {
|
||||
try {
|
||||
lock.unlock();
|
||||
} catch (Exception e) {}
|
||||
lock.unlock();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
@ -33,12 +29,8 @@ public class SingleLock extends ConcurrentAccessExperiment {
|
||||
try {
|
||||
lock.lock();
|
||||
map.get("key" + key);
|
||||
} catch (Exception e) {
|
||||
this.getSupplier(map, key);
|
||||
} finally {
|
||||
try {
|
||||
lock.unlock();
|
||||
} catch (Exception e) {}
|
||||
lock.unlock();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
@ -18,14 +18,10 @@ public class StripedLock extends ConcurrentAccessExperiment {
|
||||
int bucket = key % stripedLock.size();
|
||||
Lock lock = stripedLock.get(bucket);
|
||||
try {
|
||||
lock.tryLock();
|
||||
lock.lock();
|
||||
map.put("key" + key, "value" + key);
|
||||
} catch (Exception e) {
|
||||
this.putSupplier(map, key);
|
||||
} finally {
|
||||
try {
|
||||
lock.unlock();
|
||||
} catch (Exception e) {}
|
||||
lock.unlock();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
@ -36,14 +32,10 @@ public class StripedLock extends ConcurrentAccessExperiment {
|
||||
int bucket = key % stripedLock.size();
|
||||
Lock lock = stripedLock.get(bucket);
|
||||
try {
|
||||
lock.tryLock();
|
||||
lock.lock();
|
||||
map.get("key" + key);
|
||||
} catch (Exception e) {
|
||||
this.getSupplier(map, key);
|
||||
} finally {
|
||||
try {
|
||||
lock.unlock();
|
||||
} catch (Exception e) {}
|
||||
lock.unlock();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user