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
31 import java.util.concurrent.TimeUnit
33 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
34 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
36 class CpsToDataNodePerfTest extends CpsPersistenceSpecBase {
38 static final String PERF_TEST_DATA = '/data/perf-test.sql'
41 CpsDataPersistenceService objectUnderTest
43 static def PERF_TEST_PARENT = '/perf-parent-1'
44 static def NUMBER_OF_CHILDREN = 200
45 static def NUMBER_OF_GRAND_CHILDREN = 50
46 static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN) // Parent + Children + Grand-children
47 static def ALLOWED_SETUP_TIME_MS = TimeUnit.SECONDS.toMillis(10)
48 static def ALLOWED_READ_TIME_AL_NODES_MS = 500
50 def readStopWatch = new StopWatch()
52 @Sql([CLEAR_DATA, PERF_TEST_DATA])
53 def 'Create a node with many descendants (please note, subsequent tests depend on this running first).'() {
54 given: 'a node with a large number of descendants is created'
55 def setupStopWatch = new StopWatch()
56 setupStopWatch.start()
59 def setupDurationInMillis = setupStopWatch.getTime()
60 and: 'setup duration is under #ALLOWED_SETUP_TIME_MS milliseconds'
61 assert setupDurationInMillis < ALLOWED_SETUP_TIME_MS
64 def 'Get data node with many descendants by xpath #scenario'() {
65 when: 'get parent is executed with all descendants'
67 def result = objectUnderTest.getDataNode('PERF-DATASPACE', 'PERF-ANCHOR', xpath, INCLUDE_ALL_DESCENDANTS)
69 def readDurationInMillis = readStopWatch.getTime()
70 then: 'read duration is under 500 milliseconds'
71 assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
72 and: 'data node is returned with all the descendants populated'
73 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
74 where: 'the following xPaths are used'
76 'parent' || PERF_TEST_PARENT
80 def 'Query parent data node with many descendants by cps-path'() {
81 when: 'query is executed with all descendants'
84 def result = objectUnderTest.queryDataNodes('PERF-DATASPACE', 'PERF-ANCHOR', '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
86 def readDurationInMillis = readStopWatch.getTime()
87 then: 'read duration is under 500 milliseconds'
88 assert readDurationInMillis < ALLOWED_READ_TIME_AL_NODES_MS
89 and: 'data node is returned with all the descendants populated'
90 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
93 def 'Query many descendants by cps-path with #scenario'() {
94 when: 'query is executed with all descendants'
97 def result = objectUnderTest.queryDataNodes('PERF-DATASPACE', 'PERF-ANCHOR', '//perf-test-grand-child-1', descendantsOption)
99 def readDurationInMillis = readStopWatch.getTime()
100 then: 'read duration is under 500 milliseconds'
101 assert readDurationInMillis < alowedDuration
102 and: 'data node is returned with all the descendants populated'
103 assert result.size() == NUMBER_OF_CHILDREN
104 where: 'the following options are used'
105 scenario | descendantsOption || alowedDuration
106 'omit descendants ' | OMIT_DESCENDANTS || 150
107 'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS || 1500
110 def createLineage() {
111 (1..NUMBER_OF_CHILDREN).each {
112 def childName = "perf-test-child-${it}".toString()
113 def newChild = goForthAndMultiply(PERF_TEST_PARENT, childName)
114 objectUnderTest.addChildDataNode('PERF-DATASPACE', 'PERF-ANCHOR', PERF_TEST_PARENT, newChild)
118 def goForthAndMultiply(parentXpath, childName) {
120 (1..NUMBER_OF_GRAND_CHILDREN).each {
121 def child = new DataNodeBuilder().withXpath("${parentXpath}/${childName}/perf-test-grand-child-${it}").build()
124 return new DataNodeBuilder().withXpath("${parentXpath}/${childName}").withChildDataNodes(children).build()
127 def countDataNodes(dataNodes) {
129 for (DataNode parent : dataNodes) {
130 for (DataNode child : parent.childDataNodes) {
131 nodeCount = nodeCount + (countDataNodes(child))