José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ########################################################################## |
| 3 | # |
| 4 | # Copyright 2011 Jose Fonseca |
| 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the 'Software'), to deal |
| 9 | # in the Software without restriction, including without limitation the rights |
| 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | # copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | # THE SOFTWARE. |
| 24 | # |
| 25 | ##########################################################################/ |
| 26 | |
| 27 | '''Run two retrace instances in parallel, comparing generated snapshots. |
| 28 | ''' |
| 29 | |
| 30 | |
| 31 | import optparse |
| 32 | import os.path |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 33 | import subprocess |
| 34 | import platform |
| 35 | import sys |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 36 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 37 | from PIL import Image |
| 38 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 39 | from snapdiff import Comparer |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 40 | from highlight import Highlighter |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 41 | import jsondiff |
| 42 | |
| 43 | |
| 44 | # Null file, to use when we're not interested in subprocesses output |
| 45 | if platform.system() == 'Windows': |
| 46 | NULL = open('NUL:', 'wt') |
| 47 | else: |
| 48 | NULL = open('/dev/null', 'wt') |
| 49 | |
| 50 | |
| 51 | class Setup: |
| 52 | |
| 53 | def __init__(self, args, env=None): |
| 54 | self.args = args |
| 55 | self.env = env |
| 56 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 57 | def retrace(self): |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 58 | cmd = [ |
| 59 | options.retrace, |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 60 | '-s', '-', |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 61 | '-S', options.snapshot_frequency, |
| 62 | ] + self.args |
| 63 | p = subprocess.Popen(cmd, env=self.env, stdout=subprocess.PIPE, stderr=NULL) |
| 64 | return p |
| 65 | |
| 66 | def dump_state(self, call_no): |
| 67 | '''Get the state dump at the specified call no.''' |
| 68 | |
| 69 | cmd = [ |
| 70 | options.retrace, |
| 71 | '-D', str(call_no), |
| 72 | ] + self.args |
| 73 | p = subprocess.Popen(cmd, env=self.env, stdout=subprocess.PIPE, stderr=NULL) |
| 74 | state = jsondiff.load(p.stdout) |
| 75 | p.wait() |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 76 | return state.get('parameters', {}) |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 77 | |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 78 | def diff_state(self, ref_call_no, src_call_no): |
| 79 | '''Compare the state between two calls.''' |
| 80 | |
| 81 | ref_state = self.dump_state(ref_call_no) |
| 82 | src_state = self.dump_state(src_call_no) |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 83 | |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 84 | sys.stdout.flush() |
| 85 | differ = jsondiff.Differ(sys.stdout) |
| 86 | differ.visit(ref_state, src_state) |
| 87 | sys.stdout.write('\n') |
| 88 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 89 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 90 | def read_pnm(stream): |
| 91 | '''Read a PNM from the stream, and return the image object, and the comment.''' |
| 92 | |
| 93 | magic = stream.readline() |
| 94 | if not magic: |
| 95 | return None, None |
| 96 | assert magic.rstrip() == 'P6' |
| 97 | comment = '' |
| 98 | line = stream.readline() |
| 99 | while line.startswith('#'): |
| 100 | comment += line[1:] |
| 101 | line = stream.readline() |
| 102 | width, height = map(int, line.strip().split()) |
| 103 | maximum = int(stream.readline().strip()) |
| 104 | assert maximum == 255 |
| 105 | data = stream.read(height * width * 3) |
| 106 | image = Image.frombuffer('RGB', (width, height), data, 'raw', 'RGB', 0, 1) |
| 107 | return image, comment |
| 108 | |
| 109 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 110 | def parse_env(optparser, entries): |
| 111 | '''Translate a list of NAME=VALUE entries into an environment dictionary.''' |
| 112 | |
| 113 | env = os.environ.copy() |
| 114 | for entry in entries: |
| 115 | try: |
| 116 | name, var = entry.split('=', 1) |
| 117 | except Exception: |
| 118 | optparser.error('invalid environment entry %r' % entry) |
| 119 | env[name] = var |
| 120 | return env |
| 121 | |
| 122 | |
| 123 | def main(): |
| 124 | '''Main program. |
| 125 | ''' |
| 126 | |
| 127 | global options |
| 128 | |
| 129 | # Parse command line options |
| 130 | optparser = optparse.OptionParser( |
| 131 | usage='\n\t%prog [options] -- [glretrace options] <trace>', |
| 132 | version='%%prog') |
| 133 | optparser.add_option( |
| 134 | '-r', '--retrace', metavar='PROGRAM', |
| 135 | type='string', dest='retrace', default='glretrace', |
| 136 | help='retrace command [default: %default]') |
| 137 | optparser.add_option( |
| 138 | '--ref-env', metavar='NAME=VALUE', |
| 139 | type='string', action='append', dest='ref_env', default=[], |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 140 | help='add variable to reference environment') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 141 | optparser.add_option( |
| 142 | '--src-env', metavar='NAME=VALUE', |
| 143 | type='string', action='append', dest='src_env', default=[], |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 144 | help='add variable to source environment') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 145 | optparser.add_option( |
| 146 | '--diff-prefix', metavar='PATH', |
| 147 | type='string', dest='diff_prefix', default='.', |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 148 | help='prefix for the difference images') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 149 | optparser.add_option( |
| 150 | '-t', '--threshold', metavar='BITS', |
| 151 | type="float", dest="threshold", default=12.0, |
| 152 | help="threshold precision [default: %default]") |
| 153 | optparser.add_option( |
| 154 | '-S', '--snapshot-frequency', metavar='FREQUENCY', |
| 155 | type="string", dest="snapshot_frequency", default='draw', |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 156 | help="snapshot frequency: frame, framebuffer, or draw [default: %default]") |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 157 | |
| 158 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 159 | ref_env = parse_env(optparser, options.ref_env) |
| 160 | src_env = parse_env(optparser, options.src_env) |
| 161 | if not args: |
| 162 | optparser.error("incorrect number of arguments") |
| 163 | |
| 164 | ref_setup = Setup(args, ref_env) |
| 165 | src_setup = Setup(args, src_env) |
| 166 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 167 | highligher = Highlighter(sys.stdout) |
| 168 | |
| 169 | highligher.write('call\tprecision\n') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 170 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 171 | last_bad = -1 |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 172 | last_good = 0 |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 173 | ref_proc = ref_setup.retrace() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 174 | try: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 175 | src_proc = src_setup.retrace() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 176 | try: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 177 | while True: |
| 178 | # Get the reference image |
| 179 | ref_image, ref_comment = read_pnm(ref_proc.stdout) |
| 180 | if ref_image is None: |
| 181 | break |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 182 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 183 | # Get the source image |
| 184 | src_image, src_comment = read_pnm(src_proc.stdout) |
| 185 | if src_image is None: |
| 186 | break |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 187 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 188 | assert ref_comment == src_comment |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 189 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 190 | call_no = int(ref_comment.strip()) |
| 191 | |
| 192 | # Compare the two images |
| 193 | comparer = Comparer(ref_image, src_image) |
| 194 | precision = comparer.precision() |
| 195 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 196 | mismatch = precision < options.threshold |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 197 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 198 | if mismatch: |
| 199 | highligher.color(highligher.red) |
| 200 | highligher.bold() |
| 201 | highligher.write('%u\t%f\n' % (call_no, precision)) |
| 202 | if mismatch: |
| 203 | highligher.normal() |
| 204 | |
| 205 | if mismatch: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 206 | if options.diff_prefix: |
| 207 | prefix = os.path.join(options.diff_prefix, '%010u' % call_no) |
| 208 | prefix_dir = os.path.dirname(prefix) |
| 209 | if not os.path.isdir(prefix_dir): |
| 210 | os.makedirs(prefix_dir) |
| 211 | ref_image.save(prefix + '.ref.png') |
| 212 | src_image.save(prefix + '.src.png') |
| 213 | comparer.write_diff(prefix + '.diff.png') |
| 214 | if last_bad < last_good: |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 215 | src_setup.diff_state(last_good, call_no) |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 216 | last_bad = call_no |
| 217 | else: |
| 218 | last_good = call_no |
| 219 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame^] | 220 | highligher.flush() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 221 | finally: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 222 | src_proc.terminate() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 223 | finally: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 224 | ref_proc.terminate() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 225 | |
| 226 | |
| 227 | if __name__ == '__main__': |
| 228 | main() |