2014-04-28 19:41:01 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright 2014 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""Script to test out suitableForGpuRasterization (via gpuveto)"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# Set the PYTHONPATH to include the tools directory.
|
|
|
|
sys.path.append(
|
|
|
|
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
|
|
|
|
import find_run_binary
|
|
|
|
|
|
|
|
def list_files(dir_or_file):
|
|
|
|
"""Returns a list of all the files from the provided argument
|
|
|
|
|
|
|
|
@param dir_or_file: either a directory or skp file
|
|
|
|
|
|
|
|
@returns a list containing the files in the directory or a single file
|
|
|
|
"""
|
|
|
|
files = []
|
|
|
|
for globbedpath in glob.iglob(dir_or_file): # useful on win32
|
|
|
|
if os.path.isdir(globbedpath):
|
|
|
|
for filename in os.listdir(globbedpath):
|
|
|
|
newpath = os.path.join(globbedpath, filename)
|
|
|
|
if os.path.isfile(newpath):
|
|
|
|
files.append(newpath)
|
|
|
|
elif os.path.isfile(globbedpath):
|
|
|
|
files.append(globbedpath)
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
|
|
def execute_program(args):
|
|
|
|
"""Executes a process and waits for it to complete.
|
|
|
|
|
|
|
|
@param args: is passed into subprocess.Popen().
|
|
|
|
|
|
|
|
@returns a tuple of the process output (returncode, output)
|
|
|
|
"""
|
2017-10-09 19:45:33 +00:00
|
|
|
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
|
2014-04-28 19:41:01 +00:00
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
output, _ = proc.communicate()
|
|
|
|
errcode = proc.returncode
|
|
|
|
return (errcode, output)
|
|
|
|
|
|
|
|
|
|
|
|
class GpuVeto(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.bench_pictures = find_run_binary.find_path_to_program(
|
|
|
|
'bench_pictures')
|
2014-06-18 14:34:39 +00:00
|
|
|
sys.stdout.write('Running: %s\n' % (self.bench_pictures))
|
2014-04-28 19:41:01 +00:00
|
|
|
self.gpuveto = find_run_binary.find_path_to_program('gpuveto')
|
|
|
|
assert os.path.isfile(self.bench_pictures)
|
|
|
|
assert os.path.isfile(self.gpuveto)
|
2014-06-18 14:34:39 +00:00
|
|
|
self.indeterminate = 0
|
2014-04-28 19:41:01 +00:00
|
|
|
self.truePositives = 0
|
|
|
|
self.falsePositives = 0
|
|
|
|
self.trueNegatives = 0
|
|
|
|
self.falseNegatives = 0
|
|
|
|
|
|
|
|
def process_skps(self, dir_or_file):
|
|
|
|
for skp in enumerate(dir_or_file):
|
|
|
|
self.process_skp(skp[1])
|
|
|
|
|
2017-10-09 19:45:33 +00:00
|
|
|
sys.stdout.write('TP %d FP %d TN %d FN %d IND %d\n' % (
|
|
|
|
self.truePositives,
|
|
|
|
self.falsePositives,
|
|
|
|
self.trueNegatives,
|
|
|
|
self.falseNegatives,
|
|
|
|
self.indeterminate))
|
2014-04-28 19:41:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process_skp(self, skp_file):
|
|
|
|
assert os.path.isfile(skp_file)
|
2014-06-18 14:34:39 +00:00
|
|
|
#print skp_file
|
2014-04-28 19:41:01 +00:00
|
|
|
|
|
|
|
# run gpuveto on the skp
|
|
|
|
args = [self.gpuveto, '-r', skp_file]
|
|
|
|
returncode, output = execute_program(args)
|
|
|
|
if (returncode != 0):
|
|
|
|
return
|
|
|
|
|
|
|
|
if ('unsuitable' in output):
|
|
|
|
suitable = False
|
|
|
|
else:
|
|
|
|
assert 'suitable' in output
|
|
|
|
suitable = True
|
|
|
|
|
|
|
|
# run raster config
|
2017-10-09 19:45:33 +00:00
|
|
|
args = [self.bench_pictures, '-r', skp_file,
|
2014-06-18 14:34:39 +00:00
|
|
|
'--repeat', '20',
|
|
|
|
'--timers', 'w',
|
2014-04-28 19:41:01 +00:00
|
|
|
'--config', '8888']
|
|
|
|
returncode, output = execute_program(args)
|
|
|
|
if (returncode != 0):
|
|
|
|
return
|
|
|
|
|
2014-06-18 14:34:39 +00:00
|
|
|
matches = re.findall('[\d]+\.[\d]+', output)
|
|
|
|
if len(matches) != 1:
|
2014-04-28 19:41:01 +00:00
|
|
|
return
|
|
|
|
|
2014-06-18 14:34:39 +00:00
|
|
|
rasterTime = float(matches[0])
|
2014-04-28 19:41:01 +00:00
|
|
|
|
|
|
|
# run gpu config
|
2017-10-09 19:45:33 +00:00
|
|
|
args2 = [self.bench_pictures, '-r', skp_file,
|
2014-06-18 14:34:39 +00:00
|
|
|
'--repeat', '20',
|
|
|
|
'--timers', 'w',
|
2014-04-28 19:41:01 +00:00
|
|
|
'--config', 'gpu']
|
|
|
|
returncode, output = execute_program(args2)
|
|
|
|
if (returncode != 0):
|
|
|
|
return
|
|
|
|
|
2014-06-18 14:34:39 +00:00
|
|
|
matches = re.findall('[\d]+\.[\d]+', output)
|
|
|
|
if len(matches) != 1:
|
2014-04-28 19:41:01 +00:00
|
|
|
return
|
|
|
|
|
2014-06-18 14:34:39 +00:00
|
|
|
gpuTime = float(matches[0])
|
|
|
|
|
|
|
|
# happens if page is too big it will not render
|
|
|
|
if 0 == gpuTime:
|
|
|
|
return
|
|
|
|
|
|
|
|
tolerance = 0.05
|
|
|
|
tol_range = tolerance * gpuTime
|
2014-04-28 19:41:01 +00:00
|
|
|
|
|
|
|
|
2017-10-09 19:45:33 +00:00
|
|
|
if gpuTime - tol_range < rasterTime < gpuTime + tol_range:
|
2014-06-18 14:34:39 +00:00
|
|
|
result = "NONE"
|
|
|
|
self.indeterminate += 1
|
|
|
|
elif suitable:
|
2014-04-28 19:41:01 +00:00
|
|
|
if gpuTime < rasterTime:
|
|
|
|
self.truePositives += 1
|
2014-06-18 14:34:39 +00:00
|
|
|
result = "TP"
|
2014-04-28 19:41:01 +00:00
|
|
|
else:
|
|
|
|
self.falsePositives += 1
|
2014-06-18 14:34:39 +00:00
|
|
|
result = "FP"
|
2014-04-28 19:41:01 +00:00
|
|
|
else:
|
|
|
|
if gpuTime < rasterTime:
|
|
|
|
self.falseNegatives += 1
|
2014-06-18 14:34:39 +00:00
|
|
|
result = "FN"
|
2014-04-28 19:41:01 +00:00
|
|
|
else:
|
|
|
|
self.trueNegatives += 1
|
2014-06-18 14:34:39 +00:00
|
|
|
result = "TN"
|
2014-04-28 19:41:01 +00:00
|
|
|
|
2017-10-09 19:45:33 +00:00
|
|
|
|
|
|
|
sys.stdout.write('%s: gpuveto: %d raster %.2f gpu: %.2f Result: %s\n'
|
|
|
|
% (skp_file, suitable, rasterTime, gpuTime, result))
|
2014-04-28 19:41:01 +00:00
|
|
|
|
|
|
|
def main(main_argv):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--skp_path',
|
|
|
|
help='Path to the SKP(s). Can either be a directory ' \
|
|
|
|
'containing SKPs or a single SKP.',
|
|
|
|
required=True)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
GpuVeto().process_skps(list_files(args.skp_path))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv[1]))
|