Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiling #141

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ title: "What's new?"
These are the unreleased changes of Delft-FIAT.

### Added
- Custom rasterization functions instead of relying on GDAL (which included dataset creation at runtime)
- Profiler for developers

### Changed
- Disabled locks when running 'single threaded'
- Fixed logging of errors during settings file checks
- Improved performance when running without csv
- Logging class `Log` is now called `Logger`
- Specifying destination ('dst') is now optional for `setup_default_log`

Expand Down
14 changes: 12 additions & 2 deletions src/fiat/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from fiat.cfg import ConfigReader
from fiat.cli.formatter import MainHelpFormatter
from fiat.cli.util import file_path_check, run_log
from fiat.cli.util import file_path_check, run_log, run_profiler
from fiat.log import check_loglevel, setup_default_log
from fiat.main import FIAT
from fiat.version import __version__
Expand Down Expand Up @@ -77,7 +77,10 @@ def run(args):

# Kickstart the model
obj = FIAT(cfg)
run_log(obj.run, logger=logger)
if args.profile is not None:
run_profiler(obj.run, profile=args.profile, cfg=cfg, logger=logger)
else:
run_log(obj.run, logger=logger)


## Constructing the arguments parser for FIAT.
Expand Down Expand Up @@ -157,6 +160,13 @@ def args_parser():
action="count",
default=0,
)
run_parser.add_argument(
"-p",
"--profile",
help="Run profiler",
action="store_const",
const="profile",
)
run_parser.set_defaults(func=run)
return parser

Expand Down
32 changes: 32 additions & 0 deletions src/fiat/cli/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Util for cli."""

import cProfile
import pstats
import sys
from pathlib import Path
from typing import Callable

from fiat.cfg import ConfigReader
from fiat.log import Logger


Expand Down Expand Up @@ -36,3 +39,32 @@ def run_log(
sys.exit(1)
else:
return out


def run_profiler(
func: Callable,
profile: str,
cfg: ConfigReader,
logger: Logger,
):
"""Run the profiler from cli."""
logger.warning("Running profiler...")

# Setup the profiler and run the function
profiler = cProfile.Profile()
profiler.enable()
run_log(func, logger=logger)
profiler.disable()

# Save all the stats
profile_out = cfg.get("output.path") / profile
profiler.dump_stats(profile_out)
logger.info(f"Saved profiling stats to: {profile_out}")

# Save a human readable portion to a text file
txt_out = cfg.get("output.path") / "profile.txt"
with open(txt_out, "w") as _w:
_w.write(f"Delft-FIAT profile ({cfg.filepath}):\n\n")
stats = pstats.Stats(profiler, stream=_w)
_ = stats.sort_stats("tottime").print_stats()
logger.info(f"Saved profiling stats in human readable format: {txt_out}")
Loading
Loading