@Before public void setup() throws IOException { final TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig.of(8076, new InetSocketAddress("localhost", 8077)) .heartBeatInterval(1, SECONDS) .autoReconnectedUponDroppedConnection(true); map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000) .replication((byte) 1, tcpConfig) .create(); TcpTransportAndNetworkConfig config2 = TcpTransportAndNetworkConfig.of(8077) .heartBeatInterval(1, SECONDS) .autoReconnectedUponDroppedConnection(true); map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000) .replication((byte) 2, config2) .create(); }
@Before public void setup() throws IOException { value = DataValueClasses.newDirectReference(IntValue.class); ((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0); final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1); timeProvider = new TimeProvider() { Random rnd = new Random(4); @Override public long currentTime() { if (rnd.nextBoolean()) return t++; else return t; } }; { final TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.of(s_port, endpoint); map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(Builder.SIZE) .timeProvider(timeProvider) .replication((byte) 1, tcpConfig1) .create(); } { final TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.of(s_port + 1); map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(Builder.SIZE) .timeProvider(timeProvider) .replication((byte) 2, tcpConfig2) .create(); } s_port += 2; }
@Test public void dataKeyValueTest() { ChronicleMap<IntValue, LongValue> map = ChronicleMapBuilder.of(IntValue.class, LongValue.class).entries(1000).create(); IntValue heapKey = Values.newHeapInstance(IntValue.class); LongValue heapValue = Values.newHeapInstance(LongValue.class); LongValue directValue = Values.newNativeReference(LongValue.class); heapKey.setValue(1); heapValue.setValue(1); map.put(heapKey, heapValue); assertEquals(1, map.get(heapKey).getValue()); assertEquals(1, map.getUsing(heapKey, heapValue).getValue()); heapKey.setValue(1); map.getUsing(heapKey, directValue).addValue(1); assertEquals(2, map.getUsing(heapKey, heapValue).getValue()); heapKey.setValue(2); heapValue.setValue(3); map.put(heapKey, heapValue); assertEquals(3, map.get(heapKey).getValue()); }
// TODO test passes but is under development. public static void main(String... ignored) throws IOException { AffinityLock lock = AffinityLock.acquireCore(); File file = File.createTempFile("testCHMLatency", "deleteme"); // File file = new File("testCHMLatency.deleteme"); file.delete(); ChronicleMap<LongValue, LongValue> countersMap = ChronicleMapBuilder.of(LongValue.class, LongValue.class) .entries(KEYS) .createPersistedTo(file); // add keys LongValue key = Values.newHeapInstance(LongValue.class); LongValue value = Values.newNativeReference(LongValue.class); for (long i = 0; i < KEYS; i++) { key.setValue(i); countersMap.acquireUsing(key, value); value.setValue(0); } System.out.println("Keys created"); // Monitor monitor = new Monitor(); LongValue value2 = Values.newNativeReference(LongValue.class); for (int t = 0; t < 5; t++) { for (int rate : new int[] { 2 * 1000 * 1000, 1000 * 1000, 500 * 1000 /*, 250 * 1000, 100 * 1000, 50 * 1000*/ }) { Histogram times = new Histogram(); int u = 0; long start = System.nanoTime(); long delay = 1000 * 1000 * 1000L / rate; long next = start + delay; for (long j = 0; j < RUN_TIME * rate; j += KEYS) { int stride = Math.max(1, KEYS / (RUN_TIME * rate)); // the timed part for (int i = 0; i < KEYS && u < RUN_TIME * rate; i += stride) { // busy wait for next time. while (System.nanoTime() < next - 12) ; // monitor.sample = System.nanoTime(); long start0 = next; // start the update. key.setValue(i); LongValue using = countersMap.getUsing(key, value2); if (using == null) assertNotNull(using); value2.addAtomicValue(1); // calculate the time using the time it should have started, not when it was able. long elapse = System.nanoTime() - start0; times.sample(elapse); next += delay; } // monitor.sample = Long.MAX_VALUE; } System.out.printf("run %d %,9d : ", t, rate); times.printPercentiles(" micro-seconds."); } System.out.println(); } // monitor.running = false; countersMap.close(); file.delete(); }
@Test public void testSanity1() throws IOException, InterruptedException { String tmp = System.getProperty("java.io.tmpdir"); String pathname = tmp + "/testSanity1-" + UUID.randomUUID().toString() + ".dat"; File file = new File(pathname); System.out.println( "Starting sanity test 1. Chronicle file :" + file.getAbsolutePath().toString()); ScheduledExecutorService producerExecutor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() - 1); ScheduledExecutorService consumerExecutor = Executors.newSingleThreadScheduledExecutor(); int N = 1000; int producerPeriod = 100; TimeUnit producerTimeUnit = TimeUnit.MILLISECONDS; int consumerPeriod = 100; TimeUnit consumerTimeUnit = TimeUnit.MILLISECONDS; int totalTestTimeMS = (consumerPeriod + producerPeriod) * 20; try (ChronicleMap<String, DummyValue> map = ChronicleMapBuilder.of(String.class, DummyValue.class).entries(N).createPersistedTo(file)) { map.clear(); producerExecutor.scheduleAtFixedRate( () -> { Thread.currentThread().setName("Producer " + Thread.currentThread().getId()); Random r = new Random(); System.out.println("Before PRODUCING size is " + map.size()); for (int i = 0; i < N; i++) { LockSupport.parkNanos(r.nextInt(5)); map.put(String.valueOf(i), DummyValue.DUMMY_VALUE); } System.out.println("After PRODUCING size is " + map.size()); }, 0, producerPeriod, producerTimeUnit); consumerExecutor.scheduleAtFixedRate( () -> { Thread.currentThread().setName("Consumer"); Set<String> keys = map.keySet(); Random r = new Random(); System.out.println("Before CONSUMING size is " + map.size()); System.out.println(); for (String key : keys) { if (r.nextBoolean()) { map.remove(key); } } System.out.println("After CONSUMING size is " + map.size()); }, 0, consumerPeriod, consumerTimeUnit); Thread.sleep(totalTestTimeMS); consumerExecutor.shutdown(); try { consumerExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } producerExecutor.shutdown(); try { producerExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } } }