Enhancements for the aai-common library
[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 org.apache.commons.lang.RandomStringUtils;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Vertex;
26 import org.janusgraph.core.JanusGraphTransaction;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.Ignore;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.dbmap.AAIGraph;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.test.annotation.DirtiesContext;
35
36 import java.util.HashSet;
37 import java.util.LinkedHashSet;
38 import java.util.Set;
39
40 @Ignore("Run this only to test indexes limit")
41 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
42 public class IndexStressTest extends AAISetup {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(IndexStressTest.class);
45
46     @Before
47     public void setup(){
48         AAIGraph.getInstance().getGraph();
49     }
50
51     @Test
52     public void testIndexStress(){
53         JanusGraphTransaction tx = AAIGraph.getInstance().getGraph().newTransaction();
54
55         GraphTraversalSource g = tx.traversal();
56
57         Set<String> linkNameSet = new HashSet<>();
58         Set<String> aaiUriSet = new HashSet<>();
59
60         int TOTAL_LINKS = 101000;
61
62         for(int i = 0; i <TOTAL_LINKS; i++){
63
64             String linkName = generateName(linkNameSet);
65             aaiUriSet.add("/network/logical-links/logical-link/" + linkName);
66
67             Vertex v = g.addV()
68                 .property("aai-node-type", "logical-link")
69                 .property("link-name", linkName)
70                 .property("aai-uri", "/network/logical-links/logical-link/" + linkName)
71                 .next();
72
73             if(i % 1000 == 0){
74                 LOGGER.debug("Committing up to index {}", i);
75                 tx.commit();
76                 tx = AAIGraph.getInstance().getGraph().newTransaction();
77                 g = tx.traversal();
78             }
79         }
80
81         tx.commit();
82
83         tx = AAIGraph.getInstance().getGraph().newTransaction();
84         g = tx.traversal();
85
86         int totalLinks= 0;
87         int totalLinksWithNodeType = 0;
88         int totalLinksUsingUri = 0;
89
90         int index = 0;
91         for (String linkName : linkNameSet) {
92
93             if(g.V().has("aai-node-type", "logical-link").has("link-name", linkName).hasNext()){
94                totalLinksWithNodeType++;
95             }
96
97             if(g.V().has("link-name", linkName).hasNext()){
98                 totalLinks++;
99             }
100
101             if(g.V().has("aai-uri", "/network/logical-links/logical-link/" + linkName).hasNext()){
102                 totalLinksUsingUri++;
103             }
104
105             index++;
106
107             if(index%1000 == 0){
108                 LOGGER.debug("Processed {} many queries and has {} many to go", index, (TOTAL_LINKS-index));
109                 LOGGER.debug("Total links using linkname found: {}", totalLinks);
110                 LOGGER.debug("Total links using nodetype and linkname found: {}", totalLinksWithNodeType);
111                 LOGGER.debug("Total links using uri found: {}", totalLinksUsingUri);
112             }
113         }
114
115         tx.rollback();
116
117         LOGGER.debug("Total links using linkname found: {}", totalLinks);
118         LOGGER.debug("Total links using nodetype and linkname found: {}", totalLinksWithNodeType);
119         LOGGER.debug("Total links using uri found: {}", totalLinksUsingUri);
120     }
121
122     String generateName(Set<String> uniqueKeys){
123
124         while(true) {
125             String data = RandomStringUtils.randomAlphabetic(20);
126             if (!uniqueKeys.contains(data)){
127                 uniqueKeys.add(data);
128                 return data;
129             }
130         }
131     }
132 }