Skip to content

Commit

Permalink
Supposedly make benchmarks work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
purplesyringa committed Feb 24, 2024
1 parent 83a0752 commit 40eaaab
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import yaml
import os
import platform
import resource
import subprocess
import sys

try:
import resource
is_windows = False
except ModuleNotFoundError:
import win32process
is_windows = True


tmp = os.environ.get("TEMP", "/tmp")

Expand Down Expand Up @@ -62,20 +68,35 @@ def iterate_config(config, props = []):


def run(name, input, output, use_pipe):
rusage_before = resource.getrusage(resource.RUSAGE_CHILDREN)
if not is_windows:
rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
cpu_time_before = rusage.ru_utime + rusage.ru_stime
if use_pipe:
with open(input, "rb") as f_stdin:
input = f_stdin.read()
proc = subprocess.run([name], input=input, stdout=subprocess.PIPE, check=True)
proc = subprocess.Popen(
[name],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
stdout, _ = proc.communicate(input)
assert proc.poll() == 0
with open(output, "wb") as f_stdout:
f_stdout.write(proc.stdout)
f_stdout.write(stdout)
else:
with open(input, "rb") as f_stdin:
with open(output, "wb") as f_stdout:
subprocess.run([name], stdin=f_stdin, stdout=f_stdout, check=True)
rusage_after = resource.getrusage(resource.RUSAGE_CHILDREN)
cpu_time_before = rusage_before.ru_utime + rusage_before.ru_stime
cpu_time_after = rusage_after.ru_utime + rusage_after.ru_stime
proc = subprocess.Popen(
[name],
stdin=f_stdin,
stdout=f_stdout
)
assert proc.wait() == 0
if is_windows:
stat = win32process.GetProcessTimes(proc._handle)
return (stat["KernelTime"] + stat["UserTime"]) / 1e7
rusage = resource.getrusage(resource.RUSAGE_CHILDREN)
cpu_time_after = rusage.ru_utime + rusage.ru_stime
return cpu_time_after - cpu_time_before


Expand Down

0 comments on commit 40eaaab

Please sign in to comment.