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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.performance
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
33 class CpsToDataNodePerfTest extends CpsPersistenceSpecBase {
35 static final String SET_DATA = '/data/fragment.sql'
38 CpsDataPersistenceService objectUnderTest
40 def XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
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()
49 def setupDurationInMillis = setupStopWatch.getTime()
50 when: 'data node is requested with all descendants'
51 def readStopWatch = new StopWatch()
53 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, INCLUDE_ALL_DESCENDANTS)
55 def readDurationInMillis = readStopWatch.getTime()
56 then: 'setup duration is under 8 seconds'
57 assert setupDurationInMillis < 8000
58 and: 'read duration is under 1500 milliseconds'
59 assert readDurationInMillis < 1500
60 and: 'data node is returned with all the descendants populated'
61 assert countDataNodes(result) == 1533
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)
74 def goForthAndMultiply(parentXpath, childName, numOfGrandChildren) {
76 (1..numOfGrandChildren).each {
77 def child = new DataNodeBuilder().withXpath("${parentXpath}/${childName}/${it}-grand-child").build()
80 return new DataNodeBuilder().withXpath("${parentXpath}/${childName}").withChildDataNodes(children).build()
83 def countDataNodes(DataNode dataNode) {
85 for (DataNode child : dataNode.childDataNodes) {
86 nodeCount = nodeCount + (countDataNodes(child))