Speed up the RRS pages, and test them
cover Speed up the RRS pages, and test them
The RRS recipe list, report, maintainers and recipe detail pages ran queries per recipe, maintainer or upgrade: about 5,000 for the OE-Core recipe list, which took 4 s, and over 2,500 for linux-yocto's detail page. The database itself was busy for under 0.5 s of that, and the lookups already use indexes, so these batch the queries instead.
1: RRS views had no tests, because the test database is built without
the app. Add tests/rrs/, run by docker/run-tests.sh in a second pass with ENABLE_RRS=1 and --nomigrations (the RRS data migrations need the core layer first), plus a check for model changes without a migration. RRS stays optional: without it tests/rrs/ isn't collected.
2-4: Batch the queries of the recipe list and report, the maintainers
page and the recipe detail page. Each comes with tests of the page data and of the query count, which fail without the change.
On a copy of the production database on a test bench instance, the page data before and after is identical for 12 recipe list milestones, 9 maintainers pages and 153 recipe detail pages:
Page (OE-Core 6.2) Before After recipes/ 4.3 s 0.32 s recipesreport/ 3.7 s 0.12 s maintainers/ 1.17 s 0.17 s recipedetail/ (linux-yocto) 3.7 s 0.30 s
Building the meta-openembedded recipe list goes from 9 s to 0.3 s.
This applies on master-next: patch 1 changes the lines of docker/run-tests.sh that "requirements-dev.txt: pin the development and test tools" changed:
https://lore.kernel.org/all/[email protected]/
Tim Orling (4):
docker/run-tests.sh: run tests for the RRS app with RRS enabled rrs/views.py: fetch the recipe list's patch counts in a few queries rrs/views.py: fetch the maintainers page's statistics in a few queries rrs/views.py: fetch the recipe detail page's upgrades in a few queries
README.devel.md | 9 ++ docker/run-tests.sh | 9 +- rrs/views.py | 171 +++++++++++++++++++++++++++----- tests/rrs/conftest.py | 52 ++++++++++ tests/rrs/test_maintainers.py | 94 ++++++++++++++++++ tests/rrs/test_recipe_detail.py | 84 ++++++++++++++++ tests/rrs/test_recipe_list.py | 71 +++++++++++++ tests/rrs/test_views.py | 52 ++++++++++ 8 files changed, 514 insertions(+), 28 deletions(-) create mode 100644 tests/rrs/conftest.py create mode 100644 tests/rrs/test_maintainers.py create mode 100644 tests/rrs/test_recipe_detail.py create mode 100644 tests/rrs/test_recipe_list.py create mode 100644 tests/rrs/test_views.py
Signature · 1 line
2.56.1
PATCH 1/4 docker/run-tests.sh: run tests for the RRS app with RRS enabled
RRS is optional, so its views had no tests: the test database is built without the app. Add tests/rrs/, collected only when RRS is installed, with a fixture for a maintenance plan and smoke tests for the RRS pages, and run it in a second pass with ENABLE_RRS=1. That pass builds the tables with --nomigrations, because the RRS data migrations need the core layer before they run (README.rrs.md), and checks that the RRS models have no changes missing a migration.
AI-Generated: Claude Opus 5.5
--- README.devel.md | 9 +++++++ docker/run-tests.sh | 9 ++++++- tests/rrs/conftest.py | 52 +++++++++++++++++++++++++++++++++++++++++ tests/rrs/test_views.py | 52 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/rrs/conftest.py create mode 100644 tests/rrs/test_views.py
README.devel.md +9 -0
@@ -198,6 +198,15 @@ at a MariaDB/MySQL server where your database user may create databases pytest --ignore=tests/test_update.py tests/ ``` +The tests in `tests/rrs/` need the RRS app and are skipped without it. +`docker/run-tests.sh` runs them in a second pass with `ENABLE_RRS=1`. To run +them directly, add `'rrs'` to `INSTALLED_APPS` and build the test database from +the models, since the RRS data migrations need the core layer to exist first: + +```sh +pytest --nomigrations tests/rrs/ +``` + `tests/test_dockersetup.py` tests the files `dockersetup.py` writes. It needs only pytest and PyYAML, and is skipped inside the Docker image:
docker/run-tests.sh +8 -1
@@ -27,6 +27,10 @@ done # Runs as root only to prepare the container: tests/test_update.py rewrites # settings.py and bitbake writes its lock file next to it, the update scripts # expect bitbake already fetched, and the tests commit to git and call tree. +# The tests in tests/rrs/ need RRS, so a second pass runs them with +# ENABLE_RRS=1. --nomigrations builds the tables from the models: the RRS +# data migrations need the core layer to exist first (see README.rrs.md), +# which an empty database lacks. docker run --rm --network $NET -u root \ -e SECRET_KEY=ci-only \ -e GIT_AUTHOR_NAME=ci -e [email protected] \
@@ -40,4 +44,7 @@ docker run --rm --network $NET -u root \ su layers -s /bin/sh -c "set -e git clone -q https://git.openembedded.org/bitbake /opt/workdir/https___git_openembedded_org_bitbake pip3 install -q --user --no-warn-script-location -r /src/requirements-dev.txt - python3 -m pytest -rfE -p no:cacheprovider tests/"' + python3 -m pytest -rfE -p no:cacheprovider tests/ + export ENABLE_RRS=1 + python3 manage.py makemigrations --check --dry-run rrs + python3 -m pytest -rfE -p no:cacheprovider --nomigrations tests/rrs/"'
tests/rrs/conftest.py new file +52 -0
52 lines not on this page · raw message
tests/rrs/test_views.py new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.56.1
PATCH 2/4 rrs/views.py: fetch the recipe list's patch counts in a few queries
In reply to Tim Orling_get_recipe_list() ran five queries per recipe, so the recipes and report pages took about 4 s for OE-Core and 9 s for meta-openembedded, with the database busy for under 0.5 s of that. Fetch the recipe symbols with in_bulk() and the patch counts with one annotated query per layer branch, counting the same recipe as before when several share a pn.
On a copy of the production database the recipe lists of 12 milestones are identical before and after, and OE-Core 6.2 takes 9 queries and 0.12 s instead of 4880 queries and 3.7 s.
AI-Generated: Claude Opus 5.5
--- rrs/views.py | 30 ++++++++++----- tests/rrs/test_recipe_list.py | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 tests/rrs/test_recipe_list.py
rrs/views.py +21 -9
@@ -17,6 +17,7 @@ from django.shortcuts import get_object_or_404 from django.views.generic import TemplateView, ListView, DetailView, RedirectView from django.urls import resolve, reverse, reverse_lazy from django.db import connection +from django.db.models import Count, Q from django.contrib import messages from layerindex.models import Recipe, StaticBuildDep, Patch
@@ -432,12 +433,29 @@ def _get_recipe_list(milestone): for ma in maintainers_all: maintainers_dict_all[ma['recipesymbol_id']] = ma['name'] + # Fetch the recipe symbols, and the patch counts of their layer index + # recipes, in a few queries rather than several per recipe + # (AI-generated, Claude Opus 5.5) + recipesymbols = RecipeSymbol.objects.in_bulk([recipe['id'] for recipe in recipes]) + patch_counts = {} + if recipesymbols: + recipeobjs = Recipe.objects.filter( + layerbranch_id__in={rs.layerbranch_id for rs in recipesymbols.values()}, + pn__in={rs.pn for rs in recipesymbols.values()}) \ + .order_by('id') \ + .annotate(patches_total=Count('patch'), + patches_pending=Count('patch', filter=Q(patch__status='P'))) + for recipeobj in recipeobjs: + # Keep the first recipe by id for a pn, as .first() did + patch_counts.setdefault((recipeobj.layerbranch_id, recipeobj.pn), + (recipeobj.patches_total, recipeobj.patches_pending)) + for recipe in recipes: upstream_version = '' upstream_status = '' no_update_reason = '' outdated = '' - recipesymbol = RecipeSymbol.objects.filter(id=recipe['id']).first() + recipesymbol = recipesymbols.get(recipe['id']) if recipe_upstream_history: recipe_upstream = recipe_upstream_dict_all.get(recipe['id'])
@@ -479,14 +497,8 @@ def _get_recipe_list(milestone): recipe_list_item.upstream_status = upstream_status recipe_list_item.upstream_version = upstream_version recipe_list_item.outdated = outdated - recipeobj = recipesymbol.layerbranch.recipe_set.filter(pn=recipesymbol.pn, layerbranch=recipesymbol.layerbranch).first() - if recipeobj: - patches = recipeobj.patch_set - recipe_list_item.patches_total = patches.count() - recipe_list_item.patches_pending = patches.filter(status='P').count() - else: - recipe_list_item.patches_total = 0 - recipe_list_item.patches_pending = 0 + recipe_list_item.patches_total, recipe_list_item.patches_pending = \ + patch_counts.get((recipesymbol.layerbranch_id, recipesymbol.pn), (0, 0)) recipe_list_item.maintainer_name = maintainer_name recipe_list_item.no_update_reason = no_update_reason recipe_list.append(recipe_list_item)
tests/rrs/test_recipe_list.py new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.56.1
PATCH 3/4 rrs/views.py: fetch the maintainers page's statistics in a few queries
In reply to Tim OrlingThe maintainers page ran about a dozen queries per maintainer: the statistics of each maintainer separately, then one count of upgrades per maintainer per interval. Compute every maintainer's statistics from one read of the maintainer and upstream records per layer branch, and count each interval's upgrades for all maintainers in one grouped query.
On a copy of the production database the page data for 9 milestones is identical before and after, and OE-Core 6.2 takes 28 queries and 0.08 s instead of 952 queries and 0.75 s.
AI-Generated: Claude Opus 5.5
--- rrs/views.py | 78 +++++++++++++++++++++++++++-- tests/rrs/test_maintainers.py | 94 +++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 tests/rrs/test_maintainers.py
rrs/views.py +74 -4
@@ -373,6 +373,68 @@ def _get_milestone_statistics(milestone, maintainer_name=None): return milestone_statistics +def _get_maintainers_statistics(milestone, maintainer_names): + """ + The statistics of _get_milestone_statistics(milestone, name) for every + name in maintainer_names, with a few queries per layer branch rather + than several per maintainer (AI-generated, Claude Opus 5.5) + """ + statistics = {name: {'all': 0, 'up_to_date': 0, 'not_updated': 0, + 'cant_be_updated': 0, 'unknown': 0} + for name in maintainer_names} + if not statistics: + return statistics + + for maintplanlayer in milestone.release.plan.maintenanceplanlayerbranch_set.all(): + layerbranch = maintplanlayer.layerbranch + + recipe_upstream_history = RecipeUpstreamHistory.get_last_by_date_range( + layerbranch, + milestone.start_date, + milestone.end_date + ) + recipe_maintainer_history = Raw.get_remahi_by_end_date( + layerbranch.id, milestone.end_date) + if not recipe_maintainer_history: + continue + + maintainer_recipes = {} + for recipesymbol_id, name in RecipeMaintainer.objects.filter( + history_id=recipe_maintainer_history[0]).values_list( + 'recipesymbol_id', 'maintainer__name'): + maintainer_recipes.setdefault(name, set()).add(recipesymbol_id) + + recipe_upstreams = {} + if recipe_upstream_history and maintainer_recipes: + for ru in RecipeUpstream.objects.filter( + history=recipe_upstream_history).values( + 'recipesymbol_id', 'status', 'no_update_reason'): + recipe_upstreams.setdefault(ru['recipesymbol_id'], []).append(ru) + + for name, maintainer_statistics in statistics.items(): + recipes = maintainer_recipes.get(name, set()) + maintainer_statistics['all'] += len(recipes) + for recipesymbol_id in recipes: + for ru in recipe_upstreams.get(recipesymbol_id, []): + if ru['status'] == 'Y': + maintainer_statistics['up_to_date'] += 1 + elif ru['status'] == 'N': + if ru['no_update_reason'] == '': + maintainer_statistics['not_updated'] += 1 + else: + maintainer_statistics['cant_be_updated'] += 1 + else: + maintainer_statistics['unknown'] += 1 + + for maintainer_statistics in statistics.values(): + maintainer_statistics['percentage'] = '0' + if maintainer_statistics['all'] > 0: + maintainer_statistics['percentage'] = "%.0f" % \ + ((float(maintainer_statistics['up_to_date']) / + float(maintainer_statistics['all'])) * 100) + + return statistics + class RecipeList(): pk = None name = None
@@ -879,8 +941,18 @@ class MaintainerListView(ListView): self.intervals = sorted(intervals.keys()) current_date = date.today() + maintainer_names = {ml.name for ml in maintainer_list} + all_statistics = _get_maintainers_statistics(milestone, maintainer_names) + # Upgrades per maintainer in each interval, one query per interval + interval_upgrades = [] + for i in (self.intervals if maintainer_names else []): + interval_upgrades.append(dict(RecipeUpgrade.objects.filter( + maintainer__name__in=maintainer_names, + commit_date__gte=intervals[i]['start_date'], + commit_date__lte=intervals[i]['end_date']).values_list( + 'maintainer__name').annotate(Count('id')).order_by())) for ml in maintainer_list: - milestone_statistics = _get_milestone_statistics(milestone, ml.name) + milestone_statistics = all_statistics[ml.name] ml.recipes_all = milestone_statistics['all'] ml.recipes_up_to_date = ('' if milestone_statistics['up_to_date'] == 0 else milestone_statistics['up_to_date'])
@@ -901,9 +973,7 @@ class MaintainerListView(ListView): if current_date >= start_date and current_date <= end_date: self.current_interval = idx - number = RecipeUpgrade.objects.filter(maintainer__name = ml.name, - commit_date__gte = start_date, - commit_date__lte = end_date).count() + number = interval_upgrades[idx].get(ml.name, 0) ml.interval_statistics.append('' if number == 0 else number) # To add Wk prefix after get statics to avoid sorting problems
tests/rrs/test_maintainers.py new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.56.1
PATCH 4/4 rrs/views.py: fetch the recipe detail page's upgrades in a few queries
In reply to Tim OrlingThe recipe detail page ran about nine queries per upgrade to find the release and milestone it falls in and whether its author maintained the recipe then, so linux-yocto, with 282 upgrades, took 3.7 s. Read the plan's releases and milestones once and pick from them by date, and look up each maintainer history and maintainer once per page. Releases with the same dates are picked in id order, as the queries returned them. Also fetch the other branches' recipes with their layer branch and branch.
On a copy of the production database the page data for 153 OE-Core recipes is identical before and after, and linux-yocto takes 0.3 s instead of 3.6 s.
AI-Generated: Claude Opus 5.5
--- rrs/views.py | 63 +++++++++++++++++++------ tests/rrs/test_recipe_detail.py | 84 +++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 14 deletions(-) create mode 100644 tests/rrs/test_recipe_detail.py
rrs/views.py +49 -14
@@ -754,31 +754,63 @@ class RecipeUpgradeDetail(): self.upgrade_type = upgrade_type self.group = group -def _get_recipe_upgrade_detail(maintplan, recipe_upgrade): +def _get_release_and_milestone(maintplan, commit_date, lookups): + """ + Release.get_by_date() and Milestone.get_by_release_and_date() for + commit_date, from the plan's releases and milestones read once into + lookups (AI-generated, Claude Opus 5.5) + """ + if 'releases' not in lookups: + # id breaks ties between releases with the same dates as the queries did + lookups['releases'] = list(Release.objects.filter(plan=maintplan).order_by('-end_date', 'id')) + lookups['milestones'] = {} + for milestone in Milestone.objects.filter(release__plan=maintplan) \ + .exclude(name='All').order_by('-end_date', 'id'): + lookups['milestones'].setdefault(milestone.release_id, []).append(milestone) + # The queries compare the DateFields with the commit's date + commit_day = commit_date.date() + release = next((r for r in lookups['releases'] + if r.start_date <= commit_day <= r.end_date), None) + milestone = None + if release: + milestone = next((m for m in lookups['milestones'].get(release.id, []) + if m.start_date <= commit_day <= m.end_date), None) + return release, milestone + +def _get_recipe_upgrade_detail(maintplan, recipe_upgrade, lookups=None): + # lookups keeps what several upgrades of a recipe share, so a caller + # that passes the same dict for each runs fewer queries + if lookups is None: + lookups = {} release_name = '' milestone_name = '' recipe_maintainer_history = None - release = Release.get_by_date(maintplan, recipe_upgrade.commit_date) + release, milestone = _get_release_and_milestone(maintplan, recipe_upgrade.commit_date, lookups) if release: release_name = release.name - milestone = Milestone.get_by_release_and_date(release, - recipe_upgrade.commit_date) if milestone: milestone_name = milestone.name - recipe_maintainer_history = RecipeMaintainerHistory.get_by_end_date( - recipe_upgrade.recipesymbol.layerbranch, - milestone.end_date) + key = ('maintainer_history', recipe_upgrade.recipesymbol.layerbranch_id, milestone.end_date) + if key not in lookups: + lookups[key] = RecipeMaintainerHistory.get_by_end_date( + recipe_upgrade.recipesymbol.layerbranch, + milestone.end_date) + recipe_maintainer_history = lookups[key] is_recipe_maintainer = False maintainer_name = '' if not recipe_upgrade.maintainer is None: maintainer_name = recipe_upgrade.maintainer.name - if not recipe_maintainer_history is None and \ - RecipeMaintainer.objects.filter(maintainer__name - = maintainer_name, history = recipe_maintainer_history) \ - .count() > 0: + key = ('is_maintainer', maintainer_name, + recipe_maintainer_history.id if recipe_maintainer_history else None) + if key not in lookups: + lookups[key] = not recipe_maintainer_history is None and \ + RecipeMaintainer.objects.filter(maintainer__name + = maintainer_name, history = recipe_maintainer_history) \ + .exists() + if lookups[key]: is_recipe_maintainer = True commit_date = recipe_upgrade.commit_date.date().isoformat()
@@ -849,8 +881,11 @@ class RecipeDetailView(DetailView): details = [] multigroup = False lastgroup = '' # can't use None here - for ru in RecipeUpgrade.objects.filter(recipesymbol=recipesymbol).exclude(upgrade_type='M').order_by('group', '-commit_date', '-id'): - details.append(_get_recipe_upgrade_detail(maintplan, ru)) + lookups = {} + # Through the related manager, each upgrade shares recipesymbol + for ru in recipesymbol.recipeupgrade_set.select_related('maintainer', 'group') \ + .exclude(upgrade_type='M').order_by('group', '-commit_date', '-id'): + details.append(_get_recipe_upgrade_detail(maintplan, ru, lookups)) if not multigroup: if lastgroup == '': lastgroup = ru.group
@@ -882,7 +917,7 @@ class RecipeDetailView(DetailView): context['recipe_depends'] = [] context['recipe_distros'] = [] - context['otherbranch_recipes'] = Recipe.objects.filter(layerbranch__layer=recipesymbol.layerbranch.layer, layerbranch__branch__comparison=False, pn=recipesymbol.pn).order_by('layerbranch__branch__sort_priority') + context['otherbranch_recipes'] = Recipe.objects.filter(layerbranch__layer=recipesymbol.layerbranch.layer, layerbranch__branch__comparison=False, pn=recipesymbol.pn).select_related('layerbranch__branch').order_by('layerbranch__branch__sort_priority') return context
tests/rrs/test_recipe_detail.py new file +84 -0
84 lines not on this page · raw message
Signature · 1 line
2.56.1