Introduce Instrumentation
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsPersistencePerfSpecBase.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 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.impl
22
23 import org.onap.cps.spi.model.DataNode
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import org.springframework.util.StopWatch
26
27 class CpsPersistencePerfSpecBase extends CpsPersistenceSpecBase {
28
29     static final String PERF_TEST_DATA = '/data/perf-test.sql'
30     static final String PERF_DATASPACE = 'PERF-DATASPACE'
31     static final String PERF_ANCHOR = 'PERF-ANCHOR'
32     static final String PERF_TEST_PARENT = '/perf-parent-1'
33
34     static def xpathsToAllGrandChildren = []
35
36     static def PERFORMANCE_RECORD = []
37
38     def stopWatch = new StopWatch()
39
40     def cleanupSpec() {
41         println('#############################################################################')
42         println('##             P E R F O R M A N C E   T E S T   R E S U L T S             ##')
43         println('#############################################################################')
44         PERFORMANCE_RECORD.sort().each { println(it) }
45         PERFORMANCE_RECORD.clear()
46     }
47
48     def createLineage(cpsDataPersistenceService, numberOfChildren, numberOfGrandChildren, createLists) {
49         xpathsToAllGrandChildren = []
50         (1..numberOfChildren).each {
51             if (createLists) {
52                 def xpathFormat = "${PERF_TEST_PARENT}/perf-test-list-${it}[@key='%d']"
53                 def listElements = goForthAndMultiply(xpathFormat, numberOfGrandChildren)
54                 cpsDataPersistenceService.addListElements(PERF_DATASPACE, PERF_ANCHOR, PERF_TEST_PARENT, listElements)
55             } else {
56                 def xpathFormat = "${PERF_TEST_PARENT}/perf-test-child-${it}/perf-test-grand-child-%d"
57                 def grandChildren = goForthAndMultiply(xpathFormat, numberOfGrandChildren)
58                 def child = new DataNodeBuilder()
59                     .withXpath("${PERF_TEST_PARENT}/perf-test-child-${it}")
60                     .withChildDataNodes(grandChildren)
61                     .build()
62                 cpsDataPersistenceService.addChildDataNode(PERF_DATASPACE, PERF_ANCHOR, PERF_TEST_PARENT, child)
63             }
64         }
65     }
66
67     def goForthAndMultiply(xpathFormat, numberOfGrandChildren) {
68         def grandChildren = []
69         (1..numberOfGrandChildren).each {
70             def xpath = String.format(xpathFormat as String, it)
71             def grandChild = new DataNodeBuilder().withXpath(xpath).build()
72             xpathsToAllGrandChildren.add(grandChild.xpath)
73             grandChildren.add(grandChild)
74         }
75         return grandChildren
76     }
77
78     def countDataNodes(dataNodes) {
79         int nodeCount = 1
80         for (DataNode parent : dataNodes) {
81             for (DataNode child : parent.childDataNodes) {
82                 nodeCount = nodeCount + (countDataNodes(child))
83             }
84         }
85         return nodeCount
86     }
87
88     def recordAndAssertPerformance(String shortTitle, thresholdInMs, recordedTimeInMs) {
89         def pass = recordedTimeInMs <= thresholdInMs
90         if (shortTitle.length()>40) {
91             shortTitle = shortTitle.substring(0,40)
92         }
93         def record = String.format('%2d.%-40s limit%,7d took %,7d ms ', PERFORMANCE_RECORD.size()+1, shortTitle, thresholdInMs, recordedTimeInMs)
94         record += pass?'PASS':'FAIL'
95         PERFORMANCE_RECORD.add(record)
96         assert recordedTimeInMs <= thresholdInMs
97         return true
98     }
99 }