1"""Test lldb's expression evaluations and collect statistics."""
2
3import os, sys
4import unittest2
5import lldb
6import pexpect
7from lldbbench import *
8
9class ExpressionEvaluationCase(BenchBase):
10
11    mydir = os.path.join("benchmarks", "expression")
12
13    def setUp(self):
14        BenchBase.setUp(self)
15        self.source = 'main.cpp'
16        self.line_to_break = line_number(self.source, '// Set breakpoint here.')
17        self.count = lldb.bmIterationCount
18        if self.count <= 0:
19            self.count = 25
20
21    @benchmarks_test
22    def test_expr_cmd(self):
23        """Test lldb's expression commands and collect statistics."""
24        self.buildDefault()
25        self.exe_name = 'a.out'
26
27        print
28        self.run_lldb_repeated_exprs(self.exe_name, self.count)
29        print "lldb expr cmd benchmark:", self.stopwatch
30
31    def run_lldb_repeated_exprs(self, exe_name, count):
32        exe = os.path.join(os.getcwd(), exe_name)
33
34        # Set self.child_prompt, which is "(lldb) ".
35        self.child_prompt = '(lldb) '
36        prompt = self.child_prompt
37
38        # Reset the stopwatch now.
39        self.stopwatch.reset()
40        for i in range(count):
41            # So that the child gets torn down after the test.
42            self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe))
43            child = self.child
44
45            # Turn on logging for what the child sends back.
46            if self.TraceOn():
47                child.logfile_read = sys.stdout
48
49            child.expect_exact(prompt)
50            child.sendline('breakpoint set -f %s -l %d' % (self.source, self.line_to_break))
51            child.expect_exact(prompt)
52            child.sendline('run')
53            child.expect_exact(prompt)
54            expr_cmd1 = 'expr ptr[j]->point.x'
55            expr_cmd2 = 'expr ptr[j]->point.y'
56
57            with self.stopwatch:
58                child.sendline(expr_cmd1)
59                child.expect_exact(prompt)
60                child.sendline(expr_cmd2)
61                child.expect_exact(prompt)
62
63            child.sendline('quit')
64            try:
65                self.child.expect(pexpect.EOF)
66            except:
67                pass
68
69        self.child = None
70
71
72if __name__ == '__main__':
73    import atexit
74    lldb.SBDebugger.Initialize()
75    atexit.register(lambda: lldb.SBDebugger.Terminate())
76    unittest2.main()
77