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 def list_files(dir_or_file): |
| 23 """Returns a list of all the files from the provided argument |
| 24 |
| 25 @param dir_or_file: either a directory or skp file |
| 26 |
| 27 @returns a list containing the files in the directory or a single file |
| 28 """ |
| 29 files = [] |
| 30 for globbedpath in glob.iglob(dir_or_file): # useful on win32 |
| 31 if os.path.isdir(globbedpath): |
| 32 for filename in os.listdir(globbedpath): |
| 33 newpath = os.path.join(globbedpath, filename) |
| 34 if os.path.isfile(newpath): |
| 35 files.append(newpath) |
| 36 elif os.path.isfile(globbedpath): |
| 37 files.append(globbedpath) |
| 38 return files |
| 39 |
| 40 |
| 41 def execute_program(args): |
| 42 """Executes a process and waits for it to complete. |
| 43 |
| 44 @param args: is passed into subprocess.Popen(). |
| 45 |
| 46 @returns a tuple of the process output (returncode, output) |
| 47 """ |
| 48 proc = subprocess.Popen(args, stdout=subprocess.PIPE, |
| 49 stderr=subprocess.STDOUT) |
| 50 output, _ = proc.communicate() |
| 51 errcode = proc.returncode |
| 52 return (errcode, output) |
| 53 |
| 54 |
| 55 class GpuVeto(object): |
| 56 |
| 57 def __init__(self): |
| 58 self.bench_pictures = find_run_binary.find_path_to_program( |
| 59 'bench_pictures') |
| 60 self.gpuveto = find_run_binary.find_path_to_program('gpuveto') |
| 61 assert os.path.isfile(self.bench_pictures) |
| 62 assert os.path.isfile(self.gpuveto) |
| 63 self.truePositives = 0 |
| 64 self.falsePositives = 0 |
| 65 self.trueNegatives = 0 |
| 66 self.falseNegatives = 0 |
| 67 |
| 68 def process_skps(self, dir_or_file): |
| 69 for skp in enumerate(dir_or_file): |
| 70 self.process_skp(skp[1]) |
| 71 |
| 72 sys.stdout.write('TP %d FP %d TN %d FN %d\n' % (self.truePositives, |
| 73 self.falsePositives, |
| 74 self.trueNegatives, |
| 75 self.falseNegatives)) |
| 76 |
| 77 |
| 78 def process_skp(self, skp_file): |
| 79 assert os.path.isfile(skp_file) |
| 80 |
| 81 # run gpuveto on the skp |
| 82 args = [self.gpuveto, '-r', skp_file] |
| 83 returncode, output = execute_program(args) |
| 84 if (returncode != 0): |
| 85 return |
| 86 |
| 87 if ('unsuitable' in output): |
| 88 suitable = False |
| 89 else: |
| 90 assert 'suitable' in output |
| 91 suitable = True |
| 92 |
| 93 # run raster config |
| 94 args = [self.bench_pictures, '-r', skp_file, |
| 95 '--repeat', '20', |
| 96 '--config', '8888'] |
| 97 returncode, output = execute_program(args) |
| 98 if (returncode != 0): |
| 99 return |
| 100 |
| 101 matches = re.findall('[\d.]+', output) |
| 102 if len(matches) != 4: |
| 103 return |
| 104 |
| 105 rasterTime = float(matches[3]) |
| 106 |
| 107 # run gpu config |
| 108 args2 = [self.bench_pictures, '-r', skp_file, |
| 109 '--repeat', '20', |
| 110 '--config', 'gpu'] |
| 111 returncode, output = execute_program(args2) |
| 112 if (returncode != 0): |
| 113 return |
| 114 |
| 115 matches = re.findall('[\d.]+', output) |
| 116 if len(matches) != 4: |
| 117 return |
| 118 |
| 119 gpuTime = float(matches[3]) |
| 120 |
| 121 sys.stdout.write('%s: gpuveto: %d raster %.2f gpu: %.2f\n' % ( |
| 122 skp_file, suitable, rasterTime, gpuTime)) |
| 123 |
| 124 if suitable: |
| 125 if gpuTime < rasterTime: |
| 126 self.truePositives += 1 |
| 127 else: |
| 128 self.falsePositives += 1 |
| 129 else: |
| 130 if gpuTime < rasterTime: |
| 131 self.falseNegatives += 1 |
| 132 else: |
| 133 self.trueNegatives += 1 |
| 134 |
| 135 |
| 136 def main(main_argv): |
| 137 parser = argparse.ArgumentParser() |
| 138 parser.add_argument('--skp_path', |
| 139 help='Path to the SKP(s). Can either be a directory ' \ |
| 140 'containing SKPs or a single SKP.', |
| 141 required=True) |
| 142 |
| 143 args = parser.parse_args() |
| 144 GpuVeto().process_skps(list_files(args.skp_path)) |
| 145 |
| 146 if __name__ == '__main__': |
| 147 sys.exit(main(sys.argv[1])) |
OLD | NEW |