blob: 589b403b5918d8a1159f8c08a97d5f713a376e07 [file] [log] [blame]
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +00001#!/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
10import argparse
11import glob
12import os
13import re
14import subprocess
15import sys
16
17# Set the PYTHONPATH to include the tools directory.
18sys.path.append(
19 os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
20import find_run_binary
21
22def 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
41def 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 """
Ben Wagner63fd7602017-10-09 15:45:33 -040048 proc = subprocess.Popen(args, stdout=subprocess.PIPE,
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +000049 stderr=subprocess.STDOUT)
50 output, _ = proc.communicate()
51 errcode = proc.returncode
52 return (errcode, output)
53
54
55class GpuVeto(object):
56
57 def __init__(self):
58 self.bench_pictures = find_run_binary.find_path_to_program(
59 'bench_pictures')
egdaniel12c21982014-06-18 07:34:39 -070060 sys.stdout.write('Running: %s\n' % (self.bench_pictures))
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +000061 self.gpuveto = find_run_binary.find_path_to_program('gpuveto')
62 assert os.path.isfile(self.bench_pictures)
63 assert os.path.isfile(self.gpuveto)
egdaniel12c21982014-06-18 07:34:39 -070064 self.indeterminate = 0
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +000065 self.truePositives = 0
66 self.falsePositives = 0
67 self.trueNegatives = 0
68 self.falseNegatives = 0
69
70 def process_skps(self, dir_or_file):
71 for skp in enumerate(dir_or_file):
72 self.process_skp(skp[1])
73
Ben Wagner63fd7602017-10-09 15:45:33 -040074 sys.stdout.write('TP %d FP %d TN %d FN %d IND %d\n' % (
75 self.truePositives,
76 self.falsePositives,
77 self.trueNegatives,
78 self.falseNegatives,
79 self.indeterminate))
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +000080
81
82 def process_skp(self, skp_file):
83 assert os.path.isfile(skp_file)
egdaniel12c21982014-06-18 07:34:39 -070084 #print skp_file
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +000085
86 # run gpuveto on the skp
87 args = [self.gpuveto, '-r', skp_file]
88 returncode, output = execute_program(args)
89 if (returncode != 0):
90 return
91
92 if ('unsuitable' in output):
93 suitable = False
94 else:
95 assert 'suitable' in output
96 suitable = True
97
98 # run raster config
Ben Wagner63fd7602017-10-09 15:45:33 -040099 args = [self.bench_pictures, '-r', skp_file,
egdaniel12c21982014-06-18 07:34:39 -0700100 '--repeat', '20',
101 '--timers', 'w',
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000102 '--config', '8888']
103 returncode, output = execute_program(args)
104 if (returncode != 0):
105 return
106
egdaniel12c21982014-06-18 07:34:39 -0700107 matches = re.findall('[\d]+\.[\d]+', output)
108 if len(matches) != 1:
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000109 return
110
egdaniel12c21982014-06-18 07:34:39 -0700111 rasterTime = float(matches[0])
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000112
113 # run gpu config
Ben Wagner63fd7602017-10-09 15:45:33 -0400114 args2 = [self.bench_pictures, '-r', skp_file,
egdaniel12c21982014-06-18 07:34:39 -0700115 '--repeat', '20',
116 '--timers', 'w',
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000117 '--config', 'gpu']
118 returncode, output = execute_program(args2)
119 if (returncode != 0):
120 return
121
egdaniel12c21982014-06-18 07:34:39 -0700122 matches = re.findall('[\d]+\.[\d]+', output)
123 if len(matches) != 1:
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000124 return
125
egdaniel12c21982014-06-18 07:34:39 -0700126 gpuTime = float(matches[0])
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000127
egdaniel12c21982014-06-18 07:34:39 -0700128 # happens if page is too big it will not render
129 if 0 == gpuTime:
130 return
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000131
egdaniel12c21982014-06-18 07:34:39 -0700132 tolerance = 0.05
133 tol_range = tolerance * gpuTime
134
135
Ben Wagner63fd7602017-10-09 15:45:33 -0400136 if gpuTime - tol_range < rasterTime < gpuTime + tol_range:
egdaniel12c21982014-06-18 07:34:39 -0700137 result = "NONE"
138 self.indeterminate += 1
139 elif suitable:
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000140 if gpuTime < rasterTime:
141 self.truePositives += 1
egdaniel12c21982014-06-18 07:34:39 -0700142 result = "TP"
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000143 else:
144 self.falsePositives += 1
egdaniel12c21982014-06-18 07:34:39 -0700145 result = "FP"
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000146 else:
147 if gpuTime < rasterTime:
148 self.falseNegatives += 1
egdaniel12c21982014-06-18 07:34:39 -0700149 result = "FN"
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000150 else:
151 self.trueNegatives += 1
egdaniel12c21982014-06-18 07:34:39 -0700152 result = "TN"
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000153
Ben Wagner63fd7602017-10-09 15:45:33 -0400154
155 sys.stdout.write('%s: gpuveto: %d raster %.2f gpu: %.2f Result: %s\n'
156 % (skp_file, suitable, rasterTime, gpuTime, result))
commit-bot@chromium.orgbde3cd82014-04-28 19:41:01 +0000157
158def main(main_argv):
159 parser = argparse.ArgumentParser()
160 parser.add_argument('--skp_path',
161 help='Path to the SKP(s). Can either be a directory ' \
162 'containing SKPs or a single SKP.',
163 required=True)
164
165 args = parser.parse_args()
166 GpuVeto().process_skps(list_files(args.skp_path))
167
168if __name__ == '__main__':
169 sys.exit(main(sys.argv[1]))