Skip to content

Commit

Permalink
docs: added complexity metrics with radon to arewetiny docs, as makef…
Browse files Browse the repository at this point in the history
…ile target (#428)

* docs: added complexity metrics with radon to arewetiny docs, as makefile target

* build(deps): updated lockfile
  • Loading branch information
ErikBjare authored Jan 30, 2025
1 parent 2059ddd commit 0bd43f3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
24 changes: 22 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ SHELL := $(shell which bash)

# src dirs and files
SRCDIRS = gptme tests scripts
SRCFILES = $(shell find ${SRCDIRS} -name '*.py')
SRCFILES_RAW = $(shell find gptme tests -name '*.py' && find scripts -name '*.py' -not -path "scripts/Kokoro-82M/*" -not -path "*/Kokoro-82M/*")

# exclude files
EXCLUDES = tests/output scripts/build_changelog.py scripts/tts_server.py
SRCFILES = $(shell find ${SRCDIRS} -name '*.py' $(foreach EXCLUDE,$(EXCLUDES),-not -path $(EXCLUDE)))
SRCFILES = $(shell echo "${SRCFILES_RAW}" | tr ' ' '\n' | grep -v -f <(echo "${EXCLUDES}" | tr ' ' '\n') | tr '\n' ' ')

# radon args
RADON_ARGS = --exclude "scripts/Kokoro-82M/*" --exclude "*/Kokoro-82M/*"

build:
poetry install
Expand Down Expand Up @@ -159,6 +162,23 @@ cloc-eval:
cloc-total:
cloc ${SRCFILES} --by-file

# Code metrics
.PHONY: metrics

metrics:
@echo "=== Code Metrics Summary ==="
@echo
@echo "Project Overview:"
@echo " Files: $$(find ${SRCDIRS} -name '*.py' | wc -l)"
@echo " Total blocks: $$(poetry run radon cc ${SRCFILES} --total-average | grep "blocks" | cut -d' ' -f1 | tr -d '\n')"
@echo " Average complexity: $$(poetry run radon cc ${SRCFILES} --average --total-average | grep "Average complexity" | cut -d'(' -f2 | cut -d')' -f1)"
@echo
@echo "Most Complex Functions (D+):"
@poetry run radon cc ${SRCFILES} --min D | grep -v "^$$" | grep -E "^[^ ]| [FCM].*[DE]" | sed 's/^/ /'
@echo
@echo "Largest Files (>300 SLOC):"
@poetry run radon raw ${SRCFILES} | awk '/^[^ ]/ {file=$$0} /SLOC:/ {if ($$2 > 300) printf " %4d %s\n", $$2, file}' | sort -nr

bench-importtime:
time poetry run python -X importtime -m gptme --model openai --non-interactive 2>&1 | grep "import time" | cut -d'|' -f 2- | sort -n
@#time poetry run python -X importtime -m gptme --model openrouter --non-interactive 2>&1 | grep "import time" | cut -d'|' -f 2- | sort -n
Expand Down
19 changes: 19 additions & 0 deletions docs/arewetiny.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ LoC Total

.. command-output:: make cloc-total
:cwd: ..

Code Metrics
------------

.. command-output:: make metrics
:cwd: ..

The metrics above show:

- **Project Overview**: Basic stats about the codebase size and complexity
- **Complex Functions**: Functions rated D+ (high complexity, should be refactored)
- **Large Files**: Files over 300 SLOC (should be split into smaller modules)

We should aim to:

- Keep average complexity below 4.0
- Have no E-rated functions (extremely complex)
- Have few D-rated functions (very complex)
- Keep files under 300 SLOC where possible
43 changes: 41 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mypy = "*"
ruff = "*"
pylint = "*"
pre-commit = "*"
radon = "*"

# test
pytest = "^8.0"
Expand Down

0 comments on commit 0bd43f3

Please sign in to comment.