Skip to content

Commit

Permalink
Address Victor's comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorusso committed Sep 24, 2024
1 parent 0293c7d commit 4e892b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pyperf/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pyperf._utils import MS_WINDOWS, create_environ, create_pipe, popen_killer


EXIT_TIMEOUT = 60

# Limit to 5 calibration processes
# (10 if calibration is needed for loops and warmups)
MAX_CALIBRATION = 5
Expand Down Expand Up @@ -107,7 +109,7 @@ def spawn_worker(self, calibrate_loops, calibrate_warmups):
with popen_killer(proc):
try:
bench_json = rpipe.read_text()
exitcode = proc.wait()
exitcode = proc.wait(timeout=EXIT_TIMEOUT)
except TimeoutError as exc:
print(exc)
sys.exit(124)
Expand Down
25 changes: 12 additions & 13 deletions pyperf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,26 +327,25 @@ def open_text(self):

def read_text(self):
with self.open_text() as rfile:
if self._timeout:
return self._read_text_timeout(rfile, self._timeout)
if self._timeout is not None:
return self._read_text_timeout(rfile, self._timeout)
else:
return rfile.read()

def _read_text_timeout(self, rfile, timeout):
start_time = time.time()
start_time = time.monotonic()
output = []
while True:
if time.time() - start_time > timeout:
if time.monotonic() - start_time > timeout:
raise TimeoutError(f"Timed out after {timeout} seconds")
ready, _, _ = select.select([rfile], [], [], timeout)
if ready:
data = rfile.read()
if data:
return data
else:
break
else:
pass

if not ready:
continue
data = rfile.read(1024)
if not data:
break
output.append(data)
return "".join(output)


class WritePipe(_Pipe):
Expand Down
4 changes: 2 additions & 2 deletions pyperf/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_pipe(self):
tests.benchmark_as_json(result.bench))

def test_pipe_with_timeout(self):
rpipe, wpipe = create_pipe(timeout=1)
rpipe, wpipe = create_pipe(timeout=0.1)
with rpipe:
with wpipe:
arg = wpipe.to_subprocess()
Expand All @@ -165,7 +165,7 @@ def test_pipe_with_timeout(self):
with self.assertRaises(TimeoutError) as cm:
rpipe.read_text()
self.assertEqual(str(cm.exception),
'Timed out after 1 seconds')
'Timed out after 0.1 seconds')

def test_json_exists(self):
with tempfile.NamedTemporaryFile('wb+') as tmp:
Expand Down

0 comments on commit 4e892b4

Please sign in to comment.