blob: f39655edd1982f449dd921ad131b3802748af70f [file] [log] [blame]
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +02001# Process the test results
2# Test status (like passed, or failed with error code)
3
4import argparse
5import re
6import TestScripts.NewParser as parse
7import TestScripts.CodeGen
8from collections import deque
9import os.path
10import csv
Christophe Favergeonf76a8032019-08-09 09:15:50 +010011import TestScripts.ParseTrace
Christophe Favergeon30c03792019-10-03 12:47:41 +010012import colorama
13from colorama import init,Fore, Back, Style
Christophe Favergeonf76a8032019-08-09 09:15:50 +010014
Christophe Favergeon30c03792019-10-03 12:47:41 +010015init()
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +020016
Christophe Favergeon6f8eee92019-10-09 12:21:27 +010017def errorStr(id):
18 if id == 1:
19 return("UNKNOWN_ERROR")
20 if id == 2:
21 return("Equality error")
22 if id == 3:
23 return("Absolute difference error")
24 if id == 4:
25 return("Relative difference error")
26 if id == 5:
27 return("SNR error")
28 if id == 6:
29 return("Different length error")
30 if id == 7:
31 return("Assertion error")
32 if id == 8:
33 return("Memory allocation error")
34 if id == 9:
35 return("Empty pattern error")
36 if id == 10:
37 return("Buffer tail corrupted")
Christophe Favergeonf055bd32019-10-15 12:30:30 +010038 if id == 11:
39 return("Close float error")
Christophe Favergeon6f8eee92019-10-09 12:21:27 +010040
41 return("Unknown error %d" % id)
42
43
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +020044def findItem(root,path):
45 """ Find a node in a tree
46
47 Args:
48 path (list) : A list of node ID
49 This list is describing a path in the tree.
50 By starting from the root and following this path,
51 we can find the node in the tree.
52 Raises:
53 Nothing
54 Returns:
55 TreeItem : A node
56 """
57 # The list is converted into a queue.
58 q = deque(path)
59 q.popleft()
60 c = root
61 while q:
62 n = q.popleft()
63 # We get the children based on its ID and continue
64 c = c[n-1]
65 return(c)
66
67def joinit(iterable, delimiter):
68 # Intersperse a delimiter between element of a list
69 it = iter(iterable)
70 yield next(it)
71 for x in it:
72 yield delimiter
73 yield x
74
75# Return test result as a text tree
76class TextFormatter:
77 def start(self):
78 None
79
80 def printGroup(self,elem,theId):
81 if elem is None:
82 elem = root
83 message=elem.data["message"]
84 if not elem.data["deprecated"]:
85 kind = "Suite"
86 ident = " " * elem.ident
87 if elem.kind == TestScripts.Parser.TreeElem.GROUP:
88 kind = "Group"
89 #print(elem.path)
Christophe Favergeon30c03792019-10-03 12:47:41 +010090 print(Style.BRIGHT + ("%s%s : %s (%d)" % (ident,kind,message,theId)) + Style.RESET_ALL)
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +020091
92 def printTest(self,elem, theId, theError,theLine,passed,cycles,params):
93 message=elem.data["message"]
94 if not elem.data["deprecated"]:
95 kind = "Test"
96 ident = " " * elem.ident
Christophe Favergeon30c03792019-10-03 12:47:41 +010097 p=Fore.RED + "FAILED" + Style.RESET_ALL
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +020098 if passed == 1:
Christophe Favergeon30c03792019-10-03 12:47:41 +010099 p= Fore.GREEN + "PASSED" + Style.RESET_ALL
100 print("%s%s %s(%d)%s : %s (cycles = %d)" % (ident,message,Style.BRIGHT,theId,Style.RESET_ALL,p,cycles))
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200101 if params:
102 print("%s %s" % (ident,params))
103 if passed != 1:
Christophe Favergeon6f8eee92019-10-09 12:21:27 +0100104 print(Fore.RED + ("%s %s at line %d" % (ident, errorStr(theError), theLine)) + Style.RESET_ALL)
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200105
106 def pop(self):
107 None
108
109 def end(self):
110 None
111
112# Return test result as a CSV
113class CSVFormatter:
114
115 def __init__(self):
116 self.name=[]
117 self._start=True
118
119 def start(self):
120 print("CATEGORY,NAME,ID,STATUS,CYCLES,PARAMS")
121
122 def printGroup(self,elem,theId):
123 if elem is None:
124 elem = root
125 # Remove Root from category name in CSV file.
126 if not self._start:
127 self.name.append(elem.data["class"])
128 else:
129 self._start=False
130 message=elem.data["message"]
131 if not elem.data["deprecated"]:
132 kind = "Suite"
133 ident = " " * elem.ident
134 if elem.kind == TestScripts.Parser.TreeElem.GROUP:
135 kind = "Group"
136
137 def printTest(self,elem, theId, theError, theLine,passed,cycles,params):
138 message=elem.data["message"]
139 if not elem.data["deprecated"]:
140 kind = "Test"
141 name=elem.data["class"]
142 category= "".join(list(joinit(self.name,":")))
143 print("%s,%s,%d,%d,%d,\"%s\"" % (category,name,theId,passed,cycles,params))
144
145 def pop(self):
146 if self.name:
147 self.name.pop()
148
149 def end(self):
150 None
151
152class MathematicaFormatter:
153
154 def __init__(self):
155 self._hasContent=[False]
156 self._toPop=[]
157
158 def start(self):
159 None
160
161 def printGroup(self,elem,theId):
162 if self._hasContent[len(self._hasContent)-1]:
163 print(",",end="")
164
165 print("<|")
166 self._hasContent[len(self._hasContent)-1] = True
167 self._hasContent.append(False)
168 if elem is None:
169 elem = root
170 message=elem.data["message"]
171 if not elem.data["deprecated"]:
172
173 kind = "Suite"
174 ident = " " * elem.ident
175 if elem.kind == TestScripts.Parser.TreeElem.GROUP:
176 kind = "Group"
177 print("\"%s\" ->" % (message))
178 #if kind == "Suite":
179 print("{",end="")
180 self._toPop.append("}")
181 #else:
182 # self._toPop.append("")
183
184 def printTest(self,elem, theId, theError,theLine,passed,cycles,params):
185 message=elem.data["message"]
186 if not elem.data["deprecated"]:
187 kind = "Test"
188 ident = " " * elem.ident
189 p="FAILED"
190 if passed == 1:
191 p="PASSED"
192 parameters=""
193 if params:
194 parameters = "%s" % params
195 if self._hasContent[len(self._hasContent)-1]:
196 print(",",end="")
197 print("<|\"NAME\" -> \"%s\",\"ID\" -> %d,\"STATUS\" -> \"%s\",\"CYCLES\" -> %d,\"PARAMS\" -> \"%s\"|>" % (message,theId,p,cycles,parameters))
198 self._hasContent[len(self._hasContent)-1] = True
199 #if passed != 1:
200 # print("%s Error = %d at line %d" % (ident, theError, theLine))
201
202 def pop(self):
203 print(self._toPop.pop(),end="")
204 print("|>")
205 self._hasContent.pop()
206
207 def end(self):
208 None
209
210NORMAL = 1
211INTEST = 2
212TESTPARAM = 3
213
214def createMissingDir(destPath):
215 theDir=os.path.normpath(os.path.dirname(destPath))
216 if not os.path.exists(theDir):
217 os.makedirs(theDir)
218
219def correctPath(path):
220 while (path[0]=="/") or (path[0] == "\\"):
221 path = path[1:]
222 return(path)
223
224def extractDataFiles(results,outputDir):
225 infile = False
226 f = None
227 for l in results:
228 if re.match(r'^.*D:[ ].*$',l):
229 if infile:
230 if re.match(r'^.*D:[ ]END$',l):
231 infile = False
232 if f:
233 f.close()
234 else:
235 if f:
236 m = re.match(r'^.*D:[ ](.*)$',l)
237 data = m.group(1)
238 f.write(data)
239 f.write("\n")
240
241 else:
242 m = re.match(r'^.*D:[ ](.*)$',l)
243 path = str(m.group(1))
244 infile = True
245 destPath = os.path.join(outputDir,correctPath(path))
246 createMissingDir(destPath)
247 f = open(destPath,"w")
248
249
250
251def writeBenchmark(elem,benchFile,theId,theError,passed,cycles,params,config):
252 if benchFile:
253 name=elem.data["class"]
254 category= elem.categoryDesc()
Christophe Favergeon37b86222019-07-17 11:49:00 +0200255 old=""
256 if "testData" in elem.data:
257 if "oldID" in elem.data["testData"]:
258 old=elem.data["testData"]["oldID"]
259 benchFile.write("\"%s\",\"%s\",%d,\"%s\",%s,%d,%s\n" % (category,name,theId,old,params,cycles,config))
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200260
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100261def getCyclesFromTrace(trace):
262 if not trace:
263 return(0)
264 else:
265 return(TestScripts.ParseTrace.getCycles(trace))
266
Christophe Favergeon5cacf9d2019-08-14 10:41:17 +0200267def analyseResult(resultPath,root,results,embedded,benchmark,trace,formatter):
Christophe Favergeonbe7efb42019-08-09 10:17:03 +0100268 calibration = 0
269 if trace:
270 # First cycle in the trace is the calibration data
271 # The noramlisation factor must be coherent with the C code one.
272 calibration = int(getCyclesFromTrace(trace) / 20)
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200273 formatter.start()
274 path = []
275 state = NORMAL
276 prefix=""
277 elem=None
278 theId=None
279 theError=None
280 theLine=None
281 passed=0
282 cycles=None
283 benchFile = None
284 config=""
285 if embedded:
286 prefix = ".*S:[ ]"
287
288 # Parse the result file.
289 # NORMAL mode is when we are parsing suite or group.
290 # Otherwise we are parsing a test and we need to analyse the
291 # test result.
292 # TESTPARAM is used to read parameters of the test.
293 # Format of output is:
294 #node ident : s id or g id or t or u
295 #test status : id error linenb status Y or N (Y when passing)
296 #param for this test b x,x,x,x or b alone if not param
297 #node end : p
298 # In FPGA mode:
299 #Prefix S:[ ] before driver dump
300 # D:[ ] before data dump (output patterns)
301
302 for l in results:
303 l = l.strip()
304 if not re.match(r'^.*D:[ ].*$',l):
305 if state == NORMAL:
306 if len(l) > 0:
307 # Line starting with g or s is a suite or group.
308 # In FPGA mode, those line are prefixed with 'S: '
309 # and data file with 'D: '
310 if re.match(r'^%s[gs][ ]+[0-9]+.*$' % prefix,l):
311 # Extract the test id
312 theId=re.sub(r'^%s[gs][ ]+([0-9]+).*$' % prefix,r'\1',l)
313 theId=int(theId)
314 path.append(theId)
315 # From a list of id, find the TreeElem in the Parsed tree
316 # to know what is the node.
317 elem = findItem(root,path)
318 # Display formatted output for this node
319 if elem.params:
320 #print(elem.params.full)
321 benchPath = os.path.join(benchmark,elem.fullPath(),"fullBenchmark.csv")
322 createMissingDir(benchPath)
323 if benchFile:
324 printf("ERROR BENCH FILE %s ALREADY OPEN" % benchPath)
325 benchFile.close()
326 benchFile=None
327 benchFile=open(benchPath,"w")
328 header = "".join(list(joinit(elem.params.full,",")))
329 # A test and a benchmark are different
330 # so we don't dump a status and error
331 # A status and error in a benchmark would
332 # impact the cycles since the test
333 # would be taken into account in the measurement
334 # So benchmark are always passing and contain no test
335 #benchFile.write("ID,%s,PASSED,ERROR,CYCLES\n" % header)
336 csvheaders = ""
337
Christophe Favergeon5cacf9d2019-08-14 10:41:17 +0200338 with open(os.path.join(resultPath,'currentConfig.csv'), 'r') as f:
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200339 reader = csv.reader(f)
340 csvheaders = next(reader, None)
341 configList = list(reader)
342 #print(configList)
343 config = "".join(list(joinit(configList[0],",")))
344 configHeaders = "".join(list(joinit(csvheaders,",")))
Christophe Favergeon37b86222019-07-17 11:49:00 +0200345 benchFile.write("CATEGORY,NAME,ID,OLDID,%s,CYCLES,%s\n" % (header,configHeaders))
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200346
347 formatter.printGroup(elem,theId)
348
349 # If we have detected a test, we switch to test mode
350 if re.match(r'^%s[t][ ]*$' % prefix,l):
351 state = INTEST
352
353
354 # Pop
355 # End of suite or group
356 if re.match(r'^%sp.*$' % prefix,l):
357 if benchFile:
358 benchFile.close()
359 benchFile=None
360 path.pop()
361 formatter.pop()
362 elif state == INTEST:
363 if len(l) > 0:
364 # In test mode, we are looking for test status.
365 # A line starting with S
366 # (There may be empty lines or line for data files)
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100367 passRe = r'^%s([0-9]+)[ ]+([0-9]+)[ ]+([0-9]+)[ ]+([t0-9]+)[ ]+([YN]).*$' % prefix
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200368 if re.match(passRe,l):
369 # If we have found a test status then we will start again
370 # in normal mode after this.
371
372 m = re.match(passRe,l)
373
374 # Extract test ID, test error code, line number and status
375 theId=m.group(1)
376 theId=int(theId)
377
378 theError=m.group(2)
379 theError=int(theError)
380
381 theLine=m.group(3)
382 theLine=int(theLine)
383
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100384 maybeCycles = m.group(4)
385 if maybeCycles == "t":
Christophe Favergeonbe7efb42019-08-09 10:17:03 +0100386 cycles = getCyclesFromTrace(trace) - calibration
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100387 else:
388 cycles = int(maybeCycles)
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200389
390 status=m.group(5)
391 passed=0
392
393 # Convert status to number as used by formatter.
394 if status=="Y":
395 passed = 1
396 if status=="N":
397 passed = 0
398 # Compute path to this node
399 newPath=path.copy()
400 newPath.append(theId)
401 # Find the node in the Tree
402 elem = findItem(root,newPath)
403
404
405 state = TESTPARAM
406 else:
407 if re.match(r'^%sp.*$' % prefix,l):
408 if benchFile:
409 benchFile.close()
410 benchFile=None
411 path.pop()
412 formatter.pop()
413 if re.match(r'^%s[t][ ]*$' % prefix,l):
414 state = INTEST
415 else:
416 state = NORMAL
417 else:
418 if len(l) > 0:
419 state = INTEST
420 params=""
421 if re.match(r'^.*b[ ]+([0-9,]+)$',l):
422 m=re.match(r'^.*b[ ]+([0-9,]+)$',l)
423 params=m.group(1).strip()
424 # Format the node
425 #print(elem.fullPath())
426 #createMissingDir(destPath)
427 writeBenchmark(elem,benchFile,theId,theError,passed,cycles,params,config)
428 else:
429 params=""
430 writeBenchmark(elem,benchFile,theId,theError,passed,cycles,params,config)
431 # Format the node
432 formatter.printTest(elem,theId,theError,theLine,passed,cycles,params)
433
434
435 formatter.end()
436
437
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100438def analyze(root,results,args,trace):
Christophe Favergeon5cacf9d2019-08-14 10:41:17 +0200439 # currentConfig.csv should be in the same place
440 resultPath=os.path.dirname(args.r)
441
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100442 if args.c:
Christophe Favergeon5cacf9d2019-08-14 10:41:17 +0200443 analyseResult(resultPath,root,results,args.e,args.b,trace,CSVFormatter())
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100444 elif args.m:
Christophe Favergeon5cacf9d2019-08-14 10:41:17 +0200445 analyseResult(resultPath,root,results,args.e,args.b,trace,MathematicaFormatter())
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100446 else:
Christophe Favergeon5cacf9d2019-08-14 10:41:17 +0200447 analyseResult(resultPath,root,results,args.e,args.b,trace,TextFormatter())
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100448
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200449parser = argparse.ArgumentParser(description='Parse test description')
450
Christophe Favergeon6f8eee92019-10-09 12:21:27 +0100451parser.add_argument('-f', nargs='?',type = str, default="Output.pickle", help="Test description file path")
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200452# Where the result file can be found
453parser.add_argument('-r', nargs='?',type = str, default=None, help="Result file path")
454parser.add_argument('-c', action='store_true', help="CSV output")
455parser.add_argument('-e', action='store_true', help="Embedded test")
456# -o needed when -e is true to know where to extract the output files
457parser.add_argument('-o', nargs='?',type = str, default="Output", help="Output dir path")
458
459parser.add_argument('-b', nargs='?',type = str, default="FullBenchmark", help="Full Benchmark dir path")
460parser.add_argument('-m', action='store_true', help="Mathematica output")
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100461parser.add_argument('-t', nargs='?',type = str, default=None, help="External trace file")
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200462
463args = parser.parse_args()
464
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100465
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200466if args.f is not None:
Christophe Favergeon6f8eee92019-10-09 12:21:27 +0100467 #p = parse.Parser()
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200468 # Parse the test description file
Christophe Favergeon6f8eee92019-10-09 12:21:27 +0100469 #root = p.parse(args.f)
470 root=parse.loadRoot(args.f)
Christophe Favergeonf76a8032019-08-09 09:15:50 +0100471 if args.t:
472 with open(args.t,"r") as trace:
473 with open(args.r,"r") as results:
474 analyze(root,results,args,iter(trace))
475 else:
476 with open(args.r,"r") as results:
477 analyze(root,results,args,None)
Christophe Favergeon3b2a0ee2019-06-12 13:29:14 +0200478 if args.e:
479 # In FPGA mode, extract output files from stdout (result file)
480 with open(args.r,"r") as results:
481 extractDataFiles(results,args.o)
482
483else:
484 parser.print_help()