Introduce Instrumentation
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / performance / CpsDataPersistenceServicePerfTest.groovy
1 /*
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
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.onap.cps.spi.impl.CpsPersistencePerfSpecBase
24 import org.springframework.util.StopWatch
25 import org.onap.cps.spi.CpsDataPersistenceService
26 import org.onap.cps.spi.repository.AnchorRepository
27 import org.onap.cps.spi.repository.DataspaceRepository
28 import org.onap.cps.spi.repository.FragmentRepository
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.test.context.jdbc.Sql
31
32 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
33 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
34
35 class CpsDataPersistenceServicePerfTest extends CpsPersistencePerfSpecBase {
36
37     @Autowired
38     CpsDataPersistenceService objectUnderTest
39
40     @Autowired
41     DataspaceRepository dataspaceRepository
42
43     @Autowired
44     AnchorRepository anchorRepository
45
46     @Autowired
47     FragmentRepository fragmentRepository
48
49     static def NUMBER_OF_CHILDREN = 200
50     static def NUMBER_OF_GRAND_CHILDREN = 50
51     static def TOTAL_NUMBER_OF_NODES = 1 + NUMBER_OF_CHILDREN + (NUMBER_OF_CHILDREN * NUMBER_OF_GRAND_CHILDREN)  //  Parent + Children +  Grand-children
52
53     @Sql([CLEAR_DATA, PERF_TEST_DATA])
54     def 'Create a node with many descendants (please note, subsequent tests depend on this running first).'() {
55         given: 'a node with a large number of descendants is created'
56             stopWatch.start()
57             createLineage(objectUnderTest, NUMBER_OF_CHILDREN, NUMBER_OF_GRAND_CHILDREN, false)
58             stopWatch.stop()
59             def setupDurationInMillis = stopWatch.getTotalTimeMillis()
60         and: 'setup duration is under 10 seconds'
61             recordAndAssertPerformance('Setup', 10000, setupDurationInMillis)
62     }
63
64     def 'Get data node with many descendants by xpath #scenario'() {
65         when: 'get parent is executed with all descendants'
66             stopWatch.start()
67             def result = objectUnderTest.getDataNode(PERF_DATASPACE, PERF_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS)
68             stopWatch.stop()
69             def readDurationInMillis = stopWatch.getTotalTimeMillis()
70         then: 'read duration is under 500 milliseconds'
71             recordAndAssertPerformance("Get ${scenario}", 500, readDurationInMillis)
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'
75             scenario || xpath
76             'parent' || PERF_TEST_PARENT
77             'root'   || ''
78     }
79
80     def 'Query parent data node with many descendants by cps-path'() {
81         when: 'query is executed with all descendants'
82             stopWatch.start()
83             def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR, '//perf-parent-1' , INCLUDE_ALL_DESCENDANTS)
84             stopWatch.stop()
85             def readDurationInMillis = stopWatch.getTotalTimeMillis()
86         then: 'read duration is under 500 milliseconds'
87             recordAndAssertPerformance('Query with many descendants', 500, readDurationInMillis)
88         and: 'data node is returned with all the descendants populated'
89             assert countDataNodes(result) == TOTAL_NUMBER_OF_NODES
90     }
91
92     def 'Performance of finding multiple xpaths'() {
93         when: 'we query for all grandchildren (except 1 for fun) with the new native method'
94             xpathsToAllGrandChildren.remove(0)
95             stopWatch.start()
96             def result = objectUnderTest.getDataNodes(PERF_DATASPACE, PERF_ANCHOR, xpathsToAllGrandChildren, INCLUDE_ALL_DESCENDANTS)
97             stopWatch.stop()
98             def readDurationInMillis = stopWatch.getTotalTimeMillis()
99         then: 'the returned number of entities equal to the number of children * number of grandchildren'
100             assert result.size() == xpathsToAllGrandChildren.size()
101         and: 'it took less then 4000ms'
102             recordAndAssertPerformance('Find multiple xpaths', 4000, readDurationInMillis)
103     }
104
105     def 'Query many descendants by cps-path with #scenario'() {
106         when: 'query is executed with all descendants'
107             stopWatch.start()
108             def result = objectUnderTest.queryDataNodes(PERF_DATASPACE, PERF_ANCHOR,  '//perf-test-grand-child-1', descendantsOption)
109             stopWatch.stop()
110             def readDurationInMillis = stopWatch.getTotalTimeMillis()
111         then: 'read duration is under #allowedDuration milliseconds'
112             assert readDurationInMillis < allowedDuration
113             recordAndAssertPerformance("Query many descendants by cpspath (${scenario})", allowedDuration, readDurationInMillis)
114         and: 'data node is returned with all the descendants populated'
115             assert result.size() == NUMBER_OF_CHILDREN
116         where: 'the following options are used'
117             scenario                                        | descendantsOption        || allowedDuration
118             'omit descendants                             ' | OMIT_DESCENDANTS         || 150
119             'include descendants (although there are none)' | INCLUDE_ALL_DESCENDANTS  || 150
120     }
121 }