1"""Test evaluating expressions repeatedly comparing lldb against gdb.""" 2 3from __future__ import print_function 4 5 6import sys 7import lldb 8from lldbsuite.test.lldbbench import BenchBase 9from lldbsuite.test.decorators import * 10from lldbsuite.test.lldbtest import * 11from lldbsuite.test import configuration 12from lldbsuite.test import lldbutil 13 14 15class RepeatedExprsCase(BenchBase): 16 17 mydir = TestBase.compute_mydir(__file__) 18 19 def setUp(self): 20 BenchBase.setUp(self) 21 self.source = 'main.cpp' 22 self.line_to_break = line_number( 23 self.source, '// Set breakpoint here.') 24 self.lldb_avg = None 25 self.gdb_avg = None 26 self.count = 100 27 28 @benchmarks_test 29 @expectedFailureAll( 30 oslist=["windows"], 31 bugnumber="llvm.org/pr22274: need a pexpect replacement for windows") 32 def test_compare_lldb_to_gdb(self): 33 """Test repeated expressions with lldb vs. gdb.""" 34 self.build() 35 self.exe_name = 'a.out' 36 37 print() 38 self.run_lldb_repeated_exprs(self.exe_name, self.count) 39 print("lldb benchmark:", self.stopwatch) 40 self.run_gdb_repeated_exprs(self.exe_name, self.count) 41 print("gdb benchmark:", self.stopwatch) 42 print("lldb_avg/gdb_avg: %f" % (self.lldb_avg / self.gdb_avg)) 43 44 def run_lldb_repeated_exprs(self, exe_name, count): 45 import pexpect 46 exe = self.getBuildArtifact(exe_name) 47 48 # Set self.child_prompt, which is "(lldb) ". 49 self.child_prompt = '(lldb) ' 50 prompt = self.child_prompt 51 52 # So that the child gets torn down after the test. 53 self.child = pexpect.spawn( 54 '%s %s %s' % 55 (lldbtest_config.lldbExec, self.lldbOption, exe)) 56 child = self.child 57 58 # Turn on logging for what the child sends back. 59 if self.TraceOn(): 60 child.logfile_read = sys.stdout 61 62 child.expect_exact(prompt) 63 child.sendline( 64 'breakpoint set -f %s -l %d' % 65 (self.source, self.line_to_break)) 66 child.expect_exact(prompt) 67 child.sendline('run') 68 child.expect_exact(prompt) 69 expr_cmd1 = 'expr ptr[j]->point.x' 70 expr_cmd2 = 'expr ptr[j]->point.y' 71 72 # Reset the stopwatch now. 73 self.stopwatch.reset() 74 for i in range(count): 75 with self.stopwatch: 76 child.sendline(expr_cmd1) 77 child.expect_exact(prompt) 78 child.sendline(expr_cmd2) 79 child.expect_exact(prompt) 80 child.sendline('process continue') 81 child.expect_exact(prompt) 82 83 child.sendline('quit') 84 try: 85 self.child.expect(pexpect.EOF) 86 except: 87 pass 88 89 self.lldb_avg = self.stopwatch.avg() 90 if self.TraceOn(): 91 print("lldb expression benchmark:", str(self.stopwatch)) 92 self.child = None 93 94 def run_gdb_repeated_exprs(self, exe_name, count): 95 import pexpect 96 exe = self.getBuildArtifact(exe_name) 97 98 # Set self.child_prompt, which is "(gdb) ". 99 self.child_prompt = '(gdb) ' 100 prompt = self.child_prompt 101 102 # So that the child gets torn down after the test. 103 self.child = pexpect.spawn('gdb --nx %s' % exe) 104 child = self.child 105 106 # Turn on logging for what the child sends back. 107 if self.TraceOn(): 108 child.logfile_read = sys.stdout 109 110 child.expect_exact(prompt) 111 child.sendline('break %s:%d' % (self.source, self.line_to_break)) 112 child.expect_exact(prompt) 113 child.sendline('run') 114 child.expect_exact(prompt) 115 expr_cmd1 = 'print ptr[j]->point.x' 116 expr_cmd2 = 'print ptr[j]->point.y' 117 118 # Reset the stopwatch now. 119 self.stopwatch.reset() 120 for i in range(count): 121 with self.stopwatch: 122 child.sendline(expr_cmd1) 123 child.expect_exact(prompt) 124 child.sendline(expr_cmd2) 125 child.expect_exact(prompt) 126 child.sendline('continue') 127 child.expect_exact(prompt) 128 129 child.sendline('quit') 130 child.expect_exact('The program is running. Exit anyway?') 131 child.sendline('y') 132 try: 133 self.child.expect(pexpect.EOF) 134 except: 135 pass 136 137 self.gdb_avg = self.stopwatch.avg() 138 if self.TraceOn(): 139 print("gdb expression benchmark:", str(self.stopwatch)) 140 self.child = None 141