
com.hazelcast.stabilizer.tests.map.MapRaceTest Maven / Gradle / Ivy
The newest version!
package com.hazelcast.stabilizer.tests.map;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.stabilizer.tests.TestContext;
import com.hazelcast.stabilizer.tests.TestRunner;
import com.hazelcast.stabilizer.tests.annotations.Performance;
import com.hazelcast.stabilizer.tests.annotations.Run;
import com.hazelcast.stabilizer.tests.annotations.Setup;
import com.hazelcast.stabilizer.tests.annotations.Teardown;
import com.hazelcast.stabilizer.tests.annotations.Verify;
import com.hazelcast.stabilizer.tests.annotations.Warmup;
import com.hazelcast.stabilizer.tests.utils.ThreadSpawner;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.assertEquals;
/**
* This test verifies that there are race problems if the IMap is not used correctly.
*
*/
public class MapRaceTest {
private final static ILogger log = Logger.getLogger(MapRaceTest.class);
//props
public int threadCount = 10;
public int keyCount = 1000;
public int logFrequency = 10000;
public int performanceUpdateFrequency = 10000;
public String basename = "maprace";
private IMap map;
private final AtomicLong operations = new AtomicLong();
private IMap> resultsPerWorker;
private TestContext testContext;
@Setup
public void setup(TestContext testContext) throws Exception {
this.testContext = testContext;
HazelcastInstance targetInstance = testContext.getTargetInstance();
map = targetInstance.getMap(basename + "-" + testContext.getTestId());
resultsPerWorker = targetInstance.getMap(basename+"ResultMap" + testContext.getTestId());
}
@Teardown
public void teardown() throws Exception {
map.destroy();
resultsPerWorker.destroy();
}
@Warmup(global = true)
public void warmup() throws Exception {
for (int k = 0; k < keyCount; k++) {
map.put(k, 0l);
}
}
@Run
public void run() {
ThreadSpawner spawner = new ThreadSpawner(testContext.getTestId());
for (int k = 0; k < threadCount; k++) {
spawner.spawn(new Worker());
}
spawner.awaitCompletion();
}
@Verify
public void verify() throws Exception {
long[] amount = new long[keyCount];
for (Map map : resultsPerWorker.values()) {
for (Map.Entry entry : map.entrySet()) {
amount[entry.getKey()] += entry.getValue();
}
}
int failures = 0;
for (int k = 0; k < keyCount; k++) {
long expected = amount[k];
long found = map.get(k);
if (expected != found) {
failures++;
}
}
assertEquals("There should not be any data races", 0, failures);
}
@Performance
public long getOperationCount() {
return operations.get();
}
private class Worker implements Runnable {
private final Random random = new Random();
private final Map result = new HashMap();
@Override
public void run() {
for (int k = 0; k < keyCount; k++) {
result.put(k, 0L);
}
long iteration = 0;
while (!testContext.isStopped()) {
Integer key = random.nextInt(keyCount);
long increment = random.nextInt(100);
Long current = map.get(key);
Long update = current + increment;
map.put(key, update);
increment(key, increment);
if (iteration % logFrequency == 0) {
log.info(Thread.currentThread().getName() + " At iteration: " + iteration);
}
if (iteration % performanceUpdateFrequency == 0) {
operations.addAndGet(performanceUpdateFrequency);
}
iteration++;
}
resultsPerWorker.put(UUID.randomUUID().toString(), result);
}
private void increment(int key, long increment) {
result.put(key, result.get(key) + increment);
}
}
public static void main(String[] args) throws Throwable {
MapRaceTest test = new MapRaceTest();
new TestRunner(test).run();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy