Search in sources :

Example 11 with LongList

use of com.questdb.std.LongList in project questdb by bluestreak01.

the class PerformanceTest method testIndexAppendAndReadSpeed.

@Test
public void testIndexAppendAndReadSpeed() throws JournalException {
    File indexFile = new File(getFactory().getConfiguration().getJournalBase(), "index-test");
    int totalKeys = 30000;
    int totalValues = 20000000;
    try (KVIndex index = new KVIndex(indexFile, totalKeys, totalValues, 1, JournalMode.APPEND, 0, false)) {
        long valuesPerKey = totalValues / totalKeys;
        long t = System.nanoTime();
        long count = 0;
        for (int k = 0; k < totalKeys; k++) {
            for (int v = 0; v < valuesPerKey; v++) {
                index.add(k, k * valuesPerKey + v);
                count++;
            }
        }
        Assert.assertEquals(count, index.size());
        // make sure that ~20M items appended in under 1s
        t = System.nanoTime() - t;
        LOG.info().$("index append latency: ").$(t / totalValues).$("ns").$();
        if (enabled) {
            Assert.assertTrue("~20M items must be appended under 1s: " + TimeUnit.NANOSECONDS.toMillis(t), TimeUnit.NANOSECONDS.toMillis(t) < 1000);
        }
        for (int i = -10; i < 10; i++) {
            if (i == 0) {
                t = System.nanoTime();
            }
            index.getValueCount(1025);
        }
        t = System.nanoTime() - t;
        LOG.info().$("index value count lookup latency: ").$(+t / 10).$("ns").$();
        if (enabled) {
            Assert.assertTrue("Count lookup must be under 150ns: " + t, t / 10 < 150);
        }
        LongList list = new LongList();
        for (int i = -10; i < 10; i++) {
            if (i == 0) {
                t = System.nanoTime();
            }
            index.getValues(13567 + i, list);
        }
        t = System.nanoTime() - t;
        LOG.info().$("index values lookup latency: ").$(+t / 10).$("ns").$();
        if (enabled) {
            Assert.assertTrue("Values lookup must be under 1.5μs: " + t / 10, t / 10 < 1500);
        }
    }
}
Also used : LongList(com.questdb.std.LongList) File(java.io.File) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 12 with LongList

use of com.questdb.std.LongList in project questdb by bluestreak01.

the class CachingReaderFactoryTest method testLockBusyReader.

@Test
public void testLockBusyReader() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 10000;
    // create journals to read
    final JournalMetadata<?>[] meta = new JournalMetadata[readerCount];
    for (int i = 0; i < readerCount; i++) {
        final JournalMetadata<?> m = new JournalStructure("x" + i).$date("ts").$().build();
        ((WriterFactory) getFactory()).writer(m).close();
        meta[i] = m;
    }
    try {
        try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
            final CyclicBarrier barrier = new CyclicBarrier(threadCount);
            final CountDownLatch halt = new CountDownLatch(threadCount);
            final AtomicInteger errors = new AtomicInteger();
            final LongList lockTimes = new LongList();
            final LongList workerTimes = new LongList();
            new Thread(() -> {
                Rnd rnd = new Rnd();
                try {
                    barrier.await();
                    String name = null;
                    for (int i = 0; i < iterations; i++) {
                        if (name == null) {
                            name = meta[rnd.nextPositiveInt() % readerCount].getName();
                        }
                        while (true) {
                            try {
                                rf.lock(name);
                                lockTimes.add(System.currentTimeMillis());
                                LockSupport.parkNanos(100L);
                                rf.unlock(name);
                                name = null;
                                break;
                            } catch (JournalException e) {
                                if (!(e instanceof RetryLockException)) {
                                    e.printStackTrace();
                                    errors.incrementAndGet();
                                    break;
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
                halt.countDown();
            }).start();
            new Thread(() -> {
                Rnd rnd = new Rnd();
                workerTimes.add(System.currentTimeMillis());
                for (int i = 0; i < iterations; i++) {
                    JournalMetadata<?> metadata = meta[rnd.nextPositiveInt() % readerCount];
                    try (Journal<?> ignored = rf.reader(metadata)) {
                        if (metadata == meta[readerCount - 1] && barrier.getNumberWaiting() > 0) {
                            barrier.await();
                        }
                        LockSupport.parkNanos(10L);
                    } catch (JournalLockedException ignored) {
                    } catch (Exception e) {
                        e.printStackTrace();
                        errors.incrementAndGet();
                    }
                }
                workerTimes.add(System.currentTimeMillis());
                halt.countDown();
            }).start();
            halt.await();
            Assert.assertEquals(0, errors.get());
            // check that there are lock times between worker times
            int count = 0;
            // ensure that we have worker times
            Assert.assertEquals(2, workerTimes.size());
            long lo = workerTimes.get(0);
            long hi = workerTimes.get(1);
            Assert.assertTrue(lockTimes.size() > 0);
            for (int i = 0, n = lockTimes.size(); i < n; i++) {
                long t = lockTimes.getQuick(i);
                if (t > lo && t < hi) {
                    count++;
                }
            }
            Assert.assertTrue(count > 0);
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) Rnd(com.questdb.std.Rnd) Journal(com.questdb.store.Journal) LongList(com.questdb.std.LongList) CountDownLatch(java.util.concurrent.CountDownLatch) JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) FactoryFullException(com.questdb.ex.FactoryFullException) RetryLockException(com.questdb.ex.RetryLockException) CyclicBarrier(java.util.concurrent.CyclicBarrier) RetryLockException(com.questdb.ex.RetryLockException) JournalMetadata(com.questdb.store.factory.configuration.JournalMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

LongList (com.questdb.std.LongList)12 Test (org.junit.Test)5 AbstractTest (com.questdb.test.tools.AbstractTest)3 Record (com.questdb.common.Record)2 JournalException (com.questdb.std.ex.JournalException)2 RecordCursor (com.questdb.common.RecordCursor)1 FactoryFullException (com.questdb.ex.FactoryFullException)1 JournalLockedException (com.questdb.ex.JournalLockedException)1 RetryLockException (com.questdb.ex.RetryLockException)1 GriffinParserTestUtils.intervalToString (com.questdb.griffin.lexer.GriffinParserTestUtils.intervalToString)1 Quote (com.questdb.model.Quote)1 MultiIntervalPartitionSource (com.questdb.ql.interval.MultiIntervalPartitionSource)1 IntList (com.questdb.std.IntList)1 Rnd (com.questdb.std.Rnd)1 StringSink (com.questdb.std.str.StringSink)1 IndexCursor (com.questdb.store.IndexCursor)1 Journal (com.questdb.store.Journal)1 JournalMetadata (com.questdb.store.factory.configuration.JournalMetadata)1 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)1 File (java.io.File)1