Merge branch 'master' of github.com:OpenHFT/Chronicle-Map

Conflicts:
	src/test/java/eg/BGChronicleTest.java
This commit is contained in:
leventov
2014-12-06 00:11:30 +07:00
6 changed files with 526 additions and 360 deletions
@@ -18,15 +18,18 @@
package net.openhft.chronicle.map;
import junit.framework.Assert;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -507,6 +510,21 @@ public class StatelessClientTest {
}
}
@Test(timeout = 10000)
public void testGetAndEntryWeDontHave() throws IOException,
InterruptedException, ExecutionException {
try (ChronicleMap<Integer, CharSequence> serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)).create()) {
try (ChronicleMap<Integer, CharSequence> statelessMap = ChronicleMapBuilder.of(Integer
.class, CharSequence.class)
.statelessClient(new InetSocketAddress("localhost", 8056)).create()) {
assertEquals(null, statelessMap.get(3));
}
}
}
@Test(timeout = 10000)
public void testEquals() throws IOException, InterruptedException {
@@ -547,4 +565,94 @@ public class StatelessClientTest {
serverMap1.close();
serverMap2.close();
}
}
@Ignore
@Test
public void testThreadSafeness() throws IOException, InterruptedException {
Thread.sleep(5000);
final ExecutorService executorService = Executors.newFixedThreadPool(3);
int count = 1000000;
final CountDownLatch latch = new CountDownLatch(count * 2);
// server
try (ChronicleMap<Integer, Integer> server = ChronicleMapBuilder.of(Integer.class, Integer.class)
.putReturnsNull(true)
.replication((byte) 1, TcpTransportAndNetworkConfig.of(8059)).create()) {
// stateless client
try (ChronicleMap<Integer, Integer> client = ChronicleMapBuilder.of(Integer.class,
Integer.class)
.putReturnsNull(true)
.statelessClient(new InetSocketAddress("localhost", 8059)).create()) {
for (int i = 0; i < count; i++) {
final int j = i;
executorService.submit(new Runnable() {
@Override
public void run() {
try {
System.out.println("put " + j + ", Thread" + Thread.currentThread().getName());
client.put(j, j);
latch.countDown();
} catch (Error | Exception e) {
e.printStackTrace();
System.exit(0);
}
}
});
}
for (int i = 0; i < count; i++) {
final int j = i;
final AtomicBoolean issue = new AtomicBoolean();
executorService.submit(new Runnable() {
@Override
public void run() {
try {
System.out.println("get " + j + ", Thread" + Thread.currentThread().getName());
while (client.get(j) == null) {
System.out.println("waiting");
}
if (j != client.get(j))
issue.set(true);
latch.countDown();
} catch (Error | Exception e) {
e.printStackTrace();
System.exit(0);
}
}
});
if (issue.get())
Assert.fail();
}
latch.await();
System.out.println("..............................CLosing");
}
} finally {
executorService.shutdownNow();
executorService.awaitTermination(100, TimeUnit.SECONDS);
}
}
}
@@ -25,6 +25,7 @@ import net.openhft.lang.values.LongValue;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
public class DistributedSequenceMain {
@@ -35,18 +36,47 @@ public class DistributedSequenceMain {
.entries(128)
.actualSegments(1)
.createPersistedTo(file)) {
// throughput test.
for (int t = 0; t < 5; t++) {
LongValue value = DataValueClasses.newDirectReference(LongValue.class);
map.acquireUsing("sequence-" + t, value);
long start = System.nanoTime();
int runs = 10000000;
int runs = 1000000;
for (int i = 0; i < runs; i++) {
long nextId = value.addAtomicValue(1);
}
double rate = (double) (System.nanoTime() - start) / runs;
System.out.printf("Uncontended increment took an average of %.1f ns.%n", rate);
}
// latency test.
LongValue value = DataValueClasses.newDirectReference(LongValue.class);
map.acquireUsing("sequence-X", value);
int runs = 1000000000, count = 0, wcount = 0;
long[] worst = new long[200000];
long start = System.nanoTime();
for (; count < runs; count++) {
long nextId = value.addAtomicValue(1);
long next = System.nanoTime();
long time = next - start;
if (time > 100) {
worst[wcount++] = time;
if (wcount >= worst.length)
break;
next = System.nanoTime();
}
start = next;
}
Arrays.sort(worst);
System.out.printf("Worst 1 in 10K, 1 in 100K, 1 in 1000K, worst is %,d / %,d / %,d / %,d %n",
worst[wcount - count / 10000],
worst[wcount - count / 100000],
worst[wcount - count / 1000000],
worst[wcount - 1]
);
}
}
}