|
3 | 3 | # This script saves result data # |
4 | 4 | # It expects the format of unladen swallow's perf.py # |
5 | 5 | ####################################################### |
6 | | -import urllib, urllib2 |
| 6 | + |
| 7 | +""" |
| 8 | +Upload a json file generated by runner.py. |
| 9 | +
|
| 10 | +Revision, name and host are required. |
| 11 | +
|
| 12 | +Example usage: |
| 13 | +
|
| 14 | + $ ./saveresults.py result.json -r '45757:fabe4fc0dc08' -n pypy-c-jit \ |
| 15 | + -H benchmarker |
| 16 | +
|
| 17 | + OR |
| 18 | +
|
| 19 | + $ ./saveresults.py result.json -r '45757:fabe4fc0dc08' -n pypy-c-jit-64 \ |
| 20 | + -H benchmarker |
| 21 | +""" |
| 22 | +from __future__ import division, print_function |
| 23 | + |
7 | 24 | from datetime import datetime |
| 25 | +import optparse |
| 26 | +import sys |
| 27 | +import time |
| 28 | +try: |
| 29 | + import urllib2 |
| 30 | + from urllib import urlencode |
| 31 | +except ImportError: |
| 32 | + import urllib.request as urllib2 |
| 33 | + from urllib.parse import urlencode |
| 34 | +import json |
| 35 | +import pprint |
8 | 36 |
|
9 | | -#SPEEDURL = 'http://127.0.0.1:8000/' |
10 | | -SPEEDURL = 'http://speed.pypy.org/' |
| 37 | +SPEEDURL = 'http://127.0.0.1:8000/' |
| 38 | +# SPEEDURL = 'https://speed.pypy.org/' |
11 | 39 |
|
12 | | -def save(project, revision, results, options, executable, environment, testing=False): |
| 40 | +def save(project, revision, results, executable, host, url, testing=False, |
| 41 | + changed=True, branch='default'): |
13 | 42 | testparams = [] |
14 | 43 | #Parse data |
15 | 44 | data = {} |
16 | | - current_date = datetime.today() |
| 45 | + error = 0 |
| 46 | + |
| 47 | + print("saveresults::save sending from %s to %s" %(host, url)) |
17 | 48 | for b in results: |
18 | 49 | bench_name = b[0] |
19 | 50 | res_type = b[1] |
20 | 51 | results = b[2] |
21 | 52 | value = 0 |
22 | 53 | if res_type == "SimpleComparisonResult": |
23 | | - value = results['changed_time'] |
| 54 | + if changed: |
| 55 | + value = results['changed_time'] |
| 56 | + else: |
| 57 | + value = results['base_time'] |
24 | 58 | elif res_type == "ComparisonResult": |
25 | | - value = results['avg_changed'] |
| 59 | + if changed: |
| 60 | + value = results['avg_changed'] |
| 61 | + else: |
| 62 | + value = results['avg_base'] |
| 63 | + elif res_type == "RawResult": |
| 64 | + if changed: |
| 65 | + value = results["changed_times"] |
| 66 | + else: |
| 67 | + value = results["base_times"] |
| 68 | + if value: |
| 69 | + assert len(value) == 1 |
| 70 | + value = value[0] |
26 | 71 | else: |
27 | 72 | print("ERROR: result type unknown " + b[1]) |
28 | 73 | return 1 |
29 | | - data = { |
| 74 | + data = [{ |
30 | 75 | 'commitid': revision, |
31 | 76 | 'project': project, |
32 | 77 | 'executable': executable, |
33 | 78 | 'benchmark': bench_name, |
34 | | - 'environment': environment, |
| 79 | + 'environment': host, |
35 | 80 | 'result_value': value, |
36 | | - } |
| 81 | + 'branch': branch, |
| 82 | + }] |
| 83 | + if not value: |
| 84 | + print("Ignoring skipped result", data) |
| 85 | + continue |
37 | 86 | if res_type == "ComparisonResult": |
38 | | - data['std_dev'] = results['std_changed'] |
39 | | - if testing: testparams.append(data) |
40 | | - else: send(data) |
41 | | - if testing: return testparams |
42 | | - else: return 0 |
| 87 | + if changed: |
| 88 | + data[0]['std_dev'] = results['std_changed'] |
| 89 | + else: |
| 90 | + data[0]['std_dev'] = results['std_base'] |
| 91 | + if testing: |
| 92 | + testparams.append(data) |
| 93 | + else: |
| 94 | + error |= send(data, url) |
43 | 95 |
|
44 | | -def send(data): |
| 96 | + if error: |
| 97 | + raise IOError("Saving failed. See messages above.") |
| 98 | + if testing: |
| 99 | + return testparams |
| 100 | + else: |
| 101 | + return 0 |
| 102 | + |
| 103 | + |
| 104 | +def send(data, url): |
45 | 105 | #save results |
46 | | - params = urllib.urlencode(data) |
| 106 | + params = urlencode({'json': json.dumps(data)}).encode("utf-8") |
47 | 107 | f = None |
48 | 108 | response = "None" |
49 | | - info = str(datetime.today()) + ": Saving result for " + data['executable'] |
50 | | - info += " revision " + " " + str(data['commitid']) + ", benchmark " |
51 | | - info += data['benchmark'] |
| 109 | + info = ("%s: Saving result for %s revision %s, benchmark %s" % |
| 110 | + (str(datetime.today()), data[0]['executable'], |
| 111 | + str(data[0]['commitid']), data[0]['benchmark'])) |
52 | 112 | print(info) |
53 | 113 | try: |
54 | | - f = urllib2.urlopen(SPEEDURL + 'result/add/', params) |
55 | | - response = f.read() |
56 | | - f.close() |
| 114 | + retries = [1, 3, 6, 20] |
| 115 | + while True: |
| 116 | + try: |
| 117 | + print('result/add') |
| 118 | + f = urllib2.urlopen(url + 'result/add/json/', params) |
| 119 | + print('urlopen') |
| 120 | + response = f.read().decode() |
| 121 | + print('response') |
| 122 | + f.close() |
| 123 | + break |
| 124 | + except urllib2.URLError: |
| 125 | + if not retries: |
| 126 | + raise |
| 127 | + d = retries.pop(0) |
| 128 | + print("retrying in %d seconds..." % d) |
| 129 | + time.sleep(d) |
57 | 130 | except urllib2.URLError as e: |
58 | 131 | if hasattr(e, 'reason'): |
59 | | - response = '\n We failed to reach a server\n' |
| 132 | + response = '\n We failed to save the data\n' |
60 | 133 | response += ' Reason: ' + str(e.reason) |
61 | 134 | elif hasattr(e, 'code'): |
62 | | - response = '\n The server couldn\'t fulfill the request\n' |
63 | | - response += ' Error code: ' + str(e) |
64 | | - print("Server (%s) response: %s\n" % (SPEEDURL, response)) |
| 135 | + response = '\n The server couldn\'t fulfill the request' |
| 136 | + if hasattr(e, 'readlines'): |
| 137 | + response = "".join([response] + [s.decode() for s in e.readlines()]) |
| 138 | + print(response) |
| 139 | + print('when sending') |
| 140 | + pprint.pprint(data) |
| 141 | + raise |
| 142 | + with open('error.html', 'w') as error_file: |
| 143 | + error_file.write(response) |
| 144 | + print("Server (%s) response written to error.html" % (url,)) |
| 145 | + print(' Error code: %s\n' % (e,)) |
65 | 146 | return 1 |
| 147 | + print("saved correctly!", end='\n\n') |
66 | 148 | return 0 |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + usage = "Usage: %prog [options] <results.json>" |
| 152 | + parser = optparse.OptionParser(usage=usage) |
| 153 | + parser.add_option('-b', '--base', action='store_true', |
| 154 | + help='take base values instead of modified') |
| 155 | + parser.add_option('--host', type=str, default=SPEEDURL, |
| 156 | + help="codespeed instance, defaults to '%s'" % SPEEDURL) |
| 157 | + parser.add_option('--revision', |
| 158 | + help=('for cpython, revision number (100 for cpython 2.6.2, 101 for 2.7.2, ' |
| 159 | + '110 for cpython3.7.6, 120 for cpython3.11, ' |
| 160 | + 'edit admin interface "Revisions" to add more)\n\n' |
| 161 | + 'for pypy, use the hash from the json file'), |
| 162 | + ) |
| 163 | + parser.add_option('--branch', help="branch name (main, py3.11)", default='main') |
| 164 | + options, args = parser.parse_args(sys.argv) |
| 165 | + if options.host[-1] != '/': |
| 166 | + raise ValueError("host must end with '/'") |
| 167 | + if not options.host.startswith("http"): |
| 168 | + raise ValueError("host must start with 'http'") |
| 169 | + if len(args) != 2: |
| 170 | + print(parser.usage) |
| 171 | + sys.exit(1) |
| 172 | + results = json.load(open(args[1]))['results'] |
| 173 | + save('cpython', options, results, 'cpython', 'benchmarker', |
| 174 | + testing=False) |
0 commit comments