• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf8 -*-
3
4import math, os.path, re, sys, time, unittest
5
6def setupSysPath():
7  testPath = sys.path[0]
8  rem = re.match("(^.*/)test$", testPath)
9  if not rem:
10    print "This script expects to reside in .../test."
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
26def 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
37class 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
79setupSysPath()
80
81import lldb
82
83class LLDBTestCase(unittest.TestCase):
84  def setUp(self):
85    debugger = lldb.SBDebugger.Create()
86    debugger.SetAsync(True)
87    self.m_commandInterpreter = debugger.GetCommandInterpreter()
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  def getCategories(self):
102    return []
103
104class SanityCheckTestCase(LLDBTestCase):
105  def runTest(self):
106    ret = self.runCommand("show arch", "show-arch")
107    #print ret
108  def getCategories(self):
109    return []
110
111suite = unittest.TestLoader().loadTestsFromTestCase(SanityCheckTestCase)
112unittest.TextTestRunner(verbosity=2).run(suite)
113ExecutionTimes.executionTimes().dumpStats()
114