781d4cb4381b3afaac961987790460eb5af65a83
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / performance / CpsToDataNodePerfTest.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.spi.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 import static org.onap.cps.spi.impl.CpsPersistenceSpecBase.CLEAR_DATA
32
33 class CpsToDataNodePerfTest extends CpsPersistenceSpecBase {
34
35     static final String SET_DATA = '/data/fragment.sql'
36
37     @Autowired
38     CpsDataPersistenceService objectUnderTest
39
40     def XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
41
42     @Sql([CLEAR_DATA, SET_DATA])
43     def 'Get data node by xpath with all descendants with many children'() {
44         given: 'nodes and grandchildren have been persisted'
45             def setupStopWatch = new StopWatch()
46             setupStopWatch.start()
47             createLineage()
48             setupStopWatch.stop()
49             def setupDurationInMillis = setupStopWatch.getTime()
50         when: 'data node is requested with all descendants'
51             def readStopWatch = new StopWatch()
52             readStopWatch.start()
53             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, INCLUDE_ALL_DESCENDANTS)
54             readStopWatch.stop()
55             def readDurationInMillis = readStopWatch.getTime()
56         then: 'setup duration is under 2500 milliseconds'
57             assert setupDurationInMillis < 2500
58         and: 'read duration is under 180 milliseconds'
59             assert readDurationInMillis < 180
60         and: 'data node is returned with all the descendants populated'
61             assert countDataNodes(result) == 1533
62     }
63
64     def createLineage() {
65         def numOfChildren = 30
66         def numOfGrandChildren = 50
67         (1..numOfChildren).each {
68             def childName = "perf-test-child-${it}".toString()
69             def newChild = goForthAndMultiply(XPATH_DATA_NODE_WITH_DESCENDANTS, childName, numOfGrandChildren)
70             objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
71         }
72     }
73
74     def goForthAndMultiply(parentXpath, childName, numOfGrandChildren) {
75         def children = []
76         (1..numOfGrandChildren).each {
77             def child = new DataNodeBuilder().withXpath("${parentXpath}/${childName}/${it}-grand-child").build()
78             children.add(child)
79         }
80         return new DataNodeBuilder().withXpath("${parentXpath}/${childName}").withChildDataNodes(children).build()
81     }
82
83     def countDataNodes(DataNode dataNode) {
84         int nodeCount = 1
85         for (DataNode child : dataNode.childDataNodes) {
86             nodeCount = nodeCount + (countDataNodes(child))
87         }
88         return nodeCount
89     }
90 }