Search in sources :

Example 1 with VolatileReference

use of de.invesdwin.util.concurrent.reference.VolatileReference in project invesdwin-context-persistence by subes.

the class InfluxDBPerformanceTest method testInfluxDbPerformanceAsync.

@Test
public void testInfluxDbPerformanceAsync() throws Exception {
    final int freeHttpPort = Network.getFreeServerPort();
    final int freeUdpPort = Network.getFreeServerPort();
    final InfluxServer server = startInfluxDB(freeHttpPort, freeUdpPort);
    server.start();
    try {
        Thread.sleep(10 * 1000);
        final String dbname = "influxDbPerformance";
        final String policyname = "defaultPolicy";
        final String measurementName = "measurementsPerformance";
        final InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:" + freeHttpPort);
        influxDB.createDatabase(dbname);
        influxDB.createRetentionPolicy(policyname, dbname, "9999d", 1, true);
        BatchPoints batch = BatchPoints.database(dbname).retentionPolicy(policyname).build();
        final Instant writesStart = new Instant();
        int i = 0;
        for (final FDate date : newValues()) {
            final Point point = Point.measurement(measurementName).time(date.millisValue(), TimeUnit.MILLISECONDS).tag("hashKey", HASH_KEY).addField("value", date.millisValue()).build();
            batch.point(point);
            i++;
            if (i % FLUSH_INTERVAL == 0) {
                printProgress("Writes", writesStart, i, VALUES);
                influxDB.write(batch);
                batch = BatchPoints.database(dbname).retentionPolicy(policyname).build();
            }
        }
        influxDB.write(batch);
        batch = null;
        printProgress("WritesFinished", writesStart, VALUES, VALUES);
        TimeUnit.SECONDS.sleep(1);
        final Instant readsStart = new Instant();
        for (int reads = 1; reads <= READS; reads++) {
            final IMutableReference<FDate> prevValueRef = new VolatileReference<>();
            final AtomicBoolean finished = new AtomicBoolean();
            final int readsFinal = reads;
            final AtomicInteger count = new AtomicInteger();
            influxDB.query(new Query("Select value from " + measurementName + " where hashKey = '" + HASH_KEY + "'", dbname), 10_000, new Consumer<QueryResult>() {

                @Override
                public void accept(final QueryResult queryResult) {
                    try {
                        final List<Result> range = queryResult.getResults();
                        if (range == null || range.isEmpty()) {
                            return;
                        }
                        final Result firstResult = range.get(0);
                        if (firstResult == null) {
                            return;
                        }
                        final List<Series> series = firstResult.getSeries();
                        if (series == null || series.isEmpty()) {
                            return;
                        }
                        final List<List<Object>> firstSeries = series.get(0).getValues();
                        if (firstSeries == null) {
                            return;
                        }
                        for (int result = 0; result < firstSeries.size(); result++) {
                            final Double valueDouble = (Double) firstSeries.get(result).get(1);
                            final FDate value = new FDate(valueDouble.longValue());
                            final FDate prevValue = prevValueRef.get();
                            if (prevValue != null) {
                                Assertions.checkTrue(prevValue.isBefore(value));
                            }
                            prevValueRef.set(value);
                            count.incrementAndGet();
                        }
                    } catch (final Throwable t) {
                        Err.process(t);
                    }
                }
            }, () -> finished.set(true));
            while (!finished.get() && count.get() != VALUES) {
                TimeUnit.NANOSECONDS.sleep(1);
            }
            Assertions.checkEquals(count.get(), VALUES);
            printProgress("Reads", readsStart, VALUES * readsFinal, VALUES * READS);
        }
        printProgress("ReadsFinished", readsStart, VALUES * READS, VALUES * READS);
    } finally {
        server.stop();
    }
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) Query(org.influxdb.dto.Query) Instant(de.invesdwin.util.time.Instant) VolatileReference(de.invesdwin.util.concurrent.reference.VolatileReference) ProcessedEventsRateString(de.invesdwin.util.lang.ProcessedEventsRateString) Point(org.influxdb.dto.Point) Point(org.influxdb.dto.Point) FDate(de.invesdwin.util.time.date.FDate) QueryResult(org.influxdb.dto.QueryResult) Result(org.influxdb.dto.QueryResult.Result) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryResult(org.influxdb.dto.QueryResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InfluxDB(org.influxdb.InfluxDB) InfluxServer(io.apisense.embed.influx.InfluxServer) List(java.util.List) ATest(de.invesdwin.context.test.ATest) Test(org.junit.jupiter.api.Test)

Aggregations

ATest (de.invesdwin.context.test.ATest)1 VolatileReference (de.invesdwin.util.concurrent.reference.VolatileReference)1 ProcessedEventsRateString (de.invesdwin.util.lang.ProcessedEventsRateString)1 Instant (de.invesdwin.util.time.Instant)1 FDate (de.invesdwin.util.time.date.FDate)1 InfluxServer (io.apisense.embed.influx.InfluxServer)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InfluxDB (org.influxdb.InfluxDB)1 BatchPoints (org.influxdb.dto.BatchPoints)1 Point (org.influxdb.dto.Point)1 Query (org.influxdb.dto.Query)1 QueryResult (org.influxdb.dto.QueryResult)1 Result (org.influxdb.dto.QueryResult.Result)1 Test (org.junit.jupiter.api.Test)1