joshualitt | 810f77f | 2016-03-08 10:56:41 -0800 | [diff] [blame^] | 1 | # Copyright 2016 Google Inc. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import json |
| 8 | import os |
| 9 | import re |
| 10 | import requests |
| 11 | |
| 12 | from os import listdir |
| 13 | from os.path import isfile, join |
| 14 | |
| 15 | ops = [ |
| 16 | "enable_gpu", |
| 17 | "post", |
| 18 | "info", |
| 19 | "cmd", |
| 20 | "img" |
| 21 | ] |
| 22 | |
| 23 | def Check(request): |
| 24 | assert(request.status_code == 200) |
| 25 | return request |
| 26 | |
| 27 | def WriteJson(request, path): |
| 28 | # Writes out pretty printed json |
| 29 | with open(path, 'wb+') as fd: |
| 30 | json.dump(request.json(), fd, sort_keys=True, indent=2, |
| 31 | separators=(',', ': ')) |
| 32 | return request |
| 33 | |
| 34 | def WritePng(request, path): |
| 35 | with open(path, 'wb+') as fd: |
| 36 | fd.write(request.content) |
| 37 | |
| 38 | |
| 39 | # A simple class to drive testing |
| 40 | class SkiaServeTester: |
| 41 | def __init__(self, url, output_dir): |
| 42 | self.url = url |
| 43 | self.output_dir = output_dir |
| 44 | |
| 45 | # skp properties |
| 46 | self.skp = '' |
| 47 | self.skp_name = '' |
| 48 | |
| 49 | def set_skp(self, skp_dir, skp_name): |
| 50 | self.skp = skp_dir + '/' + skp_name |
| 51 | self.skp_name = skp_name |
| 52 | |
| 53 | def info(self): |
| 54 | return Check(requests.get(self.url + '/info')) |
| 55 | |
| 56 | def post(self): |
| 57 | with open(self.skp, 'rb') as payload: |
| 58 | files = {'file': payload} |
| 59 | |
| 60 | # upload skp |
| 61 | return Check(requests.post(self.url + '/new', files=files)) |
| 62 | |
| 63 | def cmd(self): |
| 64 | path = self.output_dir + '/' + self.skp_name + '.cmd.json' |
| 65 | return WriteJson(Check(requests.get(self.url + '/cmd')), path) |
| 66 | |
| 67 | def img(self): |
| 68 | opcount = self.opcount() |
| 69 | url = self.url + '/img/' + str(opcount) |
| 70 | path = self.output_dir + '/' + self.skp_name + '.png' |
| 71 | return WritePng(Check(requests.get(url)), path) |
| 72 | |
| 73 | def enable_gpu(self): |
| 74 | return Check(requests.post(self.url + '/enableGPU/1')) |
| 75 | |
| 76 | def disable_gpu(self): |
| 77 | return Check(requests.post(self.url + '/enableGPU/0')) |
| 78 | |
| 79 | def opcount(self): |
| 80 | r = self.cmd() |
| 81 | return len(r.json()['commands']) - 1 # why the minus 1 here? |
| 82 | |
| 83 | def main(): |
| 84 | parser = argparse.ArgumentParser(description='Tester for SkiaServe') |
| 85 | parser.add_argument('--skp_dir', default='skps', type=str) |
| 86 | parser.add_argument('--url', default='http://localhost:8888', type=str) |
| 87 | parser.add_argument('--output_dir', default='results', type=str) |
| 88 | parser.add_argument('--match', default='.*', type=str) |
| 89 | |
| 90 | args = parser.parse_args() |
| 91 | skp_dir = args.skp_dir |
| 92 | url = args.url |
| 93 | output_dir = args.output_dir |
| 94 | |
| 95 | if not os.path.isdir(output_dir): |
| 96 | os.makedirs(output_dir) |
| 97 | |
| 98 | skps = [] |
| 99 | for skp in listdir(skp_dir): |
| 100 | if isfile(join(skp_dir, skp)) and re.match(args.match, skp): |
| 101 | skps.append(skp) |
| 102 | |
| 103 | tester = SkiaServeTester(url, output_dir) |
| 104 | |
| 105 | for skp_name in skps: |
| 106 | tester.set_skp(skp_dir, skp_name) |
| 107 | for op in ops: |
| 108 | getattr(tester, op)() |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | main() |