OILS / test / syscall.py View on Github | oilshell.org

248 lines, 153 significant
1#!/usr/bin/env python2
2"""test/syscall.py
3
4Print a results table.
5
6Input looks like
7
801-dash
901-dash
1001-osh
1101-osh
1201-osh
13...
14"""
15from __future__ import print_function
16
17import collections
18import optparse
19import os
20import re
21import sys
22
23
24def log(msg, *args):
25 if args:
26 msg = msg % args
27 print(msg, file=sys.stderr)
28
29
30def Cell(i):
31 """Visually show number of processes.
32
33 ^ ^^ ^^^ etc.
34 """
35 s = '^' * i
36 return '%6s' % s
37
38
39# lines look like this:
40#
41# 554 01-osh.1234
42# 553 01-osh.1235
43
44WC_LINE = re.compile(
45 r'''
46\s*
47(\d+) # number of lines
48\s+
49(\d{2}) # case ID
50\.
51([a-z-]+) # shell name
52''', re.VERBOSE)
53
54assert WC_LINE.match(' 68 01.osh-cpp.19610')
55
56
57def WriteHeader(f, shells, col=''):
58 f.write("ID\t")
59 for sh in shells:
60 f.write("%6s\t" % sh)
61 f.write('%s\t' % col)
62 f.write('Description')
63 f.write("\n")
64
65
66def WriteProcessReport(f, cases, code_strs, proc_sh, num_procs,
67 procs_by_shell):
68 f.write('Number of Processes Started, by shell and test case\n\n')
69
70 WriteHeader(f, proc_sh, col='osh>min')
71
72 not_minimum = 0
73 more_than_bash = 0
74 fewer_than_bash = 0
75
76 for case_id in sorted(cases):
77 f.write(case_id + "\t")
78 min_procs = 20
79 for sh in proc_sh:
80 n = num_procs[case_id, sh]
81 f.write(Cell(n) + "\t")
82 min_procs = min(n, min_procs)
83
84 osh_count = num_procs[case_id, 'osh']
85 if osh_count != min_procs:
86 f.write('%d>%d\t' % (osh_count, min_procs))
87 not_minimum += 1
88 else:
89 f.write('\t')
90
91 bash_count = num_procs[case_id, 'bash']
92 if osh_count > bash_count:
93 more_than_bash += 1
94 if osh_count < bash_count:
95 fewer_than_bash += 1
96
97 f.write(code_strs[case_id])
98 f.write("\n")
99
100 f.write("TOTAL\t")
101 for sh in proc_sh:
102 f.write('%6d\t' % procs_by_shell[sh])
103 f.write('\n\n')
104 f.write("Cases where ...\n")
105 f.write(" OSH isn't the minimum: %d\n" % not_minimum)
106 f.write(" OSH starts more than bash: %d\n" % more_than_bash)
107 f.write(" OSH starts fewer than bash: %d\n\n" % fewer_than_bash)
108
109 return not_minimum, more_than_bash, fewer_than_bash
110
111
112def WriteSyscallReport(f, cases, code_strs, syscall_sh, num_syscalls,
113 syscalls_by_shell):
114 f.write('Number of Syscalls\n\n')
115
116 WriteHeader(f, syscall_sh)
117
118 for case_id in sorted(cases):
119 f.write(case_id + "\t")
120 #min_procs = 20
121 for sh in syscall_sh:
122 n = num_syscalls[case_id, sh]
123 f.write('%6d\t' % n)
124 #min_procs = min(n, min_procs)
125
126 f.write('\t')
127
128 f.write(code_strs[case_id])
129 f.write("\n")
130
131 f.write("TOTAL\t")
132 for sh in syscall_sh:
133 f.write('%6d\t' % syscalls_by_shell[sh])
134 f.write('\n\n')
135
136
137def Options():
138 """Returns an option parser instance."""
139 p = optparse.OptionParser()
140 p.add_option('--suite',
141 dest='suite',
142 default='SUITE',
143 help='Test suite name')
144 p.add_option(
145 '--not-minimum',
146 dest='not_minimum',
147 type=int,
148 default=0,
149 help=
150 "Expected number of cases where OSH doesn't start the minimum number of"
151 "processes")
152 p.add_option(
153 '--more-than-bash',
154 dest='more_than_bash',
155 type=int,
156 default=0,
157 help=
158 'Expected number of cases where OSH starts more processes than bash')
159 return p
160
161
162def main(argv):
163 o = Options()
164 opts, argv = o.parse_args(argv[1:])
165
166 cases_path = argv[0]
167 out_dir = argv[1]
168
169 code_strs = {}
170 with open(cases_path) as f:
171 for line in f:
172 case_id, code_str = line.split(None, 1) # whitespace
173 code_strs[case_id] = code_str
174
175 cases = set()
176 shells = set()
177
178 num_procs = collections.defaultdict(int)
179 procs_by_shell = collections.defaultdict(int)
180
181 num_syscalls = collections.defaultdict(int)
182 syscalls_by_shell = collections.defaultdict(int)
183
184 #
185 # Summarize Data
186 #
187
188 for line in sys.stdin:
189 m = WC_LINE.match(line)
190 if not m:
191 raise RuntimeError('Invalid line %r' % line)
192 num_sys, case, sh = m.groups()
193 num_sys = int(num_sys)
194
195 cases.add(case)
196 shells.add(sh)
197
198 num_procs[case, sh] += 1
199 num_syscalls[case, sh] += num_sys
200
201 procs_by_shell[sh] += 1
202 syscalls_by_shell[sh] += num_sys
203
204 # Orders columns by how good the results are, then shell name.
205 proc_sh = sorted(procs_by_shell, key=lambda sh: (procs_by_shell[sh], sh))
206 syscall_sh = sorted(syscalls_by_shell,
207 key=lambda sh: (syscalls_by_shell[sh], sh))
208
209 #
210 # Print Tables
211 #
212
213 out_path = os.path.join(out_dir, 'processes.%s.txt' % opts.suite)
214 with open(out_path, 'w') as f:
215 not_minimum, more_than_bash, fewer_than_bash = WriteProcessReport(
216 f, cases, code_strs, proc_sh, num_procs, procs_by_shell)
217 log('Wrote %s', out_path)
218
219 #
220 # Print Table of Syscall Counts
221 #
222
223 out_path = os.path.join(out_dir, 'syscalls.%s.txt' % opts.suite)
224 with open(out_path, 'w') as f:
225 WriteSyscallReport(f, cases, code_strs, syscall_sh, num_syscalls,
226 syscalls_by_shell)
227 log('Wrote %s', out_path)
228
229 ok = True
230 if more_than_bash != opts.more_than_bash:
231 log('Expected %d more than bash, got %d', opts.more_than_bash,
232 more_than_bash)
233 ok = False
234
235 if not_minimum != opts.not_minimum:
236 log('Expected %d that are not minimal, got %d', opts.not_minimum,
237 not_minimum)
238 ok = False
239
240 return 0 if ok else 1
241
242
243if __name__ == '__main__':
244 try:
245 sys.exit(main(sys.argv))
246 except RuntimeError as e:
247 print('FATAL: %s' % e, file=sys.stderr)
248 sys.exit(1)