Node API - GET Method performance issue (junit)
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsToDataNodePerfSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ri.performance
22
23 import org.apache.commons.lang3.time.StopWatch
24 import org.onap.cps.spi.CpsDataPersistenceService
25 import org.onap.cps.spi.impl.CpsPersistenceSpecBase
26 import org.onap.cps.spi.model.DataNode
27 import org.onap.cps.spi.model.DataNodeBuilder
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.jdbc.Sql
30 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
31
32 class CpsToDataNodePerfSpec extends CpsPersistenceSpecBase {
33
34     @Autowired
35     CpsDataPersistenceService objectUnderTest
36
37     static final String SET_DATA = '/data/fragment.sql'
38     static final String  XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
39
40     @Sql([CLEAR_DATA, SET_DATA])
41     def 'Get data node by xpath with all descendants with many children'() {
42         given: 'nodes and grandchildren have been persisted'
43             def setupStopWatch = new StopWatch()
44             setupStopWatch.start()
45             createLineage()
46             setupStopWatch.stop()
47             def setupDurationInMillis = setupStopWatch.getTime()
48         when: 'data node is requested with all descendants'
49             def readStopWatch = new StopWatch()
50             readStopWatch.start()
51             def result = objectUnderTest.getDataNode(
52                 DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, INCLUDE_ALL_DESCENDANTS)
53             readStopWatch.stop()
54             def readDurationInMillis = readStopWatch.getTime()
55         then : 'setup duration is under 8 seconds'
56             assert setupDurationInMillis < 8000
57         and: 'read duration is under 3.5 seconds'
58             assert readDurationInMillis < 3500
59         and: 'data node is returned with all the descendants populated'
60             assert countDataNodes(result) == 1533
61     }
62
63     def createLineage() {
64         def numOfChildren = 30
65         def numOfGrandChildren = 50
66         (1..numOfChildren).each {
67             def childName = "perf-test-child-${it}".toString()
68             def newChild = goForthAndMultiply(XPATH_DATA_NODE_WITH_DESCENDANTS, childName, numOfGrandChildren)
69             objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
70         }
71     }
72
73     def goForthAndMultiply(parentXpath, childName, numOfGrandChildren) {
74         def children = []
75         (1..numOfGrandChildren).each {
76             def child = new DataNodeBuilder().withXpath("${parentXpath}/${childName}/${it}-grand-child").build()
77             children.add(child)
78         }
79         return new DataNodeBuilder().withXpath("${parentXpath}/${childName}").withChildDataNodes(children).build()
80     }
81
82     def countDataNodes(DataNode dataNode) {
83         int nodeCount = 1
84         for (DataNode child : dataNode.childDataNodes) {
85             nodeCount = nodeCount + (countDataNodes(child))
86         }
87         return nodeCount
88     }
89 }