Search in sources :

Example 36 with Instant

use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.

the class ATimeSeriesDBTest method testGetPreviousAndNextSkipFile.

@Test
public void testGetPreviousAndNextSkipFile() throws IncompleteUpdateFoundException {
    final String key = "asdf";
    final ATimeSeriesDB<String, FDate> table = new ATimeSeriesDB<String, FDate>("testGetPreviousSkipFile") {

        @Override
        protected ISerde<FDate> newValueSerde() {
            return new TypeDelegateSerde<FDate>(FDate.class);
        }

        @Override
        protected Integer newValueFixedLength() {
            return null;
        }

        @Override
        protected String innerHashKeyToString(final String key) {
            return key;
        }

        @Override
        protected FDate extractEndTime(final FDate value) {
            return value;
        }

        @Override
        protected File getBaseDirectory() {
            return ContextProperties.TEMP_DIRECTORY;
        }
    };
    final List<FDate> dates = new ArrayList<>();
    for (int i = 0; i < 100_000; i++) {
        dates.add(new FDate(i));
    }
    final MutableInt segments = new MutableInt();
    new ATimeSeriesUpdater<String, FDate>(key, table) {

        @Override
        protected ICloseableIterable<? extends FDate> getSource(final FDate updateFrom) {
            return WrapperCloseableIterable.maybeWrap(dates);
        }

        @Override
        protected void onUpdateFinished(final Instant updateStart) {
        }

        @Override
        protected void onUpdateStart() {
        }

        @Override
        protected FDate extractEndTime(final FDate element) {
            return element;
        }

        @Override
        protected void onFlush(final int flushIndex, final ATimeSeriesUpdater<String, FDate>.UpdateProgress updateProgress) {
            segments.increment();
        }

        @Override
        public Percent getProgress() {
            return null;
        }
    }.update();
    Assertions.assertThat(segments.intValue()).isEqualByComparingTo(10);
    for (int i = 0; i < dates.size(); i += ATimeSeriesUpdater.BATCH_FLUSH_INTERVAL) {
        final FDate expectedValue = dates.get(dates.size() - i - 1);
        final long expectedIndex = expectedValue.millisValue();
        final FDate value = table.getPreviousValue(key, dates.get(dates.size() - 1), i);
        final long valueIndex = value.millisValue();
        Assertions.checkEquals(valueIndex, expectedIndex, i + ": expected [" + expectedIndex + "] got [" + valueIndex + "]");
    }
    for (int i = 0; i < dates.size(); i += ATimeSeriesUpdater.BATCH_FLUSH_INTERVAL) {
        final FDate expectedValue = dates.get(dates.size() - i - 1);
        final long expectedIndex = expectedValue.millisValue();
        final FDate value = table.getPreviousValue(key, FDate.MAX_DATE, i);
        final long valueIndex = value.millisValue();
        Assertions.checkEquals(valueIndex, expectedIndex, i + ": expected [" + expectedIndex + "] got [" + valueIndex + "]");
    }
    for (int i = 0; i < dates.size(); i += ATimeSeriesUpdater.BATCH_FLUSH_INTERVAL) {
        final FDate expectedValue = dates.get(0);
        final long expectedIndex = expectedValue.millisValue();
        final FDate value = table.getPreviousValue(key, FDate.MIN_DATE, i);
        final long valueIndex = value.millisValue();
        Assertions.checkEquals(valueIndex, expectedIndex, i + ": expected [" + expectedIndex + "] got [" + valueIndex + "]");
    }
    for (int i = 0; i < dates.size(); i += ATimeSeriesUpdater.BATCH_FLUSH_INTERVAL) {
        final FDate expectedValue = dates.get(i);
        final long expectedIndex = expectedValue.millisValue();
        final FDate value = table.getNextValue(key, dates.get(0), i);
        final long valueIndex = value.millisValue();
        Assertions.checkEquals(valueIndex, expectedIndex, i + ": expected [" + expectedIndex + "] got [" + valueIndex + "]");
    }
    for (int i = 0; i < dates.size(); i += ATimeSeriesUpdater.BATCH_FLUSH_INTERVAL) {
        final FDate expectedValue = dates.get(i);
        final long expectedIndex = expectedValue.millisValue();
        final FDate value = table.getNextValue(key, FDate.MIN_DATE, i);
        final long valueIndex = value.millisValue();
        Assertions.checkEquals(valueIndex, expectedIndex, i + ": expected [" + expectedIndex + "] got [" + valueIndex + "]");
    }
    for (int i = 0; i < dates.size(); i += ATimeSeriesUpdater.BATCH_FLUSH_INTERVAL) {
        final FDate expectedValue = dates.get(dates.size() - 1);
        final long expectedIndex = expectedValue.millisValue();
        final FDate value = table.getNextValue(key, FDate.MAX_DATE, i);
        final long valueIndex = value.millisValue();
        Assertions.checkEquals(valueIndex, expectedIndex, i + ": expected [" + expectedIndex + "] got [" + valueIndex + "]");
    }
}
Also used : Percent(de.invesdwin.util.math.decimal.scaled.Percent) ICloseableIterable(de.invesdwin.util.collections.iterable.ICloseableIterable) Instant(de.invesdwin.util.time.Instant) ArrayList(java.util.ArrayList) TypeDelegateSerde(de.invesdwin.util.marshallers.serde.TypeDelegateSerde) FDate(de.invesdwin.util.time.date.FDate) ATimeSeriesUpdater(de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ATest(de.invesdwin.context.test.ATest) Test(org.junit.jupiter.api.Test)

Example 37 with Instant

use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.

the class TimeseriesDBPerformanceTest method readGetLatest.

private void readGetLatest(final ATimeSeriesDB<String, FDate> table, final String suffix, final long countReads) throws InterruptedException {
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    final List<FDate> values = Lists.toList(newValues());
    final Instant readsStart = new Instant();
    for (long reads = 1; reads <= countReads; reads++) {
        FDate prevValue = null;
        for (int i = 0; i < values.size(); i++) {
            try {
                final FDate value = table.getLatestValue(HASH_KEY, values.get(i));
                if (prevValue != null) {
                    Assertions.checkTrue(prevValue.isBefore(value));
                }
                prevValue = value;
            } catch (final NoSuchElementException e) {
                break;
            }
        }
        if (loopCheck.check()) {
            printProgress("GetLatests" + suffix, readsStart, VALUES * reads, VALUES * countReads);
        }
    }
    printProgress("GetLatests" + suffix + "Finished", readsStart, VALUES * countReads, VALUES * countReads);
}
Also used : Instant(de.invesdwin.util.time.Instant) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate) NoSuchElementException(java.util.NoSuchElementException)

Example 38 with Instant

use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.

the class HsqldbPerformanceTest method readIterator.

private void readIterator(final Connection conn) throws Exception {
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    final Instant readsStart = new Instant();
    final Statement select = conn.createStatement();
    for (int reads = 1; reads <= READS; reads++) {
        FDate prevValue = null;
        int count = 0;
        try (ResultSet results = select.executeQuery("SELECT value FROM abc ORDER BY KEY ASC")) {
            while (results.next()) {
                final FDate value = new FDate(results.getLong(1));
                if (prevValue != null) {
                    Assertions.checkTrue(prevValue.isBefore(value));
                }
                prevValue = value;
                count++;
            }
        }
        Assertions.checkEquals(count, VALUES);
        if (loopCheck.check()) {
            printProgress("Reads", readsStart, VALUES * reads, VALUES * READS);
        }
    }
    printProgress("ReadsFinished", readsStart, VALUES * READS, VALUES * READS);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Instant(de.invesdwin.util.time.Instant) ResultSet(java.sql.ResultSet) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate)

Example 39 with Instant

use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.

the class HsqldbPerformanceTest method testHsqldbPerformance.

@Test
public void testHsqldbPerformance() throws Exception {
    final File directory = new File(ContextProperties.getCacheDirectory(), HsqldbPerformanceTest.class.getSimpleName());
    Files.deleteNative(directory);
    Files.forceMkdirParent(directory);
    final Instant writesStart = new Instant();
    int i = 0;
    Class.forName("org.hsqldb.jdbc.JDBCDriver");
    final Connection conn = DriverManager.getConnection("jdbc:hsqldb:file:" + directory.getAbsolutePath(), "SA", "");
    final Statement create = conn.createStatement();
    create.execute("DROP TABLE abc IF EXISTS");
    create.execute("CREATE TABLE abc (key BIGINT, value BIGINT, PRIMARY KEY(key))");
    create.execute("CREATE UNIQUE INDEX idx_abc on abc (key)");
    create.close();
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    final Statement tx = conn.createStatement();
    tx.execute("START TRANSACTION");
    final PreparedStatement insert = conn.prepareStatement("INSERT INTO abc VALUES (?,?)");
    boolean batch = false;
    for (final FDate date : newValues()) {
        final long value = date.longValue(FTimeUnit.MILLISECONDS);
        insert.setLong(1, value);
        insert.setLong(2, value);
        insert.addBatch();
        i++;
        batch = true;
        if (i % FLUSH_INTERVAL == 0) {
            insert.executeBatch();
            batch = false;
            if (loopCheck.check()) {
                printProgress("Writes", writesStart, i, VALUES);
            }
        }
    }
    if (batch) {
        insert.executeBatch();
    }
    insert.close();
    tx.execute("COMMIT");
    printProgress("WritesFinished", writesStart, VALUES, VALUES);
    tx.close();
    readIterator(conn);
    readGet(conn);
    readGetLatest(conn);
    final Statement drop = conn.createStatement();
    drop.execute("drop table abc");
    drop.close();
    Files.deleteNative(directory);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Instant(de.invesdwin.util.time.Instant) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) File(java.io.File) FDate(de.invesdwin.util.time.date.FDate) Test(org.junit.jupiter.api.Test)

Example 40 with Instant

use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.

the class SqlitePerformanceTest method testSQLitePerformance.

@Test
public void testSQLitePerformance() throws Exception {
    final File directory = new File(ContextProperties.getCacheDirectory(), SqlitePerformanceTest.class.getSimpleName());
    Files.deleteNative(directory);
    Files.forceMkdirParent(directory);
    final Instant writesStart = new Instant();
    int i = 0;
    Class.forName("org.sqlite.JDBC");
    final Connection conn = DriverManager.getConnection("jdbc:sqlite:" + directory.getAbsolutePath());
    final Statement create = conn.createStatement();
    create.execute("DROP TABLE IF EXISTS abc");
    create.execute("CREATE TABLE abc (key LONG, value LONG, PRIMARY KEY(key))");
    create.execute("CREATE UNIQUE INDEX idx_abc on abc (key)");
    create.close();
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    final Statement tx = conn.createStatement();
    tx.execute("BEGIN TRANSACTION");
    final PreparedStatement insert = conn.prepareStatement("INSERT INTO abc VALUES (?,?)");
    for (final FDate date : newValues()) {
        final long value = date.longValue(FTimeUnit.MILLISECONDS);
        insert.setLong(1, value);
        insert.setLong(2, value);
        insert.addBatch();
        i++;
        if (i % FLUSH_INTERVAL == 0) {
            insert.executeBatch();
            if (loopCheck.check()) {
                printProgress("Writes", writesStart, i, VALUES);
            }
        }
    }
    insert.executeBatch();
    insert.close();
    tx.execute("COMMIT");
    printProgress("WritesFinished", writesStart, VALUES, VALUES);
    tx.close();
    readIterator(conn);
    readGet(conn);
    readGetLatest(conn);
    final Statement drop = conn.createStatement();
    drop.execute("drop table abc");
    drop.close();
    Files.deleteNative(directory);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Instant(de.invesdwin.util.time.Instant) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) File(java.io.File) FDate(de.invesdwin.util.time.date.FDate) Test(org.junit.jupiter.api.Test)

Aggregations

Instant (de.invesdwin.util.time.Instant)93 FDate (de.invesdwin.util.time.date.FDate)78 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)65 NoSuchElementException (java.util.NoSuchElementException)30 Test (org.junit.jupiter.api.Test)25 File (java.io.File)16 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)15 Statement (java.sql.Statement)12 ATest (de.invesdwin.context.test.ATest)8 RecordFile (com.indeed.lsmtree.recordlog.RecordFile)6 ProcessedEventsRateString (de.invesdwin.util.lang.ProcessedEventsRateString)6 TimedDecimal (de.invesdwin.util.math.decimal.TimedDecimal)5 FDate (de.invesdwin.util.time.fdate.FDate)5 IOException (java.io.IOException)5 Connection (java.sql.Connection)5 APersistentMap (de.invesdwin.context.integration.persistentmap.APersistentMap)4 ATimeSeriesUpdater (de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater)4 Test (org.junit.Test)4 SnappyCodec (com.indeed.util.compress.SnappyCodec)3