Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / stress / IndexStressTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.stress;
22
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import org.apache.commons.lang3.RandomStringUtils;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.janusgraph.core.JanusGraphTransaction;
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.dbmap.AAIGraph;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.test.annotation.DirtiesContext;
38
39 @Ignore("Run this only to test indexes limit")
40 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
41 public class IndexStressTest extends AAISetup {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(IndexStressTest.class);
44
45     @Before
46     public void setup() {
47         AAIGraph.getInstance().getGraph();
48     }
49
50     @Test
51     public void testIndexStress() {
52         JanusGraphTransaction tx = AAIGraph.getInstance().getGraph().newTransaction();
53
54         GraphTraversalSource g = tx.traversal();
55
56         Set<String> linkNameSet = new HashSet<>();
57         Set<String> aaiUriSet = new HashSet<>();
58
59         int TOTAL_LINKS = 101000;
60
61         for (int i = 0; i < TOTAL_LINKS; i++) {
62
63             String linkName = generateName(linkNameSet);
64             aaiUriSet.add("/network/logical-links/logical-link/" + linkName);
65
66             Vertex v = g.addV().property("aai-node-type", "logical-link").property("link-name", linkName)
67                     .property("aai-uri", "/network/logical-links/logical-link/" + linkName).next();
68
69             if (i % 1000 == 0) {
70                 LOGGER.debug("Committing up to index {}", i);
71                 tx.commit();
72                 tx = AAIGraph.getInstance().getGraph().newTransaction();
73                 g = tx.traversal();
74             }
75         }
76
77         tx.commit();
78
79         tx = AAIGraph.getInstance().getGraph().newTransaction();
80         g = tx.traversal();
81
82         int totalLinks = 0;
83         int totalLinksWithNodeType = 0;
84         int totalLinksUsingUri = 0;
85
86         int index = 0;
87         for (String linkName : linkNameSet) {
88
89             if (g.V().has("aai-node-type", "logical-link").has("link-name", linkName).hasNext()) {
90                 totalLinksWithNodeType++;
91             }
92
93             if (g.V().has("link-name", linkName).hasNext()) {
94                 totalLinks++;
95             }
96
97             if (g.V().has("aai-uri", "/network/logical-links/logical-link/" + linkName).hasNext()) {
98                 totalLinksUsingUri++;
99             }
100
101             index++;
102
103             if (index % 1000 == 0) {
104                 LOGGER.debug("Processed {} many queries and has {} many to go", index, (TOTAL_LINKS - index));
105                 LOGGER.debug("Total links using linkname found: {}", totalLinks);
106                 LOGGER.debug("Total links using nodetype and linkname found: {}", totalLinksWithNodeType);
107                 LOGGER.debug("Total links using uri found: {}", totalLinksUsingUri);
108             }
109         }
110
111         tx.rollback();
112
113         LOGGER.debug("Total links using linkname found: {}", totalLinks);
114         LOGGER.debug("Total links using nodetype and linkname found: {}", totalLinksWithNodeType);
115         LOGGER.debug("Total links using uri found: {}", totalLinksUsingUri);
116     }
117
118     String generateName(Set<String> uniqueKeys) {
119
120         while (true) {
121             String data = RandomStringUtils.randomAlphabetic(20);
122             if (!uniqueKeys.contains(data)) {
123                 uniqueKeys.add(data);
124                 return data;
125             }
126         }
127     }
128 }