|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright 2014 Google Inc. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license that can be | |
6 # found in the LICENSE file. | |
7 | |
8 """Script to test out suitableForGpuRasterization (via gpuveto)""" | |
9 | |
10 import argparse | |
11 import glob | |
12 import os | |
13 import re | |
14 import subprocess | |
15 import sys | |
16 | |
17 # Set the PYTHONPATH to include the tools directory. | |
18 sys.path.append( | |
19 os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) | |
20 import find_run_binary | |
21 | |
22 USAGE = """ | |
rmistry
2014/04/28 18:12:04
No longer needed.
robertphillips
2014/04/28 18:21:41
Done.
| |
23 Usage: | |
24 {command} SKP_FILE | |
25 {command} SKP_DIR\n | |
26 """ | |
27 | |
28 | |
29 def find_path_to_program(program): | |
rmistry
2014/04/28 18:12:04
Lets delete find_path_to_program and directly use
robertphillips
2014/04/28 18:21:41
Done.
| |
30 """Returns path to an existing program binary. | |
31 | |
32 @param program: Basename of the program to find (e.g., 'render_pictures'). | |
33 | |
34 @returns absolute path to the program binary, as a string. | |
35 | |
36 Raises: | |
37 Exception: unable to find the program binary. | |
38 """ | |
39 return find_run_binary.find_path_to_program(program) | |
40 | |
41 | |
42 def list_files(dir_or_file): | |
43 """Returns a list of all the files from the provided argument | |
44 | |
45 @param dir_or_file: either a directory or skp file | |
46 | |
47 @returns a list containing the files in the directory or a single file | |
48 """ | |
49 files = [] | |
50 for globbedpath in glob.iglob(dir_or_file): # useful on win32 | |
51 if os.path.isdir(globbedpath): | |
52 for filename in os.listdir(globbedpath): | |
53 newpath = os.path.join(globbedpath, filename) | |
54 if os.path.isfile(newpath): | |
55 files.append(newpath) | |
56 elif os.path.isfile(globbedpath): | |
57 files.append(globbedpath) | |
58 return files | |
59 | |
60 | |
61 def execute_program(args): | |
62 """Executes a process and waits for it to complete. | |
63 | |
64 @param args: is passed into subprocess.Popen(). | |
65 | |
66 @returns a tuple of the process output (returncode, output) | |
67 """ | |
68 proc = subprocess.Popen(args, stdout=subprocess.PIPE, | |
69 stderr=subprocess.STDOUT) | |
70 output, ignore = proc.communicate() | |
rmistry
2014/04/28 18:12:04
Use _ instead of ignore.
robertphillips
2014/04/28 18:21:41
Done.
| |
71 errcode = proc.returncode | |
72 return (errcode, output) | |
73 | |
74 | |
75 class TestObj(object): | |
rmistry
2014/04/28 18:12:04
Rename this to maybe GpuVeto ?
robertphillips
2014/04/28 18:21:41
Done.
| |
76 | |
77 def __init__(self): | |
78 self.bench_pictures = find_path_to_program('bench_pictures') | |
79 self.gpuveto = find_path_to_program('gpuveto') | |
80 assert os.path.isfile(self.bench_pictures) | |
81 assert os.path.isfile(self.gpuveto) | |
82 self.truePositives = 0 | |
83 self.falsePositives = 0 | |
84 self.trueNegatives = 0 | |
85 self.falseNegatives = 0 | |
86 | |
87 def process_skps(self, dir_or_file): | |
88 for skp in enumerate(dir_or_file): | |
89 self.process_skp(skp[1]) | |
90 | |
91 sys.stdout.write('TP %d FP %d TN %d FN %d\n' % (self.truePositives, | |
92 self.falsePositives, | |
93 self.trueNegatives, | |
94 self.falseNegatives)) | |
95 | |
96 | |
97 def process_skp(self, skp_file): | |
98 assert os.path.isfile(skp_file) | |
99 | |
100 # run gpuveto on the skp | |
101 args = [self.gpuveto, '-r', skp_file] | |
102 returncode, output = execute_program(args) | |
103 if (returncode != 0): | |
104 return | |
105 | |
106 if ('unsuitable' in output): | |
107 suitable = False | |
108 else: | |
109 assert 'suitable' in output | |
110 suitable = True | |
111 | |
112 # run raster config | |
113 args = [self.bench_pictures, '-r', skp_file, | |
114 '--repeat', '20', | |
115 '--config', '8888'] | |
116 returncode, output = execute_program(args) | |
117 if (returncode != 0): | |
118 return | |
119 | |
120 matches = re.findall('[\d.]+', output) | |
121 if len(matches) != 4: | |
122 return | |
123 | |
124 rasterTime = float(matches[3]) | |
125 | |
126 # run gpu config | |
127 args2 = [self.bench_pictures, '-r', skp_file, | |
128 '--repeat', '20', | |
129 '--config', 'gpu'] | |
130 returncode, output = execute_program(args2) | |
131 if (returncode != 0): | |
132 return | |
133 | |
134 matches = re.findall('[\d.]+', output) | |
135 if len(matches) != 4: | |
136 return | |
137 | |
138 gpuTime = float(matches[3]) | |
139 | |
140 sys.stdout.write("%s: gpuveto: %d raster %.2f gpu: %.2f\n" % ( | |
rmistry
2014/04/28 18:12:04
Use single-quotes instead of double-quotes (to be
robertphillips
2014/04/28 18:21:41
Done.
| |
141 skp_file, suitable, rasterTime, gpuTime)) | |
142 | |
143 if suitable: | |
144 if gpuTime < rasterTime: | |
145 self.truePositives += 1 | |
146 else: | |
147 self.falsePositives += 1 | |
148 else: | |
149 if gpuTime < rasterTime: | |
150 self.falseNegatives += 1 | |
151 else: | |
152 self.trueNegatives += 1 | |
153 | |
rmistry
2014/04/28 18:12:04
Add one more newline here.
robertphillips
2014/04/28 18:21:41
Done.
| |
154 def main(main_argv): | |
155 parser = argparse.ArgumentParser() | |
156 parser.add_argument('--skp_path', | |
157 help='Path to the SKP(s). Can either be a directory ' \ | |
158 'containing SKPs or a single SKP.', | |
159 required=True) | |
160 | |
161 args = parser.parse_args() | |
162 TestObj().process_skps(list_files(args.skp_path)) | |
163 | |
164 if __name__ == '__main__': | |
165 sys.exit(main(sys.argv[1])) | |
OLD | NEW |