forked from overviewer/Minecraft-Overviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestRender.py
174 lines (143 loc) · 5.17 KB
/
testRender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
"Test Render Script"
import argparse
import math
import os
import re
import shutil
import sys
import tempfile
import time
from shlex import split
from subprocess import PIPE, STDOUT, CalledProcessError, run
overviewer_scripts = ['./overviewer.py', './gmap.py']
def check_call(args, verbose=False):
try:
return run(
args,
check=True,
stdout=None if verbose else PIPE,
stderr=None if verbose else STDOUT,
universal_newlines=True,
)
except CalledProcessError as e:
if verbose:
print(e.output)
raise e
def check_output(args):
p = run(
args,
check=True,
stdout=PIPE,
universal_newlines=True
)
return p.stdout
def clean_render(overviewerargs, verbose=False):
tempdir = tempfile.mkdtemp('mc-overviewer-test')
overviewer_script = None
for script in overviewer_scripts:
if os.path.exists(script):
overviewer_script = script
break
if overviewer_script is None:
sys.stderr.write("could not find main overviewer script\n")
sys.exit(1)
try:
# check_call raises CalledProcessError when overviewer.py exits badly
check_call([sys.executable] + split("setup.py clean build"), verbose=verbose)
try:
check_call([sys.executable, overviewer_script, '-d'] + overviewerargs, verbose=verbose)
except CalledProcessError:
pass
starttime = time.time()
check_call([sys.executable, overviewer_script] +
overviewerargs + [tempdir, ], verbose=verbose)
endtime = time.time()
return endtime - starttime
finally:
shutil.rmtree(tempdir, True)
def get_stats(timelist):
average = sum(timelist) / float(len(timelist))
meandiff = [(x - average) ** 2 for x in timelist]
sd = math.sqrt(sum(meandiff) / len(meandiff))
return {
"count": len(timelist),
"minimum": min(timelist),
"maximum": max(timelist),
"average": average,
"standard deviation": sd
}
def get_current_branch():
gittext = check_output(split('git rev-parse --abbrev-ref HEAD'))
return gittext.strip() if gittext != "HEAD" else None
def get_current_commit():
gittext = check_output(split('git rev-parse HEAD'))
return gittext.strip() if gittext else None
def get_current_ref():
branch = get_current_branch()
if branch:
return branch
commit = get_current_commit()
if commit:
return commit
def get_commits(gitrange):
gittext = check_output(split('git rev-list --reverse') + [gitrange, ])
return (c for c in gittext.split("\n"))
def set_commit(commit):
check_call(split('git checkout') + [commit, ])
def main(args):
commits = []
for commit in args.commits:
if '..' in commit:
commits = get_commits(commit)
else:
commits.append(commit)
if not commits:
commits = [get_current_ref(), ]
log = None
if args.log:
log = args.log
reset_commit = get_current_ref()
try:
for commit in commits:
print("testing commit", commit)
set_commit(commit)
timelist = []
print(" -- "),
try:
for i in range(args.number):
sys.stdout.write(str(i + 1) + " ")
sys.stdout.flush()
timelist.append(clean_render(args.overviewer_args, verbose=args.verbose))
print("... done")
stats = get_stats(timelist)
print(stats)
if log:
log.write("%s %s\n" % (commit, repr(stats)))
except CalledProcessError as e:
if args.fatal_errors:
print(e)
print("Overviewer croaked, exiting...")
print("(to avoid this, use --keep-going)")
sys.exit(1)
finally:
set_commit(reset_commit)
if log:
log.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("overviewer_args", metavar="[overviewer options/world]", nargs="+")
parser.add_argument("-n", "--option", metavar="N", type=int, action="store",
dest="number", default=3, help="number of renders per commit [default: 3]")
parser.add_argument("-c", "--commits", metavar="RANGE",
action="append", type=str, dest="commits", default=[],
help="the commit (or range of commits) to test [default: current]")
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="don't suppress overviewer output")
parser.add_argument("-k", "--keep-going",
action="store_false", dest="fatal_errors", default=True,
help="don't stop testing when Overviewer croaks")
parser.add_argument("-l", "--log", dest="log", type=argparse.FileType('w'), metavar="FILE",
help="log all test results to a file")
args = parser.parse_args()
main(args)