1"""Test evaluating expressions repeatedly comparing lldb against gdb."""
2
3import os, sys
4import unittest2
5import lldb
6import pexpect
7from lldbbench import *
8
9class RepeatedExprsCase(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.lldb_avg = None
18        self.gdb_avg = None
19        self.count = lldb.bmIterationCount
20        if self.count <= 0:
21            self.count = 100
22
23    @benchmarks_test
24    def test_compare_lldb_to_gdb(self):
25        """Test repeated expressions with lldb vs. gdb."""
26        self.buildDefault()
27        self.exe_name = 'a.out'
28
29        print
30        self.run_lldb_repeated_exprs(self.exe_name, self.count)
31        print "lldb benchmark:", self.stopwatch
32        self.run_gdb_repeated_exprs(self.exe_name, self.count)
33        print "gdb benchmark:", self.stopwatch
34        print "lldb_avg/gdb_avg: %f" % (self.lldb_avg/self.gdb_avg)
35
36    def run_lldb_repeated_exprs(self, exe_name, count):
37        exe = os.path.join(os.getcwd(), exe_name)
38
39        # Set self.child_prompt, which is "(lldb) ".
40        self.child_prompt = '(lldb) '
41        prompt = self.child_prompt
42
43        # So that the child gets torn down after the test.
44        self.child = pexpect.spawn('%s %s %s' % (self.lldbExec, self.lldbOption, exe))
45        child = self.child
46
47        # Turn on logging for what the child sends back.
48        if self.TraceOn():
49            child.logfile_read = sys.stdout
50
51        child.expect_exact(prompt)
52        child.sendline('breakpoint set -f %s -l %d' % (self.source, self.line_to_break))
53        child.expect_exact(prompt)
54        child.sendline('run')
55        child.expect_exact(prompt)
56        expr_cmd1 = 'expr ptr[j]->point.x'
57        expr_cmd2 = 'expr ptr[j]->point.y'
58
59        # Reset the stopwatch now.
60        self.stopwatch.reset()
61        for i in range(count):
62            with self.stopwatch:
63                child.sendline(expr_cmd1)
64                child.expect_exact(prompt)
65                child.sendline(expr_cmd2)
66                child.expect_exact(prompt)
67            child.sendline('process continue')
68            child.expect_exact(prompt)
69
70        child.sendline('quit')
71        try:
72            self.child.expect(pexpect.EOF)
73        except:
74            pass
75
76        self.lldb_avg = self.stopwatch.avg()
77        if self.TraceOn():
78            print "lldb expression benchmark:", str(self.stopwatch)
79        self.child = None
80
81    def run_gdb_repeated_exprs(self, exe_name, count):
82        exe = os.path.join(os.getcwd(), exe_name)
83
84        # Set self.child_prompt, which is "(gdb) ".
85        self.child_prompt = '(gdb) '
86        prompt = self.child_prompt
87
88        # So that the child gets torn down after the test.
89        self.child = pexpect.spawn('gdb --nx %s' % exe)
90        child = self.child
91
92        # Turn on logging for what the child sends back.
93        if self.TraceOn():
94            child.logfile_read = sys.stdout
95
96        child.expect_exact(prompt)
97        child.sendline('break %s:%d' % (self.source, self.line_to_break))
98        child.expect_exact(prompt)
99        child.sendline('run')
100        child.expect_exact(prompt)
101        expr_cmd1 = 'print ptr[j]->point.x'
102        expr_cmd2 = 'print ptr[j]->point.y'
103
104        # Reset the stopwatch now.
105        self.stopwatch.reset()
106        for i in range(count):
107            with self.stopwatch:
108                child.sendline(expr_cmd1)
109                child.expect_exact(prompt)
110                child.sendline(expr_cmd2)
111                child.expect_exact(prompt)
112            child.sendline('continue')
113            child.expect_exact(prompt)
114
115        child.sendline('quit')
116        child.expect_exact('The program is running.  Exit anyway?')
117        child.sendline('y')
118        try:
119            self.child.expect(pexpect.EOF)
120        except:
121            pass
122
123        self.gdb_avg = self.stopwatch.avg()
124        if self.TraceOn():
125            print "gdb expression benchmark:", str(self.stopwatch)
126        self.child = None
127
128
129if __name__ == '__main__':
130    import atexit
131    lldb.SBDebugger.Initialize()
132    atexit.register(lambda: lldb.SBDebugger.Terminate())
133    unittest2.main()
134