-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
221 lines (199 loc) · 6.77 KB
/
test.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import time
import os
import sys
import PIL.Image
import OIL
import StringIO
import random
import math
def time_test(func, arg, timelimit=1.0):
times = []
last_time = start_time = time.time()
while last_time - start_time < timelimit:
random.seed(0)
func(arg)
cur_time = time.time()
times.append(cur_time - last_time)
last_time = cur_time
count = len(times)
mean = sum(times) / count
stddev = (sum([(i - mean)**2 for i in times]) / (count - 1))**0.5
return (count, last_time - start_time, mean, stddev)
image_paths = {
'input' : 'input.png',
'dice' : 'dice.png',
}
images = {}
NUM_COMPOSITES = 200
NUM_QUADS = 90
NUM_RESIZES = 10
def pil_composite(out):
dest = PIL.Image.new("RGBA", (512, 512))
for _ in xrange(NUM_COMPOSITES):
dx = random.randint(0, 511)
dy = random.randint(0, 511)
dest.paste(images['dice'], (dx, dy), images['dice'])
dest.save(out)
def oil_composite(out):
dest = OIL.Image(512, 512)
for _ in xrange(NUM_COMPOSITES):
dx = random.randint(0, 511)
dy = random.randint(0, 511)
dest.composite(images['dice'], 255, dx, dy, 0, 0, 0, 0)
dest.save(out)
def oil_triangles(out):
dest = OIL.Image(512, 512)
matrix = OIL.Matrix().scale(0.8, 0.8, 0.8)
vertices = [
((-1, -1, 0), (0, 0), (255, 255, 255, 255)),
((1, -1, 0), (1, 0), (255, 255, 255, 255)),
((1, 1, 0), (1, 1), (255, 255, 255, 255)),
((-1, 1, 0), (0, 1), (255, 255, 255, 255)),
]
indices = [
0, 1, 2,
0, 2, 3,
]
for i in range(NUM_QUADS):
matrix.rotate(0, 0, 2 * math.pi / NUM_QUADS)
dest.draw_triangles(matrix, images['dice'], vertices, indices)
dest.save(out)
def pil_resize_half(out):
im = images['input']
for _ in range(NUM_RESIZES):
dest = im.resize((im.size[0] / 2, im.size[1] / 2), PIL.Image.ANTIALIAS)
dest.save(out)
def oil_resize_half(out):
src = images['input']
for _ in range(NUM_RESIZES):
dest = OIL.Image(src.size[0] / 2, src.size[1] / 2)
dest.resize_half(src)
dest.save(out)
tests = [
("Load", [
("PIL", None, lambda o: PIL.Image.open(image_paths['input']).load()),
("OIL", "CPU", lambda o: OIL.Image.load(image_paths['input'])),
("OIL", "OPENGL", lambda o: OIL.Image.load(image_paths['input'])),
]),
("Save", [
("PIL", None, lambda o: images['input'].save(o)),
("OIL", "CPU", lambda o: images['input'].save(o)),
("OIL", "OPENGL", lambda o: images['input'].save(o)),
]),
("SaveDither", [
("PIL", None, lambda o: images['input'].convert('RGB').convert('P', palette=PIL.Image.ADAPTIVE, colors=256).save(o)),
("OIL", "CPU", lambda o: images['input'].save(o, indexed=True, palette_size=256)),
]),
("Composite", [
("PIL", None, pil_composite),
("OIL", "CPU", oil_composite),
("OIL", "CPU_SSE", oil_composite),
("OIL", "OPENGL", oil_composite),
]),
("Triangles", [
("OIL", "CPU", oil_triangles),
("OIL", "CPU_SSE", oil_triangles),
("OIL", "OPENGL", oil_triangles),
]),
("Resize Half", [
("PIL", None, pil_resize_half),
("OIL", "CPU", oil_resize_half),
("OIL", "CPU_SSE", oil_resize_half),
("OIL", "OPENGL", oil_resize_half),
]),
]
class Table:
def __init__(self, *columns):
self.file = sys.stdout
self.columns = columns
def column(self, *entries):
for length, text in zip(self.columns, entries):
text = str(text)
if len(text) > length:
self.file.write(text[:length])
else:
self.file.write(text + " " * (length - len(text)))
self.file.write("\n")
def draw_bar(offset, length, bound_low, bound_high, start, end, seq):
file = sys.stdout
file.write(" " * offset)
step = (bound_high - bound_low) / length
last_was_inside = False
middle = (start + end) / 2
for i in xrange(length):
x = i * step + bound_low + (step / 2)
if x < start or x > end:
if x + (step / 2) > middle and x - (step / 2) <= middle:
file.write("O")
else:
file.write(" ")
last_was_inside = False
continue
if not last_was_inside:
file.write("|")
last_was_inside = True
else:
if x + step > end:
file.write("|")
elif x + (step / 2) > middle and x - (step / 2) < middle:
file.write("O")
else:
file.write("-")
file.write("\n")
searches = sys.argv[1:]
for name, testlist in tests:
if searches:
if not any([s.lower() in name.lower() for s in searches]):
continue
print name + ":"
print
table = Table(4, 10, 17, 19, 5, 10, 5)
table.column("", "name", "seconds per call", "standard deviation", "(%)", "file size", "(%)")
table.column("", "----", "----------------", "------------------", "---", "---------", "---")
bars = []
reference_time = None
reference_size = None
for case, backend, func in testlist:
if backend:
try:
backend_id = getattr(OIL, "BACKEND_" + backend)
except AttributeError:
table.column("", backend, "(not enabled)")
continue
OIL.backend_set(backend_id)
for key, path in image_paths.items():
images[key] = OIL.Image.load(path)
case = backend
else:
for key, path in image_paths.items():
images[key] = PIL.Image.open(path)
fname = "./test/" + case
fname += " " + name
fname += ".png"
_, _, percall, stddev = time_test(func, fname)
if reference_time is None:
reference_time = percall
percallper = str(float(percall) / reference_time)
percallper = percallper[:4]
fsize = "--"
fper = ""
if fname:
try:
fsize = os.stat(fname).st_size
if reference_size is None:
reference_size = fsize
fper = float(fsize) / reference_size
except OSError:
pass
table.column("", case, percall, stddev, percallper, fsize, fper)
bars.append((case, percall, stddev))
print
bounds = []
for _, mean, dev in bars:
bounds.append(mean + dev)
bounds.append(mean - dev)
bound_low = min(bounds)
bound_high = max(bounds)
for seq, mean, dev in bars:
draw_bar(table.columns[0], sum(table.columns[1:]), bound_low, bound_high, mean - dev, mean + dev, seq)
print