| 1 |
|
| 2 |
|
| 3 |
u""" |
| 4 |
========================= |
| 5 |
Write benchmark results |
| 6 |
========================= |
| 7 |
|
| 8 |
Write benchmark results. |
| 9 |
|
| 10 |
:Copyright: |
| 11 |
|
| 12 |
Copyright 2014 - 2025 |
| 13 |
Andr\xe9 Malo or his licensors, as applicable |
| 14 |
|
| 15 |
:License: |
| 16 |
|
| 17 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 |
you may not use this file except in compliance with the License. |
| 19 |
You may obtain a copy of the License at |
| 20 |
|
| 21 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 22 |
|
| 23 |
Unless required by applicable law or agreed to in writing, software |
| 24 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 25 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 |
See the License for the specific language governing permissions and |
| 27 |
limitations under the License. |
| 28 |
|
| 29 |
Usage:: |
| 30 |
|
| 31 |
python -mbench.write [-p plain] [-t table] <pickled |
| 32 |
|
| 33 |
-p plain Plain file to write to (like docs/BENCHMARKS). |
| 34 |
-t table Table file to write to (like docs/_userdoc/benchmark.txt). |
| 35 |
|
| 36 |
""" |
| 37 |
__author__ = u"Andr\xe9 Malo" |
| 38 |
__version__ = "1.0.0" |
| 39 |
|
| 40 |
import os as _os |
| 41 |
import re as _re |
| 42 |
import sys as _sys |
| 43 |
|
| 44 |
|
| 45 |
try: |
| 46 |
unicode |
| 47 |
except NameError: |
| 48 |
|
| 49 |
def uni(v): |
| 50 |
if hasattr(v, "decode"): |
| 51 |
return v.decode("latin-1") |
| 52 |
return str(v) |
| 53 |
|
| 54 |
else: |
| 55 |
|
| 56 |
def uni(v): |
| 57 |
if isinstance(v, unicode): |
| 58 |
return v.encode("utf-8") |
| 59 |
return str(v) |
| 60 |
|
| 61 |
|
| 62 |
try: |
| 63 |
cmp |
| 64 |
except NameError: |
| 65 |
cmp = lambda a, b: (a > b) - (a < b) |
| 66 |
|
| 67 |
|
| 68 |
def write_table(filename, results): |
| 69 |
""" |
| 70 |
Output tabled benchmark results |
| 71 |
|
| 72 |
:Parameters: |
| 73 |
`filename` : ``str`` |
| 74 |
Filename to write to |
| 75 |
|
| 76 |
`results` : ``list`` |
| 77 |
Results |
| 78 |
""" |
| 79 |
try: |
| 80 |
next |
| 81 |
except NameError: |
| 82 |
next = lambda i: (getattr(i, "next", None) or i.__next__)() |
| 83 |
|
| 84 |
names = [ |
| 85 |
("simple_port", "Simple Port"), |
| 86 |
("jsmin_2_0_9", "jsmin 2.0.9"), |
| 87 |
("rjsmin", "|rjsmin|"), |
| 88 |
("_rjsmin", r"_\ |rjsmin|"), |
| 89 |
] |
| 90 |
benched_per_table = 2 |
| 91 |
|
| 92 |
results = sorted( |
| 93 |
results, |
| 94 |
key=(lambda x: [int(a) for a in x[0].split(".")]), |
| 95 |
reverse=True, |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
pythons, widths = [], [0] * (benched_per_table + 1) |
| 100 |
versions = [] |
| 101 |
for version, _, result in results: |
| 102 |
version = uni(version) |
| 103 |
if ( |
| 104 |
versions |
| 105 |
and versions[-1].startswith("3.10.") |
| 106 |
and not version.startswith("2.") |
| 107 |
): |
| 108 |
continue |
| 109 |
versions.append(version) |
| 110 |
|
| 111 |
namesub = _re.compile(r"(?:-\d+(?:\.\d+)*)?\.js$").sub |
| 112 |
result = iter(result) |
| 113 |
tables = [] |
| 114 |
|
| 115 |
|
| 116 |
for benched in result: |
| 117 |
rows = [["Name"] + [desc for _, desc in names]] |
| 118 |
for _ in range(benched_per_table): |
| 119 |
if _: |
| 120 |
try: |
| 121 |
benched = next(result) |
| 122 |
except StopIteration: |
| 123 |
rows.append([""] + ["" for _ in names]) |
| 124 |
continue |
| 125 |
|
| 126 |
times = dict( |
| 127 |
(uni(port), (time, benched["sizes"][idx])) |
| 128 |
for idx, (port, time) in enumerate(benched["times"]) |
| 129 |
) |
| 130 |
columns = [ |
| 131 |
"%s (%.1f)" |
| 132 |
% ( |
| 133 |
namesub( |
| 134 |
"", _os.path.basename(uni(benched["filename"])) |
| 135 |
), |
| 136 |
benched["size"] / 1024.0, |
| 137 |
) |
| 138 |
] |
| 139 |
for idx, (port, _) in enumerate(names): |
| 140 |
if port not in times: |
| 141 |
columns.append("n/a") |
| 142 |
continue |
| 143 |
time, size = times[port] |
| 144 |
if time is None: |
| 145 |
columns.append("(failed)") |
| 146 |
continue |
| 147 |
columns.append( |
| 148 |
"%s%.2f ms (%.1f %s)" |
| 149 |
% ( |
| 150 |
idx == 0 and " " or "", |
| 151 |
time, |
| 152 |
size / 1024.0, |
| 153 |
idx == 0 |
| 154 |
and "\\*" |
| 155 |
or ["=", ">", "<"][ |
| 156 |
cmp(size, benched["sizes"][0]) |
| 157 |
], |
| 158 |
) |
| 159 |
) |
| 160 |
rows.append(columns) |
| 161 |
|
| 162 |
|
| 163 |
for idx, row in enumerate(rows): |
| 164 |
widths[idx] = max(widths[idx], max(map(len, row))) |
| 165 |
|
| 166 |
|
| 167 |
tables.append(zip(*rows)) |
| 168 |
pythons.append((version, tables)) |
| 169 |
|
| 170 |
if versions[-1].startswith("2."): |
| 171 |
break |
| 172 |
|
| 173 |
|
| 174 |
lines = [] |
| 175 |
separator = lambda c="-": "+".join( |
| 176 |
[""] + [c * (width + 2) for width in widths] + [""] |
| 177 |
) |
| 178 |
|
| 179 |
for idx, (version, tables) in enumerate(pythons): |
| 180 |
if idx: |
| 181 |
lines.append("") |
| 182 |
lines.append("") |
| 183 |
|
| 184 |
line = "Python %s" % (version,) |
| 185 |
lines.append(line) |
| 186 |
lines.append("~" * len(line)) |
| 187 |
|
| 188 |
for table in tables: |
| 189 |
lines.append("") |
| 190 |
lines.append(".. rst-class:: benchmark") |
| 191 |
lines.append("") |
| 192 |
|
| 193 |
for idx, row in enumerate(table): |
| 194 |
if idx == 0: |
| 195 |
|
| 196 |
lines.append(separator()) |
| 197 |
lines.append( |
| 198 |
"|".join( |
| 199 |
[""] |
| 200 |
+ [ |
| 201 |
" %s%*s " % (col, len(col) - width, "") |
| 202 |
for width, col in zip(widths, row) |
| 203 |
] |
| 204 |
+ [""] |
| 205 |
) |
| 206 |
) |
| 207 |
lines.append(separator("=")) |
| 208 |
else: |
| 209 |
lines.append( |
| 210 |
"|".join( |
| 211 |
[""] |
| 212 |
+ [ |
| 213 |
j == 0 |
| 214 |
and ( |
| 215 |
" %s%*s " |
| 216 |
% (col, len(col) - widths[j], "") |
| 217 |
) |
| 218 |
or ( |
| 219 |
["%*s ", " %*s "][idx == 1] |
| 220 |
% (widths[j], col) |
| 221 |
) |
| 222 |
for j, col in enumerate(row) |
| 223 |
] |
| 224 |
+ [""] |
| 225 |
) |
| 226 |
) |
| 227 |
lines.append(separator()) |
| 228 |
|
| 229 |
fplines = [] |
| 230 |
fp = open(filename) |
| 231 |
try: |
| 232 |
fpiter = iter(fp) |
| 233 |
for line in fpiter: |
| 234 |
line = line.rstrip() |
| 235 |
if line == ".. begin tables": |
| 236 |
buf = [] |
| 237 |
for line in fpiter: |
| 238 |
line = line.rstrip() |
| 239 |
if line == ".. end tables": |
| 240 |
fplines.append(".. begin tables") |
| 241 |
fplines.append("") |
| 242 |
fplines.extend(lines) |
| 243 |
fplines.append("") |
| 244 |
fplines.append(".. end tables") |
| 245 |
buf = [] |
| 246 |
break |
| 247 |
else: |
| 248 |
buf.append(line) |
| 249 |
else: |
| 250 |
fplines.extend(buf) |
| 251 |
_sys.stderr.write("Placeholder container not found!\n") |
| 252 |
else: |
| 253 |
fplines.append(line) |
| 254 |
finally: |
| 255 |
fp.close() |
| 256 |
|
| 257 |
fp = open(filename, "w") |
| 258 |
try: |
| 259 |
fp.write("\n".join(fplines) + "\n") |
| 260 |
finally: |
| 261 |
fp.close() |
| 262 |
|
| 263 |
|
| 264 |
def write_plain(filename, results): |
| 265 |
""" |
| 266 |
Output plain benchmark results |
| 267 |
|
| 268 |
:Parameters: |
| 269 |
`filename` : ``str`` |
| 270 |
Filename to write to |
| 271 |
|
| 272 |
`results` : ``list`` |
| 273 |
Results |
| 274 |
""" |
| 275 |
lines = [] |
| 276 |
results = sorted( |
| 277 |
results, |
| 278 |
key=(lambda x: [int(a) for a in x[0].split(".")]), |
| 279 |
reverse=True, |
| 280 |
) |
| 281 |
for idx, (version, import_notes, result) in enumerate(results): |
| 282 |
if idx: |
| 283 |
lines.append("") |
| 284 |
lines.append("") |
| 285 |
|
| 286 |
lines.append( |
| 287 |
"$ python%s -OO bench/main.py bench/*.js" |
| 288 |
% (".".join(version.split(".")[:2])) |
| 289 |
) |
| 290 |
lines.append("~" * 72) |
| 291 |
for note in import_notes: |
| 292 |
lines.append(uni(note)) |
| 293 |
lines.append("Python Release: %s" % (version,)) |
| 294 |
|
| 295 |
for single in result: |
| 296 |
lines.append("") |
| 297 |
lines.append( |
| 298 |
"Benchmarking %r... (%.1f KiB)" |
| 299 |
% (uni(single["filename"]), single["size"] / 1024.0) |
| 300 |
) |
| 301 |
for msg in single["messages"]: |
| 302 |
lines.append(msg) |
| 303 |
times = [] |
| 304 |
space = max([len(uni(port)) for port, _ in single["times"]]) |
| 305 |
for idx, (port, time) in enumerate(single["times"]): |
| 306 |
port = uni(port) |
| 307 |
if time is None: |
| 308 |
lines.append(" FAILED %s" % (port,)) |
| 309 |
else: |
| 310 |
times.append(time) |
| 311 |
lines.append( |
| 312 |
" Timing %s%s ... (%5.1f KiB %s) %8.2f ms" |
| 313 |
% ( |
| 314 |
port, |
| 315 |
" " * (space - len(port)), |
| 316 |
single["sizes"][idx] / 1024.0, |
| 317 |
idx == 0 |
| 318 |
and "*" |
| 319 |
or ["=", ">", "<"][ |
| 320 |
cmp(single["sizes"][idx], single["sizes"][0]) |
| 321 |
], |
| 322 |
time, |
| 323 |
) |
| 324 |
) |
| 325 |
if len(times) > 1: |
| 326 |
lines[-1] += " (factor: %s)" % ( |
| 327 |
", ".join( |
| 328 |
[ |
| 329 |
"%.2f" % (timed / time) |
| 330 |
for timed in times[:-1] |
| 331 |
] |
| 332 |
) |
| 333 |
) |
| 334 |
|
| 335 |
lines.append("") |
| 336 |
lines.append("") |
| 337 |
lines.append("# vim: nowrap") |
| 338 |
fp = open(filename, "w") |
| 339 |
try: |
| 340 |
fp.write("\n".join(lines) + "\n") |
| 341 |
finally: |
| 342 |
fp.close() |
| 343 |
|
| 344 |
|
| 345 |
def main(argv=None): |
| 346 |
"""Main""" |
| 347 |
import getopt as _getopt |
| 348 |
import pickle as _pickle |
| 349 |
|
| 350 |
if argv is None: |
| 351 |
argv = _sys.argv[1:] |
| 352 |
try: |
| 353 |
opts, args = _getopt.getopt(argv, "hp:t:", ["help"]) |
| 354 |
except getopt.GetoptError: |
| 355 |
e = _sys.exc_info()[0](_sys.exc_info()[1]) |
| 356 |
print >> _sys.stderr, "%s\nTry %s -mbench.write --help" % ( |
| 357 |
e, |
| 358 |
_os.path.basename(_sys.executable), |
| 359 |
) |
| 360 |
_sys.exit(2) |
| 361 |
|
| 362 |
plain, table = None, None |
| 363 |
for key, value in opts: |
| 364 |
if key in ("-h", "--help"): |
| 365 |
print >> _sys.stderr, ( |
| 366 |
"%s -mbench.write [-p plain] [-t table] <pickled" |
| 367 |
% (_os.path.basename(_sys.executable),) |
| 368 |
) |
| 369 |
_sys.exit(0) |
| 370 |
elif key == "-p": |
| 371 |
plain = str(value) |
| 372 |
elif key == "-t": |
| 373 |
table = str(value) |
| 374 |
|
| 375 |
struct = [] |
| 376 |
_sys.stdin = getattr(_sys.stdin, "detach", lambda: _sys.stdin)() |
| 377 |
try: |
| 378 |
while True: |
| 379 |
version, import_notes, result = _pickle.load(_sys.stdin) |
| 380 |
if hasattr(version, "decode"): |
| 381 |
version = version.decode("latin-1") |
| 382 |
struct.append((version, import_notes, result)) |
| 383 |
except EOFError: |
| 384 |
pass |
| 385 |
|
| 386 |
if plain: |
| 387 |
write_plain(plain, struct) |
| 388 |
|
| 389 |
if table: |
| 390 |
write_table(table, struct) |
| 391 |
|
| 392 |
|
| 393 |
if __name__ == "__main__": |
| 394 |
main() |