net.openhft.chronicle.threads.NamedThreadFactory Java Examples

The following examples show how to use net.openhft.chronicle.threads.NamedThreadFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EchoMultiServerMain.java    From Chronicle-Network with Apache License 2.0 6 votes vote down vote up
public static void main(@NotNull String... args) throws IOException {
    int port = args.length < 1 ? EchoClientMain.PORT : Integer.parseInt(args[0]);
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.bind(new InetSocketAddress(port));
    System.out.println("listening on " + ssc);
    ExecutorService service = new ThreadPoolExecutor(0, 1000,
            60L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(),
            new NamedThreadFactory("connections", true));
    ((ThreadPoolExecutor) service).setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

    while (true) {
        final SocketChannel socket = ssc.accept();
        socket.socket().setTcpNoDelay(true);
        socket.configureBlocking(true);
        service.submit(() -> process(socket));
    }
}
 
Example #2
Source File: TrickyContextCasesTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@Test(expected = Exception.class)
public void testPutShouldBeWriteLocked() throws ExecutionException, InterruptedException {
    ChronicleMap<Integer, byte[]> map = ChronicleMapBuilder
            .of(Integer.class, byte[].class)
            .averageValue(new byte[1])
            .entries(100).actualSegments(1).create();
    map.put(1, new byte[]{1});
    map.put(2, new byte[]{2});
    try (ExternalMapQueryContext<Integer, byte[], ?> q = map.queryContext(1)) {
        MapEntry<Integer, byte[]> entry = q.entry(); // acquires read lock implicitly
        assertNotNull(entry);
        Executors.newFixedThreadPool(1,
                new NamedThreadFactory("test"))
                .submit(() -> {
                    // this call should try to acquire write lock, that should lead to dead lock
                    // but if not...
                    // relocates the entry for the key 1 after the entry for 2, under update lock
                    map.put(1, new byte[]{1, 2, 3, 4, 5});
                    // puts the entry for 3 at the place of the entry for the key 1, under update lock
                    map.put(3, new byte[]{3});
                }).get();
        // prints [3]
        System.out.println(Arrays.toString(entry.value().get()));
    }
}
 
Example #3
Source File: EntryCountMapTest.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@Ignore("Long running, large tests test")
@Test
public void testMedium() throws IOException, ExecutionException, InterruptedException {
    System.out.print("testMedium seeds");
    int procs = Runtime.getRuntime().availableProcessors();
    ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
    for (int t = 0; t < ecmTests; t++) {
        // regression test.
        List<Future<?>> futures = new ArrayList<>();
        for (int s : new int[]{512, 256, 128, 64}) {
            int s3 = s * s * 128;
            futures.add(testEntriesMaxSize(es, s, s3 * 2, s3 * 24 / 10, t));
            if (s < 512) {
                futures.add(testEntriesMaxSize(es, s, s3 * 3 / 2, s3 * 9 / 5, t));
                futures.add(testEntriesMaxSize(es, s, s3, s3 * 12 / 10, t));
            }
        }
        for (Future<?> future : futures) {
            System.out.print(".");
            future.get();
        }
    }
    es.shutdown();
    // hyperbolic average gives more weight to small numbers.
    System.out.printf("Score: %.2f%n", scoreCount / score);
}
 
Example #4
Source File: OvertakeTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void threadingTest() throws InterruptedException, ExecutionException, TimeoutException {
    System.out.println("Continue appending");
    ExecutorService execService = Executors.newFixedThreadPool(2,
            new NamedThreadFactory("test"));
    SynchronousQueue<Long> sync = new SynchronousQueue<>();
    long t_index;

    MyAppender myapp = new MyAppender(sync);
    Future<Long> f = execService.submit(myapp);
    try (ChronicleQueue tailer_queue = ChronicleQueue.singleBuilder(path)
            .testBlockSize()
            .writeBufferMode(BufferMode.None)
            .build()) {
        t_index = 0;
        MyTailer mytailer = new MyTailer(tailer_queue, t_index, sync);
        Future<Long> f2 = execService.submit(mytailer);
        t_index = f2.get(10, TimeUnit.SECONDS);
        a_index = f.get(10, TimeUnit.SECONDS);
        assertEquals(a_index, t_index);
    }
    execService.shutdown();
    execService.awaitTermination(1, TimeUnit.SECONDS);
}
 
Example #5
Source File: PortfolioValueTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void createData(final ChronicleMap<LongValue, PortfolioAssetInterface> cache) throws ExecutionException, InterruptedException {
    long startTime = System.currentTimeMillis();

    ExecutorService executor = Executors.newFixedThreadPool(nThreads,
            new NamedThreadFactory("test"));
    Future<?>[] futures = new Future[nThreads];
    long batchSize = nAssets / nThreads;

    for (int t = 0; t < nThreads; t++) {
        final long batch = t;
        futures[t] = executor.submit(() -> {
            final LongValue key = Values.newHeapInstance(LongValue.class);
            final PortfolioAssetInterface value = Values.newHeapInstance(PortfolioAssetInterface.class);

            long start = batch * batchSize;
            long end = Math.min(nAssets, (batch + 1) * batchSize);
            long n = (end - start);

            if (end > start) {
                System.out.println("Inserting batch " + (batch + 1) + "/" + nThreads + " of " + n + " records");

                for (long k = start; k < end; k++) {
                    key.setValue(k);
                    value.setAssetId(k);
                    value.setShares(1);
                    value.setPrice(2.0);
                    cache.put(key, value);
                }
            }
        });
    }

    for (Future<?> future : futures) {
        future.get();
    }

    long elapsedTime = (System.currentTimeMillis() - startTime);
    System.out.println("Data inserted in " + elapsedTime + " ms");
}
 
Example #6
Source File: RollCycleMultiThreadTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test
public void testRead1() throws ExecutionException, InterruptedException {
    File path = DirectoryUtils.tempDir(getClass().getSimpleName());
    TestTimeProvider timeProvider = new TestTimeProvider();

    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder
            .fieldlessBinary(path)
            .testBlockSize()
            .rollCycle(DAILY)
            .timeProvider(timeProvider).build()) {

        ParallelQueueObserver observer = new ParallelQueueObserver(queue0);

        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(
                new NamedThreadFactory("test"));

        try (ChronicleQueue queue = SingleChronicleQueueBuilder
                .fieldlessBinary(path)
                .testBlockSize()
                .rollCycle(DAILY)
                .timeProvider(timeProvider)
                .build()) {
            ExcerptAppender appender = queue.acquireAppender();

            Assert.assertEquals(0, (int) scheduledExecutorService.submit(observer::call).get());
            // two days pass
            timeProvider.add(TimeUnit.DAYS.toMillis(2));

            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 3 data");
            }
            Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer::call).get());
            assertEquals(1, observer.documentsRead);

        }
        scheduledExecutorService.shutdown();
        scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
    }
}
 
Example #7
Source File: UberHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
/**
 * wait 2 seconds before closing the socket connection, this should allow time of the
 * termination event to be sent.
 */
private void closeSoon() {
    if (isClosing.compareAndSet(false, true)) {
        @NotNull final ScheduledExecutorService closer = newSingleThreadScheduledExecutor(new NamedThreadFactory("closer", true));
        closer.schedule(() -> {
            close();
            closer.shutdown();
        }, 2, SECONDS);
    }
}
 
Example #8
Source File: ZippedDocumentAppender.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
/**
 * @param bytesRingBuffer a ring buffer to hold the bytes before they are zipped
 * @param chronicleQueue  the chronicle you wish to append the zipped bytes to
 */
public ZippedDocumentAppender(@NotNull final BytesRingBuffer bytesRingBuffer,
                              @NotNull final DirectChronicleQueue chronicleQueue) {
    this.q = bytesRingBuffer;
    this.chronicleQueue = chronicleQueue;
    qReader = newSingleThreadExecutor(new NamedThreadFactory("qReader"));
    qReader.submit(new Consumer());
}
 
Example #9
Source File: JDBCService.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public JDBCService(@NotNull ChronicleQueue in, ChronicleQueue out, ThrowingSupplier<Connection, SQLException> connectionSupplier) {
    this.in = in;
    this.out = out;
    this.connectionSupplier = connectionSupplier;

    ExecutorService service = Executors.newSingleThreadExecutor(
            new NamedThreadFactory(in.file().getName() + "-JDBCService", true));
    service.execute(this::runLoop);
    service.shutdown(); // stop when the task exits.
}
 
Example #10
Source File: ChronicleReaderTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000)
public void readOnlyQueueTailerShouldObserveChangesAfterInitiallyObservedReadLimit() throws IOException, InterruptedException, TimeoutException, ExecutionException {
    DirectoryUtils.deleteDir(dataDir.toFile());
    dataDir.toFile().mkdirs();
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(dataDir).testBlockSize().build()) {

        final StringEvents events = queue.acquireAppender().methodWriterBuilder(StringEvents.class).build();
        events.say("hello");

        final long readerCapacity = getCurrentQueueFileLength(dataDir);

        final RecordCounter recordCounter = new RecordCounter();
        final ChronicleReader chronicleReader = basicReader().withMessageSink(recordCounter);

        final ExecutorService executorService = Executors.newSingleThreadExecutor(
                new NamedThreadFactory("executor"));
        Future<?> submit = executorService.submit(chronicleReader::execute);

        final long expectedReadingDocumentCount = (readerCapacity / ONE_KILOBYTE.length) + 1;
        int i;
        for (i = 0; i < expectedReadingDocumentCount; i++) {
            events.say(new String(ONE_KILOBYTE));
        }

        recordCounter.latch.countDown();
        executorService.shutdown();
        executorService.awaitTermination(Jvm.isDebug() ? 50 : 5, TimeUnit.SECONDS);
        submit.get(1, TimeUnit.SECONDS);

        // #460 read only not supported on windows.
        if (!OS.isWindows())
            assertEquals(expectedReadingDocumentCount, recordCounter.recordCount.get() - 1);
    }
}
 
Example #11
Source File: RecoverTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Ignore("HCOLL-422")
@Test
public void recoverTest() throws IOException, ExecutionException, InterruptedException {
    File mapFile = File.createTempFile("recoverTestFile", ".map");
    mapFile.deleteOnExit();

    ChronicleMapBuilder<Integer, Integer> builder = ChronicleMap
            .of(Integer.class, Integer.class)
            .entries(2)
            .actualSegments(1)
            .checksumEntries(true);
    ChronicleHashBuilderPrivateAPI<?, ?> privateAPI =
            (ChronicleHashBuilderPrivateAPI<?, ?>) builder.privateAPI();
    privateAPI.replication((byte) 1);
    privateAPI.cleanupRemovedEntries(false);

    map = (ReplicatedChronicleMap<Integer, Integer, ?>) builder.createPersistedTo(mapFile);

    map.acquireModificationIterator((byte) 2);

    // acquires read lock successfully
    assertNull(map.get(0));

    ExecutorService executorService = Executors.newSingleThreadExecutor(
            new NamedThreadFactory("recoverTest"));

    executorService.submit(() -> {
        ExternalMapQueryContext<Integer, Integer, ?> c = map.queryContext(0);
        c.writeLock().lock();
    }).get();

    try {
        map.get(0);
        throw new AssertionError("Expected dead lock exception");
    } catch (Exception expected) {
        // do nothing
    }

    map.close();

    map = (ReplicatedChronicleMap<Integer, Integer, ?>)
            builder.recoverPersistedTo(mapFile, true);

    // acquires read lock successfully
    assertNull(map.get(0));

    map.put(1, 1);
    map.put(2, 2);
    map.remove(1);

    long segmentHeadersOffset = this.map.segmentHeadersOffset;
    map.close();

    try (RandomAccessFile raf = new RandomAccessFile(mapFile, "rw")) {
        FileChannel ch = raf.getChannel();
        MappedByteBuffer mapBB = ch.map(FileChannel.MapMode.READ_WRITE, 0, mapFile.length());
        for (long offset = segmentHeadersOffset; offset < mapFile.length();
             offset += 8) {
            for (int bit = 0; bit < 64; bit++) {
                LOG.error("flip bit {} of word at {}", bit, offset);
                mapBB.putLong((int) offset, mapBB.getLong((int) offset) ^ (1L << bit));
                ChronicleMapBuilder<Integer, Integer> recoverBuilder = ChronicleMap
                        .of(Integer.class, Integer.class);
                ChronicleHashBuilderPrivateAPI<?, ?> recoverPrivateAPI =
                        (ChronicleHashBuilderPrivateAPI<?, ?>) recoverBuilder.privateAPI();
                recoverPrivateAPI.replication((byte) 1);
                recoverPrivateAPI.cleanupRemovedEntries(false);
                try (ChronicleMap<Integer, Integer> recovered =
                             recoverBuilder.recoverPersistedTo(mapFile, false)) {
                    recovered.put(1, 1);
                    recovered.put(2, 2);
                    recovered.remove(1);
                }
            }
        }
    }
}
 
Example #12
Source File: RollCycleMultiThreadTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
    @Test
    public void testRead2() throws ExecutionException, InterruptedException {
        File path = DirectoryUtils.tempDir("testRead2");
        TestTimeProvider timeProvider = new TestTimeProvider();

        try (ChronicleQueue queue0 = SingleChronicleQueueBuilder
                .fieldlessBinary(path)
                .testBlockSize()
                .rollCycle(DAILY)
                .timeProvider(timeProvider)
                .build()) {

            final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);

            final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(
                    new NamedThreadFactory("test"));

            try (ChronicleQueue queue = SingleChronicleQueueBuilder
                    .fieldlessBinary(path)
                    .testBlockSize()
                    .rollCycle(DAILY)
                    .timeProvider(timeProvider)
                    .build()) {

                ExcerptAppender appender = queue.acquireAppender();

                try (final DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write().text("Day 1 data");
                }

                Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer).get());

                // two days pass
                timeProvider.add(TimeUnit.DAYS.toMillis(2));

                try (final DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write().text("Day 3 data");
                }

                Assert.assertEquals(2, (int) scheduledExecutorService.submit(observer).get());

//                System.out.println(queue.dump());
                assertEquals(2, observer.documentsRead);
            }
            scheduledExecutorService.shutdown();
            scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
        }
    }
 
Example #13
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteWithDocumentReadBytesDifferentThreads() throws InterruptedException, TimeoutException, ExecutionException {
    try (final ChronicleQueue queue = builder(getTmpDir(), wireType)
            .build()) {

        final String expected = "some long message";

        ExecutorService service1 = Executors.newSingleThreadExecutor(
                new NamedThreadFactory("service1"));
        ScheduledExecutorService service2 = null;
        try {
            Future f = service1.submit(() -> {
                try (final ExcerptAppender appender = queue.acquireAppender()) {

                    try (final DocumentContext dc = appender.writingDocument()) {
                        dc.wire().writeEventName(() -> "key").text(expected);
                    }
                }
            });

            BlockingQueue<Bytes> result = new ArrayBlockingQueue<>(10);

            service2 = Executors.newSingleThreadScheduledExecutor(
                    new NamedThreadFactory("service2"));
            service2.scheduleAtFixedRate(() -> {
                Bytes b = Bytes.allocateElasticOnHeap(128);
                final ExcerptTailer tailer = queue.createTailer();
                tailer.readBytes(b);
                if (b.readRemaining() == 0)
                    return;
                b.readPosition(0);
                result.add(b);
                throw new RejectedExecutionException();
            }, 1, 1, TimeUnit.MICROSECONDS);

            final Bytes bytes = result.poll(5, TimeUnit.SECONDS);
            if (bytes == null) {
                // troubleshoot failed test http://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshothttp://teamcity.higherfrequencytrading.com:8111/viewLog.html?buildId=264141&tab=buildResultsDiv&buildTypeId=OpenHFT_ChronicleQueue4_Snapshot
                f.get(1, TimeUnit.SECONDS);
                throw new NullPointerException("nothing in result");
            }
            try {
                final String actual = this.wireType.apply(bytes).read(() -> "key").text();
                assertEquals(expected, actual);
                f.get(1, TimeUnit.SECONDS);
            } finally {
                bytes.releaseLast();
            }
        } finally {
            service1.shutdownNow();
            if (service2 != null)
                service2.shutdownNow();
        }
    }
}
 
Example #14
Source File: MoveToWrongIndexThenToEndTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void testBufferUnderflowException() throws InterruptedException {
    append();
    append();

    long lastIndex = getLastIndex(basePath);

    ExecutorService executor = Executors.newSingleThreadExecutor(
            new NamedThreadFactory("executor"));
    try {
        Semaphore l0 = new Semaphore(0);
        Semaphore l1 = new Semaphore(0);
        AtomicReference<Throwable> refThrowable = new AtomicReference<>();

        executor.execute(() -> {

            try (SingleChronicleQueue chronicle = createChronicle(basePath)) {

                ExcerptTailer tailer = chronicle.createTailer();

                tailer.moveToIndex(lastIndex);

                l0.release();

                for (int i = 0; i < numOfToEndCalls; ++i) {
                    tailer.toEnd(); // BufferUnderflowException in readSkip()
                }
            } catch (Throwable e) {
                e.printStackTrace();
                refThrowable.set(e);
            } finally {
                l1.release();
            }
        });

        waitFor(l0, "tailer start");

        append();
        append();

        waitFor(l1, "tailer finish");

        assertNull("refThrowable", refThrowable.get());

    } finally {
        try {
            executor.shutdown();
        } finally {
            if (!executor.isShutdown()) {
                executor.shutdownNow();
            }
        }
    }
}
 
Example #15
Source File: RareAppenderLatencyTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Before
public void before() {
    appenderES = Executors.newSingleThreadExecutor(
            new NamedThreadFactory("Appender", false));
}
 
Example #16
Source File: LargeEntriesTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private void doLargeEntryPerf(int ENTRIES, final int ENTRY_SIZE) throws IOException, InterruptedException, ExecutionException {
        System.out.printf("Testing %,d entries of %,d KB%n", ENTRIES, ENTRY_SIZE / 1024);
        File file = File.createTempFile("largeEntries", ".deleteme");
        file.deleteOnExit();
        final ChronicleMap<String, String> map = ChronicleMapBuilder
                .of(String.class, String.class)
//                .valueReaderAndDataAccess(, SnappyStringMarshaller.INSTANCE, )
                .entries(ENTRIES)
                .averageKeySize(10)
                .averageValueSize(ENTRY_SIZE)
                .putReturnsNull(true)
                .createPersistedTo(file);
        {
//            warmUpCompression(ENTRY_SIZE);
            int threads = Runtime.getRuntime().availableProcessors();
            ExecutorService es = Executors.newFixedThreadPool(threads,
                    new NamedThreadFactory("test"));
            final int block = ENTRIES / threads;
            for (int i = 0; i < 3; i++) {
                long start = System.currentTimeMillis();
                List<Future<?>> futureList = new ArrayList<>();
                for (int t = 0; t < threads; t++) {
                    final int finalT = t;
                    futureList.add(es.submit(new Runnable() {
                        @Override
                        public void run() {
                            exerciseLargeStrings(map, finalT * block, finalT * block + block,
                                    ENTRY_SIZE);
                        }
                    }));
                }
                for (Future<?> future : futureList) {
                    future.get();
                }
                long time = System.currentTimeMillis() - start;
                long operations = 3;
                System.out.printf("Put/Get rate was %.1f MB/s%n", operations * ENTRIES * ENTRY_SIZE / 1e6 / (time / 1e3));
            }
            es.shutdown();
            if (es.isTerminated())
                map.close();
        }
    }
 
Example #17
Source File: LargeEntriesTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test
    public void testLargeStrings() throws ExecutionException, InterruptedException, IOException {
        final int ENTRIES = 250;
        final int ENTRY_SIZE = 100 * 1024;

        File file = File.createTempFile("largeEntries" + System.currentTimeMillis(), ".deleteme");
        file.deleteOnExit();
        try (final ChronicleMap<String, String> map = ChronicleMapBuilder
                .of(String.class, String.class)
//                .valueReaderAndDataAccess(, SnappyStringMarshaller.INSTANCE, )
                .actualSegments(1) // to force an error.
                .entries(ENTRIES)
                .averageKeySize(10)
                .averageValueSize(ENTRY_SIZE)
                .putReturnsNull(true)
                .createPersistedTo(file)) {
            warmUpCompression(ENTRY_SIZE);
            int threads = 4; //Runtime.getRuntime().availableProcessors();
            ExecutorService es = Executors.newFixedThreadPool(threads, new NamedThreadFactory("test"));
            final int block = ENTRIES / threads;
            for (int i = 0; i < 3; i++) {
                long start = System.currentTimeMillis();
                List<Future<?>> futureList = new ArrayList<>();
                for (int t = 0; t < threads; t++) {
                    final int finalT = t;
                    futureList.add(es.submit(new Runnable() {
                        @Override
                        public void run() {
                            exerciseLargeStrings(map, finalT * block, finalT * block + block,
                                    ENTRY_SIZE);
                        }
                    }));
                }
                for (Future<?> future : futureList) {
                    future.get();
                }
                long time = System.currentTimeMillis() - start;
                long operations = 3;
                System.out.printf("Put/Get rate was %.1f MB/s%n",
                        operations * ENTRIES * ENTRY_SIZE / 1e6 / (time / 1e3));
            }
            es.shutdown();
            es.awaitTermination(1, TimeUnit.MINUTES);
            assertTrue(es.isTerminated());
        }
        file.delete();
    }
 
Example #18
Source File: ChronicleMapTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test
    @Ignore("Performance test")
    public void testAcquirePerf()
            throws IOException, ClassNotFoundException, IllegalAccessException,
            InstantiationException, InterruptedException, ExecutionException {
//        int runs = Integer.getInteger("runs", 10);
        int procs = 1; // Runtime.getRuntime().availableProcessors();
        int threads = procs * 3;
        ExecutorService es = Executors.newFixedThreadPool(procs, new NamedThreadFactory("test"));
        for (int runs : new int[]{1, /*10, 250, 500, 1000, 2500*/}) {
            for (int entrySize : new int[]{240, 256}) {
                int valuePadding = entrySize - 16;
                char[] chars = new char[valuePadding];
                Arrays.fill(chars, 'x');
                final StringBuilder value0 = new StringBuilder();
                value0.append(chars);

                for (int segments : new int[]{/*128, 256, */512/*, 1024, 2048*/}) {
                    final long entries = runs * 1000 * 1000L;
                    ChronicleMapBuilder<CharSequence, CharSequence> builder = ChronicleMapBuilder
                            .of(CharSequence.class, CharSequence.class)
                            .entries(entries)
                            .actualSegments(segments)
                            .averageKeySize(14)
                            .averageValueSize(value0.length() + 4);

//                    File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme");
//                    tmpFile.deleteOnExit();
                    try (final ChronicleMap<CharSequence, CharSequence> map =
                                 builder.create()) {

                        int count = runs > 500 ? 2 : 3;
                        final int independence = Math.min(procs, runs > 500 ? 8 : 4);
                        System.out.println("\nKey size: " + runs + " Million entries. " + builder);
                        for (int j = 0; j < count; j++) {
                            long start = System.currentTimeMillis();
                            List<Future> futures = new ArrayList<>();
                            for (int i = 0; i < threads; i++) {
                                final int t = i;
                                futures.add(es.submit(new Runnable() {
                                    @Override
                                    public void run() {
                                        Random rand = new Random(t);
                                        StringBuilder key = new StringBuilder();
                                        StringBuilder value = new StringBuilder();
                                        long next = 50 * 1000 * 1000;
                                        // use a factor to give up to 10 digit numbers.
                                        int factor = Math.max(1,
                                                (int) ((10 * 1000 * 1000 * 1000L - 1) / entries));
                                        for (long j = t % independence;
                                             j < entries + independence - 1;
                                             j += independence) {
                                            key.setLength(0);
                                            key.append("us:");
                                            key.append(j * factor);
                                            // 75% reads, 25% writes.
                                            if (rand.nextInt(4) > 0) {
                                                map.getUsing(key, value);

                                            } else {
                                                try (net.openhft.chronicle.core.io.Closeable c =
                                                             map.acquireContext(key, value)) {
                                                    if (value.length() < value0.length() - 1)
                                                        value.append(value0);
                                                    else if (value.length() > value0.length())
                                                        value.setLength(value0.length() - 1);
                                                    else
                                                        value.append('+');
                                                }
                                            }
                                        }
                                    }
                                }));
                            }
                            for (Future future : futures) {
                                future.get();
                            }
                            long time = System.currentTimeMillis() - start;
                            System.out.printf("EntrySize: %,d Entries: %,d M " +
                                            "Segments: %,d Throughput %.1f M ops/sec%n",
                                    entrySize, runs, segments,
                                    threads * entries / independence / 1000.0 / time);
                        }
                    }
                    printStatus();
                }
            }
        }
        es.shutdown();
        es.awaitTermination(1, TimeUnit.MINUTES);
    }
 
Example #19
Source File: NioSslIntegrationTest.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldEncryptAndDecryptTraffic() throws Exception {
    final ExecutorService threadPool = Executors.newFixedThreadPool(2,
            new NamedThreadFactory("test"));

    final ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.bind(new InetSocketAddress("0.0.0.0", 13337));
    serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    serverChannel.configureBlocking(true);

    final SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);
    channel.connect(new InetSocketAddress("127.0.0.1", serverChannel.socket().getLocalPort()));

    try {

        final Client client = new Client(channel);

        final StateMachineProcessor clientProcessor = new StateMachineProcessor(channel, false,
                SSLContextLoader.getInitialisedContext(), client);

        final SocketChannel serverConnection = serverChannel.accept();
        serverConnection.configureBlocking(false);

        final Server server = new Server(serverConnection);
        final StateMachineProcessor serverProcessor = new StateMachineProcessor(serverConnection, true,
                SSLContextLoader.getInitialisedContext(), server);

        while (!(channel.finishConnect() && serverConnection.finishConnect())) {
            Thread.yield();
        }

        if (SEND_DATA_BEFORE_SSL_HANDSHAKE) {
            testDataConnection(channel, serverConnection);
        }

        threadPool.submit(clientProcessor);
        threadPool.submit(serverProcessor);

        client.waitForResponse(10, TimeUnit.SECONDS);
        serverProcessor.stop();
        clientProcessor.stop();
    } finally {
        Closeable.closeQuietly(channel, serverChannel);
    }

    threadPool.shutdown();
    assertTrue(threadPool.awaitTermination(10, TimeUnit.SECONDS));
}