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