1# -*- coding: utf-8 -*- 2 3#------------------------------------------------------------------------- 4# Quality Program utilities 5# -------------------------------------- 6# 7# Copyright 2018 8# 9# Licensed under the Apache License, Version 2.0 (the "License"); 10# you may not use this file except in compliance with the License. 11# You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, software 16# distributed under the License is distributed on an "AS IS" BASIS, 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18# See the License for the specific language governing permissions and 19# limitations under the License. 20# 21#------------------------------------------------------------------------- 22 23import os 24import copy 25import sys 26import xml.sax 27import xml.sax.handler 28from log_parser import BatchResultParser, StatusCode 29 30class TimeOfExecutionGroups() : 31 def __init__(self): 32 self.path = "" 33 self.numberOfTests = 0 34 self.timeOfExecution = 0 35 36class TimeOfExecutionTests() : 37 def __init__(self): 38 self.path = "" 39 self.timeOfExecution = 0 40 41def sortKey (element ) : 42 return int(element.timeOfExecution) 43 44def sortKeyTimePerTest (element) : 45 return int(int(element.timeOfExecution)/int(element.numberOfTests)) 46 47class XMLLogHandlerTests(xml.sax.handler.ContentHandler) : 48 def __init__ (self): 49 self.list = [] 50 self.element = TimeOfExecutionTests() 51 self.testTime = False 52 53 def startElement (self, name, attrs): 54 if name == "TestCaseResult" : 55 self.element.path = attrs.getValue("CasePath") 56 if name == "Number" and "TestDuration" == attrs.getValue("Name") : 57 self.testTime = True 58 59 def characters(self, content) : 60 if self.testTime : 61 self.testTime = False 62 self.element.timeOfExecution = content 63 self.list.append(copy.deepcopy(self.element)) 64 65 def bottleneck (self, resultCount) : 66 print("The biggest tests time of execution") 67 print('%-4s%12s\t%12s' % ("Index", "Time", "Full name")) 68 self.list.sort(key = sortKey, reverse = True) 69 ndx = 1 70 for test in self.list : 71 print('%-4i%12i\t%12s' % (int(ndx), int(test.timeOfExecution), test.path)) 72 ndx+=1 73 if int(ndx) > int(resultCount) : 74 break 75 76class XMLLogHandlerGroups(xml.sax.handler.ContentHandler) : 77 def __init__ (self, testList) : 78 self.list = [] 79 self.testList = testList 80 self.element = TimeOfExecutionGroups() 81 self.addIt = False 82 83 def startElement (self, name, attrs) : 84 self.element.numberOfTests = 0 85 if name == "Number" : 86 self.element.path = attrs.getValue("Name") 87 if self.element.path == "dEQP-VK" : 88 self.addIt = True 89 self.element.numberOfTests = len(self.testList) 90 else : 91 for test in self.testList : 92 if test.path[:test.path.rfind(".")] in self.element.path : 93 self.addIt = True 94 self.element.numberOfTests += 1 95 96 def characters(self, content) : 97 if self.addIt : 98 self.addIt = False 99 self.element.timeOfExecution = content 100 self.list.append(copy.deepcopy(self.element)) 101 102 def bottleneck (self, resultCount) : 103 self.list.sort(key = sortKey, reverse = True) 104 print("\nGroups Statistics") 105 print("Total time of execution:\t", self.list[0].timeOfExecution) 106 print("Number of executed tests:\t", self.list[0].numberOfTests) 107 print("\nThe biggest total time of execution") 108 print('%-4s%15s%15s\t%-30s' % ("Index", "Time", "Test count", "Full name")) 109 ndx = 1 110 for test in self.list : 111 if test.path == "dEQP-VK" : 112 continue 113 print('%-4s%15s%15s\t%-30s' % (ndx, test.timeOfExecution, test.numberOfTests, test.path)) 114 ndx+=1 115 if int(ndx) > int(resultCount) : 116 break 117 self.list.sort(key = sortKeyTimePerTest, reverse = True) 118 print("\nThe biggest time of execution per test") 119 print('%-4s%15s%15s%15s\t%-30s' % ("Index", "Time", "Test count", "\tAvg. test time", "Full name")) 120 ndx = 1 121 for test in self.list : 122 if test.path == "dEQP-VK" : 123 continue 124 print('%-4s%15s%15s%15i\t%-30s' % (ndx, test.timeOfExecution, test.numberOfTests, int(test.timeOfExecution)/int(test.numberOfTests), test.path)) 125 ndx+=1 126 if int(ndx) > int(resultCount) : 127 break 128 129class LogErrorHandler(xml.sax.handler.ErrorHandler) : 130 def __init__ (self) : 131 pass 132 133 def error (self, err) : 134 #print("error(%s)" % str(err)) 135 pass 136 137 def fatalError (self, err) : 138 #print("fatalError(%s)" % str(err)) 139 pass 140 141 def warning (self, warn) : 142 #print("warning(%s)" % str(warn)) 143 pass 144 145def findFirstElementByName (nodes, name) : 146 for node in nodes: 147 if node.nodeName == name : 148 return node 149 chFound = findFirstElementByName(node.childNodes, name) 150 if chFound != None : 151 return chFound 152 return None 153 154def printTimes (inFile, resultCount) : 155 #Test section 156 parser = BatchResultParser() 157 results = parser.parseFile(inFile) 158 handler = XMLLogHandlerTests() 159 errHandler = LogErrorHandler() 160 for result in results : 161 xml.sax.parseString(result.log, handler, errHandler) 162 handler.bottleneck(resultCount) 163 164 #Group section 165 startRecordLines = False 166 lines = "" 167 f = open(inFile, 'rb') 168 for line in f : 169 if "#endTestsCasesTime" in line : 170 break 171 if startRecordLines : 172 lines += line 173 if "#beginTestsCasesTime" in line : 174 startRecordLines = True 175 f.close() 176 handlerGroups = XMLLogHandlerGroups(handler.list) 177 xml.sax.parseString(lines, handlerGroups, errHandler) 178 handlerGroups.bottleneck(resultCount) 179 180if __name__ == "__main__" : 181 if len(sys.argv) != 3: 182 print("%s: [test log] [count of result to display]" % sys.argv[0]) 183 print("example: python %s TestResults.qpa 10" % sys.argv[0]) 184 sys.exit(-1) 185 printTimes(sys.argv[1], sys.argv[2]) 186