tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bench_wpt.py (1198B)


      1 import io
      2 import os
      3 import sys
      4 
      5 import pyperf
      6 
      7 sys.path[0:0] = [os.path.join(os.path.dirname(__file__), "..")]
      8 import html5lib  # noqa: E402
      9 
     10 
     11 def bench_html5lib(fh):
     12    fh.seek(0)
     13    html5lib.parse(fh, treebuilder="etree", useChardet=False)
     14 
     15 
     16 def add_cmdline_args(cmd, args):
     17    if args.benchmark:
     18        cmd.append(args.benchmark)
     19 
     20 
     21 BENCHMARKS = {}
     22 for root, dirs, files in os.walk(os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "wpt")):
     23    for f in files:
     24        if f.endswith(".html"):
     25            BENCHMARKS[f[: -len(".html")]] = os.path.join(root, f)
     26 
     27 
     28 if __name__ == "__main__":
     29    runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
     30    runner.metadata["description"] = "Run parser benchmarks from WPT"
     31    runner.argparser.add_argument("benchmark", nargs="?", choices=sorted(BENCHMARKS))
     32 
     33    args = runner.parse_args()
     34    if args.benchmark:
     35        benchmarks = (args.benchmark,)
     36    else:
     37        benchmarks = sorted(BENCHMARKS)
     38 
     39    for bench in benchmarks:
     40        name = "wpt_%s" % bench
     41        path = BENCHMARKS[bench]
     42        with open(path, "rb") as fh:
     43            fh2 = io.BytesIO(fh.read())
     44 
     45        runner.bench_func(name, bench_html5lib, fh2)