2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter.query;
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;
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;
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;
49 private QueryFactory factory;
52 public static void beforeClass() {
53 final Stopwatch sw = Stopwatch.createStarted();
54 final BindingMap.Builder<SystemKey, System> builder = BindingMap.builder(SYSTEM_COUNT);
56 for (int i = 0; i < SYSTEM_COUNT; ++i) {
57 builder.add(new SystemBuilder().setName("name" + i).setAlias("alias" + i).build());
60 FOO = new FooBuilder().setSystem(builder.build()).build();
61 LOG.info("Test data with {} items built in {}", SYSTEM_COUNT, sw);
65 public static void afterClass() {
70 protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
71 super.setupWithRuntimeContext(runtimeContext);
72 factory = new DefaultQueryFactory(new DefaultBindingCodecTreeFactory().create(runtimeContext));
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);
85 public void queryLessThanAlarm() throws InterruptedException, ExecutionException {
86 final String needle = "alias" + (SYSTEM_COUNT - 1);
88 final Stopwatch sw = Stopwatch.createStarted();
89 final QueryExpression<System> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
90 .extractChild(System.class)
92 .leaf(System::getAlias).valueEquals(needle)
94 LOG.info("Query built in {}", sw);
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);
103 final QueryResult<@NonNull System> result = future.get();
104 LOG.info("Query executed {} in {}", future, sw);
106 assertTrue(result.stream().findAny().isPresent());
107 LOG.info("Query result in {}", sw);
111 public void searchLessThanAlarm() throws InterruptedException, ExecutionException {
112 final String needle = "alias" + (SYSTEM_COUNT - 1);
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));
120 final Foo haystack = future.get().orElseThrow();
121 LOG.info("Read executed in {}", sw);
123 Object result = null;
124 for (System system : haystack.nonnullSystem().values()) {
125 if (needle.equals(system.getAlias())) {
131 LOG.info("Search found {} in {}", result, sw);
132 assertNotNull(result);