Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf8 -*- |
| 3 | |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 4 | from __future__ import print_function |
| 5 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | import math, os.path, re, sys, time, unittest |
| 7 | |
| 8 | def setupSysPath(): |
| 9 | testPath = sys.path[0] |
Johnny Chen | a02199f | 2010-06-22 00:11:26 +0000 | [diff] [blame] | 10 | rem = re.match("(^.*/)test$", testPath) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | if not rem: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 12 | print("This script expects to reside in .../test.") |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | sys.exit(-1) |
| 14 | lldbBasePath = rem.group(1) |
| 15 | lldbDebugPythonPath = "build/Debug/LLDB.framework/Resources/Python" |
| 16 | lldbReleasePythonPath = "build/Release/LLDB.framework/Resources/Python" |
| 17 | lldbPythonPath = None |
| 18 | if os.path.isfile(lldbDebugPythonPath + "/lldb.py"): |
| 19 | lldbPythonPath = lldbDebugPythonPath |
| 20 | if os.path.isfile(lldbReleasePythonPath + "/lldb.py"): |
| 21 | lldbPythonPath = lldbReleasePythonPath |
| 22 | if not lldbPythonPath: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 23 | print("This script requires lldb.py to be in either " + lldbDebugPythonPath, end='') |
| 24 | print("or" + lldbReleasePythonPath) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | sys.exit(-1) |
| 26 | sys.path.append(lldbPythonPath) |
| 27 | |
| 28 | def prettyTime(t): |
| 29 | if t == 0.0: |
| 30 | return "0s" |
| 31 | if t < 0.000001: |
| 32 | return ("%.3f" % (t * 1000000000.0)) + "ns" |
| 33 | if t < 0.001: |
| 34 | return ("%.3f" % (t * 1000000.0)) + "µs" |
| 35 | if t < 1: |
| 36 | return ("%.3f" % (t * 1000.0)) + "ms" |
| 37 | return str(t) + "s" |
| 38 | |
| 39 | class ExecutionTimes: |
| 40 | @classmethod |
| 41 | def executionTimes(cls): |
| 42 | if cls.m_executionTimes == None: |
| 43 | cls.m_executionTimes = ExecutionTimes() |
| 44 | for i in range(100): |
| 45 | cls.m_executionTimes.start() |
| 46 | cls.m_executionTimes.end("null") |
| 47 | return cls.m_executionTimes |
| 48 | def __init__(self): |
| 49 | self.m_times = dict() |
| 50 | def start(self): |
| 51 | self.m_start = time.time() |
| 52 | def end(self, component): |
| 53 | e = time.time() |
| 54 | if component not in self.m_times: |
| 55 | self.m_times[component] = list() |
| 56 | self.m_times[component].append(e - self.m_start) |
| 57 | def dumpStats(self): |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame^] | 58 | for key in list(self.m_times.keys()): |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | if len(self.m_times[key]): |
| 60 | sampleMin = float('inf') |
| 61 | sampleMax = float('-inf') |
| 62 | sampleSum = 0.0 |
| 63 | sampleCount = 0.0 |
| 64 | for time in self.m_times[key]: |
| 65 | if time > sampleMax: |
| 66 | sampleMax = time |
| 67 | if time < sampleMin: |
| 68 | sampleMin = time |
| 69 | sampleSum += time |
| 70 | sampleCount += 1.0 |
| 71 | sampleMean = sampleSum / sampleCount |
| 72 | sampleVariance = 0 |
| 73 | for time in self.m_times[key]: |
| 74 | sampleVariance += (time - sampleMean) ** 2 |
| 75 | sampleVariance /= sampleCount |
| 76 | sampleStandardDeviation = math.sqrt(sampleVariance) |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 77 | print(key + ": [" + prettyTime(sampleMin) + ", " + prettyTime(sampleMax) + "] ", end='') |
| 78 | print("µ " + prettyTime(sampleMean) + ", σ " + prettyTime(sampleStandardDeviation)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | m_executionTimes = None |
| 80 | |
| 81 | setupSysPath() |
| 82 | |
| 83 | import lldb |
| 84 | |
| 85 | class LLDBTestCase(unittest.TestCase): |
| 86 | def setUp(self): |
Johnny Chen | f94c8c2 | 2010-06-25 23:22:48 +0000 | [diff] [blame] | 87 | debugger = lldb.SBDebugger.Create() |
| 88 | debugger.SetAsync(True) |
| 89 | self.m_commandInterpreter = debugger.GetCommandInterpreter() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | if not self.m_commandInterpreter: |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 91 | print("Couldn't get the command interpreter") |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | sys.exit(-1) |
| 93 | def runCommand(self, command, component): |
| 94 | res = lldb.SBCommandReturnObject() |
| 95 | ExecutionTimes.executionTimes().start() |
| 96 | self.m_commandInterpreter.HandleCommand(command, res, False) |
| 97 | ExecutionTimes.executionTimes().end(component) |
| 98 | if res.Succeeded(): |
| 99 | return res.GetOutput() |
| 100 | else: |
| 101 | self.fail("Command " + command + " returned an error") |
| 102 | return None |
Enrico Granata | 165f8af | 2012-09-21 19:10:53 +0000 | [diff] [blame] | 103 | def getCategories(self): |
| 104 | return [] |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | |
| 106 | class SanityCheckTestCase(LLDBTestCase): |
| 107 | def runTest(self): |
| 108 | ret = self.runCommand("show arch", "show-arch") |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 109 | #print(ret) |
Enrico Granata | 165f8af | 2012-09-21 19:10:53 +0000 | [diff] [blame] | 110 | def getCategories(self): |
| 111 | return [] |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | |
| 113 | suite = unittest.TestLoader().loadTestsFromTestCase(SanityCheckTestCase) |
| 114 | unittest.TextTestRunner(verbosity=2).run(suite) |
| 115 | ExecutionTimes.executionTimes().dumpStats() |