Add timing logs to get_template to identify DB query bottleneck

This commit is contained in:
YueGuobin 2026-06-15 23:18:00 +08:00
parent fa9aa5a9cc
commit 08e0dbc122
No known key found for this signature in database
2 changed files with 13 additions and 1 deletions

View File

@ -53,11 +53,15 @@ class TemplatesRepository(BaseRepository):
super().__init__(db_session)
async def get_template(self, template_id: UUID) -> Union[None, models.Template]:
import time
_t0 = time.time()
query = select(models.Template).\
options(selectinload(models.Template.images)).\
where(models.Template.template_id == template_id)
result = await self._db_session.execute(query)
elapsed = time.time() - _t0
if elapsed > 0.1:
log.warning(f"[CTRL-TIMING] DB get_template SLOW template_id={template_id} elapsed={elapsed:.3f}s")
return result.scalars().first()
async def get_template_by_name_and_version(self, name: str, version: str) -> Union[None, models.Template]:

View File

@ -17,6 +17,7 @@
import os
import uuid
import pydantic
import logging
from uuid import UUID
from fastapi.encoders import jsonable_encoder
@ -33,6 +34,8 @@ from gns3server.controller.controller_error import (
ControllerForbiddenError,
)
log = logging.getLogger(__name__)
TEMPLATE_TYPE_TO_SCHEMA = {
"cloud": schemas.CloudTemplate,
"ethernet_hub": schemas.EthernetHubTemplate,
@ -257,14 +260,19 @@ class TemplatesService:
return template
async def get_template(self, template_id: UUID) -> dict:
import time
_t0 = time.time()
db_template = await self._templates_repo.get_template(template_id)
log.info(f"[CTRL-TIMING] TemplatesService.get_template repo done elapsed={time.time()-_t0:.3f}s")
if db_template:
template = db_template.asjson()
else:
template = self.get_builtin_template(template_id)
if not template:
raise ControllerNotFoundError(f"Template '{template_id}' not found")
log.info(f"[CTRL-TIMING] TemplatesService.get_template DONE total={time.time()-_t0:.3f}s")
return template
async def _remove_image(self, template_id: UUID, image_path: str) -> None: