Skip to content

Commit

Permalink
Add resource usage
Browse files Browse the repository at this point in the history
Closes #263
  • Loading branch information
jeromekelleher committed Sep 13, 2024
1 parent 3c8cb4a commit 5a3250e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ dependencies = [
"scipy",
"click",
"zarr<2.18",
"humanize",
"resource",
]
dynamic = ["version"]

Expand Down
35 changes: 35 additions & 0 deletions sc2ts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import contextlib
import dataclasses
import datetime
import time
import os

import numpy as np
import tqdm
Expand All @@ -15,13 +17,42 @@
import tsinfer
import click
import daiquiri
import humanize
import pandas as pd

try:
import resource
except ImportError:
resource = None # resource.getrusage absent on windows, so skip outputting max mem

import sc2ts
from . import core
from . import utils

logger = logging.getLogger(__name__)

__before = time.time()


def summarise_usage():
wall_time = time.time() - __before
user_time = os.times().user
sys_time = os.times().system
if resource is None:
# Don't report max memory on Windows. We could do this using the psutil lib, via
# psutil.Process(os.getpid()).get_ext_memory_info().peak_wset if demand exists
maxmem_str = "?"
else:
max_mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
if sys.platform != "darwin":
max_mem *= 1024 # Linux and other OSs (e.g. freeBSD) report maxrss in kb
maxmem_str = "; max_memory=" + humanize.naturalsize(max_mem, binary=True)
return (
f"Done in {humanize.naturaldelta(wall_time)}; "
f"elapsed={wall_time:.2f}; user={user_time:.2f}; sys={sys_time:.2f}"
+ maxmem_str
)


def get_environment():
"""
Expand Down Expand Up @@ -356,6 +387,10 @@ def extend(
show_progress=progress,
)
add_provenance(ts_out, output_ts)
resource_usage = f"{date}:{summarise_usage()}"
logger.info(resource_usage)
if progress:
print(resource_usage, file=sys.stderr)


@click.command()
Expand Down

0 comments on commit 5a3250e

Please sign in to comment.