BAEL-3855

This commit is contained in:
Unknown 2020-03-15 12:50:43 +01:00
parent 0e3a4221fe
commit 5065a7353d
3 changed files with 12 additions and 27 deletions

View File

@ -18,36 +18,37 @@ import org.openjdk.jmh.annotations.Warmup;
@Fork(value = 2) @Fork(value = 2)
@Warmup(iterations = 0) @Warmup(iterations = 0)
public class ConcurrentAccessBenchmark { public class ConcurrentAccessBenchmark {
ConcurrentAccessExperiment accessMyMap;
static final int SLOTS = 4; static final int SLOTS = 4;
static final int THREADS = 10000; static final int THREADS = 10000;
static final int BUCKETS = Runtime.getRuntime().availableProcessors() * SLOTS; static final int BUCKETS = Runtime.getRuntime().availableProcessors() * SLOTS;
SingleLock singleLock = new SingleLock();
StripedLock stripedLock = new StripedLock(BUCKETS);
@Benchmark @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public Map<String,String> singleLockHashMap() throws InterruptedException { 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 @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public Map<String,String> stripedLockHashMap() throws InterruptedException { 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 @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public Map<String,String> singleLockConcurrentHashMap() throws InterruptedException { 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 @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public Map<String,String> stripedLockConcurrentHashMap() throws InterruptedException { 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);
} }
} }

View File

@ -17,12 +17,8 @@ public class SingleLock extends ConcurrentAccessExperiment {
try { try {
lock.lock(); lock.lock();
map.put("key" + key, "value" + key); map.put("key" + key, "value" + key);
} catch (Exception e) {
this.putSupplier(map, key);
} finally { } finally {
try { lock.unlock();
lock.unlock();
} catch (Exception e) {}
} }
return null; return null;
}); });
@ -33,12 +29,8 @@ public class SingleLock extends ConcurrentAccessExperiment {
try { try {
lock.lock(); lock.lock();
map.get("key" + key); map.get("key" + key);
} catch (Exception e) {
this.getSupplier(map, key);
} finally { } finally {
try { lock.unlock();
lock.unlock();
} catch (Exception e) {}
} }
return null; return null;
}); });

View File

@ -18,14 +18,10 @@ public class StripedLock extends ConcurrentAccessExperiment {
int bucket = key % stripedLock.size(); int bucket = key % stripedLock.size();
Lock lock = stripedLock.get(bucket); Lock lock = stripedLock.get(bucket);
try { try {
lock.tryLock(); lock.lock();
map.put("key" + key, "value" + key); map.put("key" + key, "value" + key);
} catch (Exception e) {
this.putSupplier(map, key);
} finally { } finally {
try { lock.unlock();
lock.unlock();
} catch (Exception e) {}
} }
return null; return null;
}); });
@ -36,14 +32,10 @@ public class StripedLock extends ConcurrentAccessExperiment {
int bucket = key % stripedLock.size(); int bucket = key % stripedLock.size();
Lock lock = stripedLock.get(bucket); Lock lock = stripedLock.get(bucket);
try { try {
lock.tryLock(); lock.lock();
map.get("key" + key); map.get("key" + key);
} catch (Exception e) {
this.getSupplier(map, key);
} finally { } finally {
try { lock.unlock();
lock.unlock();
} catch (Exception e) {}
} }
return null; return null;
}); });