13f20a852b6dfa12a1341fefb60b0a019c994a39
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / query / QueryPerformanceTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter.query;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Stopwatch;
16 import com.google.common.util.concurrent.FluentFuture;
17 import java.util.Optional;
18 import java.util.concurrent.ExecutionException;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.junit.AfterClass;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.QueryReadTransaction;
25 import org.opendaylight.mdsal.binding.api.ReadTransaction;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.binding.api.query.QueryExpression;
28 import org.opendaylight.mdsal.binding.api.query.QueryFactory;
29 import org.opendaylight.mdsal.binding.api.query.QueryResult;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
31 import org.opendaylight.mdsal.binding.dom.codec.impl.DefaultBindingCodecTreeFactory;
32 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.yang.gen.v1.mdsal.query.norev.Foo;
35 import org.opendaylight.yang.gen.v1.mdsal.query.norev.FooBuilder;
36 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.System;
37 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.SystemBuilder;
38 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.SystemKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class QueryPerformanceTest extends AbstractDataBrokerTest {
45     private static final Logger LOG = LoggerFactory.getLogger(QueryPerformanceTest.class);
46     private static final int SYSTEM_COUNT = 1_000_000;
47     private static Foo FOO;
48
49     private QueryFactory factory;
50
51     @BeforeClass
52     public static void beforeClass() {
53         final Stopwatch sw = Stopwatch.createStarted();
54         final BindingMap.Builder<SystemKey, System> builder = BindingMap.builder(SYSTEM_COUNT);
55
56         for (int i = 0; i < SYSTEM_COUNT; ++i) {
57             builder.add(new SystemBuilder().setName("name" + i).setAlias("alias" + i).build());
58         }
59
60         FOO = new FooBuilder().setSystem(builder.build()).build();
61         LOG.info("Test data with {} items built in {}", SYSTEM_COUNT, sw);
62     }
63
64     @AfterClass
65     public static void afterClass() {
66         FOO = null;
67     }
68
69     @Override
70     protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
71         super.setupWithRuntimeContext(runtimeContext);
72         factory = new DefaultQueryFactory(new DefaultBindingCodecTreeFactory().create(runtimeContext));
73     }
74
75     @Override
76     protected void setupWithDataBroker(final DataBroker dataBroker) {
77         final Stopwatch sw = Stopwatch.createStarted();
78         WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
79         wtx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Foo.class), FOO);
80         assertCommit(wtx.commit());
81         LOG.info("Test data with {} items populated in {}", SYSTEM_COUNT, sw);
82     }
83
84     @Test
85     public void queryLessThanAlarm() throws InterruptedException, ExecutionException {
86         final String needle = "alias" + (SYSTEM_COUNT - 1);
87
88         final Stopwatch sw = Stopwatch.createStarted();
89         final QueryExpression<System> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
90             .extractChild(System.class)
91                 .matching()
92                     .leaf(System::getAlias).valueEquals(needle)
93                 .build();
94         LOG.info("Query built in {}", sw);
95
96         sw.reset().start();
97         final FluentFuture<QueryResult<@NonNull System>> future;
98         try (ReadTransaction rtx = getDataBroker().newReadOnlyTransaction()) {
99             assertThat(rtx, instanceOf(QueryReadTransaction.class));
100             future = ((QueryReadTransaction) rtx).execute(LogicalDatastoreType.CONFIGURATION, query);
101         }
102
103         final QueryResult<@NonNull System> result = future.get();
104         LOG.info("Query executed {} in {}", future, sw);
105
106         assertTrue(result.stream().findAny().isPresent());
107         LOG.info("Query result in {}", sw);
108     }
109
110     @Test
111     public void searchLessThanAlarm() throws InterruptedException, ExecutionException {
112         final String needle = "alias" + (SYSTEM_COUNT - 1);
113
114         final Stopwatch sw = Stopwatch.createStarted();
115         final FluentFuture<Optional<Foo>> future;
116         try (ReadTransaction rtx = getDataBroker().newReadOnlyTransaction()) {
117             future = rtx.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Foo.class));
118         }
119
120         final Foo haystack = future.get().orElseThrow();
121         LOG.info("Read executed in {}", sw);
122
123         Object result = null;
124         for (System system : haystack.nonnullSystem().values()) {
125             if (needle.equals(system.getAlias())) {
126                 result = system;
127                 break;
128             }
129         }
130
131         LOG.info("Search found {} in {}", result, sw);
132         assertNotNull(result);
133     }
134 }