Skip to content

Commit

Permalink
misc(ac-random-test): show ellapsed time
Browse files Browse the repository at this point in the history
  • Loading branch information
himkt committed Apr 28, 2024
1 parent 4f72d4e commit 6c26034
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions bin/ac-random-test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os
import shutil
import subprocess
import sys
import time

"""
Example of `--gen-input-cmd gen.py`
Expand All @@ -24,9 +25,11 @@ with open("in.txt", "w") as f:
```
"""

# :link: https://gist.github.com/rene-d/9e584a7dd2935d0f461904b9f2950007
class bcolors:
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARN = '\033[1;33m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
Expand All @@ -35,8 +38,20 @@ devnull = open(os.devnull, "w")


def run_and_decode(args: list[str]) -> str:
s = time.time()
ret = subprocess.check_output(args, stdin=open("in.txt"), stderr=devnull)
return ret.decode("utf-8").rstrip()
f = time.time()
return (ret.decode("utf-8").rstrip(), 1000.0 * (f - s))


def ellapsed_with_color(ellapsed: float) -> str:
if ellapsed >= 2000.0:
color = bcolors.FAIL
elif ellapsed >= 1000.0:
color = bcolors.WARN
else:
color = bcolors.OKBLUE
return color + f"{ellapsed:.2f}" + bcolors.ENDC


def verify(args: argparse.Namespace):
Expand All @@ -52,15 +67,15 @@ def verify(args: argparse.Namespace):
ret_cmd = [s.format(argv0=name) for s in run_cmd]
ret_lazy_cmd = [s.format(argv0=f"{name}_lazy") for s in run_cmd]

ret = run_and_decode(ret_cmd)
ret, ellapsed = run_and_decode(ret_cmd)
if args.without_lazy:
print("Run: " + bcolors.OKGREEN + "passed" + bcolors.ENDC)
print("Run: " + bcolors.OKGREEN + "passed" + bcolors.ENDC + f" ({ellapsed_with_color(ellapsed)}ms)")
return

ret_lazy = run_and_decode(ret_lazy_cmd)
ret_lazy, _ = run_and_decode(ret_lazy_cmd)

if ret_lazy == ret:
print("Check: " + bcolors.OKGREEN + "passed" + bcolors.ENDC)
print("Check: " + bcolors.OKGREEN + "passed" + bcolors.ENDC + f" ({ellapsed_with_color(ellapsed)}ms)")
else:
print("Check: " + bcolors.FAIL + "failed" + bcolors.ENDC)
print("Found the edge case and stored in in.txt.", end="")
Expand Down

0 comments on commit 6c26034

Please sign in to comment.