2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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.onap.cps.spi.impl.CpsPersistencePerfSpecBase
24 import org.onap.cps.spi.CpsDataPersistenceService
25 import org.onap.cps.spi.repository.AnchorRepository
26 import org.onap.cps.spi.repository.DataspaceRepository
27 import org.onap.cps.spi.repository.FragmentRepository
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.jdbc.Sql
31 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
32 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
34 class CpsDataPersistenceServicePerfTest extends CpsPersistencePerfSpecBase {
37 CpsDataPersistenceService objectUnderTest
40 DataspaceRepository dataspaceRepository
43 AnchorRepository anchorRepository
46 FragmentRepository fragmentRepository
48 static def NUMBER_OF_CHILDREN = 200
49 static def NUMBER_OF_GRAND_CHILDREN = 50
50 static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN) // Parent + Children + Grand-children
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'
56 createLineage(objectUnderTest, NUMBER_OF_CHILDREN, NUMBER_OF_GRAND_CHILDREN, false)
58 def setupDurationInMillis = stopWatch.getTotalTimeMillis()
59 and: 'setup duration is under 10 seconds'
60 recordAndAssertPerformance('Setup', 10000, setupDurationInMillis)
63 def 'Get data node with many descendants by xpath #scenario'() {
64 when: 'get parent is executed with all descendants'
66 def result = objectUnderTest.getDataNodes(PERF_DATASPACE, PERF_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS)
68 def readDurationInMillis = stopWatch.getTotalTimeMillis()
69 then: 'read duration is under #allowedDuration milliseconds'
70 recordAndAssertPerformance("Get ${scenario}", allowedDuration, readDurationInMillis)
71 and: 'data node is returned with all the descendants populated'
72 assert countDataNodes(result[0]) == TOTAL_NUMBER_OF_NODES
73 where: 'the following xPaths are used'
74 scenario | xpath || allowedDuration
75 'parent' | PERF_TEST_PARENT || 3500
79 def 'Query parent data node with many descendants by cps-path'() {
80 when: 'query is executed with all descendants'
82 def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
84 def readDurationInMillis = stopWatch.getTotalTimeMillis()
85 then: 'read duration is under 500 milliseconds'
86 recordAndAssertPerformance('Query with many descendants', 500, readDurationInMillis)
87 and: 'data node is returned with all the descendants populated'
88 assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
91 def 'Performance of finding multiple xpaths'() {
92 when: 'we query for all grandchildren (except 1 for fun) with the new native method'
93 xpathsToAllGrandChildren.remove(0)
95 def result = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpathsToAllGrandChildren, INCLUDE_ALL_DESCENDANTS)
97 def readDurationInMillis = stopWatch.getTotalTimeMillis()
98 then: 'the returned number of entities equal to the number of children * number of grandchildren'
99 assert result.size() == xpathsToAllGrandChildren.size()
100 and: 'it took less then 3000ms'
101 recordAndAssertPerformance('Find multiple xpaths', 3000, readDurationInMillis)
104 def 'Query many descendants by cps-path with #scenario'() {
105 when: 'query is executed with all descendants'
107 def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-test-grand-child-1', descendantsOption)
109 def readDurationInMillis = stopWatch.getTotalTimeMillis()
110 then: 'read duration is under #allowedDuration milliseconds'
111 recordAndAssertPerformance("Query many descendants by cpspath (${scenario})", allowedDuration, readDurationInMillis)
112 and: 'data node is returned with all the descendants populated'
113 assert result.size() == NUMBER_OF_CHILDREN
114 where: 'the following options are used'
115 scenario | descendantsOption || allowedDuration
116 'omit descendants ' | OMIT_DESCENDANTS || 150
117 'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS || 150
120 def 'Update data nodes with descendants'() {
121 given: 'a list of xpaths to data nodes with descendants (xpath for each child)'
122 def xpaths = (1..20).collect {
123 "${PERF_TEST_PARENT}/perf-test-child-${it}".toString()
125 and: 'the correct number of data nodes are fetched'
126 def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, INCLUDE_ALL_DESCENDANTS)
127 assert dataNodes.size() == 20
128 assert countDataNodes(dataNodes) == 20 + 20 * 50
129 when: 'the fragment entities are updated by the data nodes'
131 objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
133 def updateDurationInMillis = stopWatch.getTotalTimeMillis()
134 then: 'update duration is under 600 milliseconds'
135 recordAndAssertPerformance('Update data nodes with descendants', 600, updateDurationInMillis)
138 def 'Update data nodes without descendants'() {
139 given: 'a list of xpaths to data nodes without descendants (xpath for each grandchild)'
141 for (int childIndex = 21; childIndex <= 40; childIndex++) {
142 xpaths.addAll((1..50).collect {
143 "${PERF_TEST_PARENT}/perf-test-child-${childIndex}/perf-test-grand-child-${it}".toString()
146 and: 'the correct number of data nodes are fetched'
147 def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(PERF_DATASPACE, PERF_ANCHOR, xpaths, OMIT_DESCENDANTS)
148 assert dataNodes.size() == 20 * 50
149 assert countDataNodes(dataNodes) == 20 * 50
150 when: 'the fragment entities are updated by the data nodes'
152 objectUnderTest.updateDataNodesAndDescendants(PERF_DATASPACE, PERF_ANCHOR, dataNodes)
154 def updateDurationInMillis = stopWatch.getTotalTimeMillis()
155 then: 'update duration is under 600 milliseconds'
156 recordAndAssertPerformance('Update data nodes without descendants', 600, updateDurationInMillis)