Skip to content

fix: Improve performance on TestStatusHistory endpoint#1863

Open
alanpeixinho wants to merge 1 commit intokernelci:mainfrom
profusion:fix/improve-test-status-history-performance
Open

fix: Improve performance on TestStatusHistory endpoint#1863
alanpeixinho wants to merge 1 commit intokernelci:mainfrom
profusion:fix/improve-test-status-history-performance

Conversation

@alanpeixinho
Copy link
Copy Markdown
Contributor

@alanpeixinho alanpeixinho force-pushed the fix/improve-test-status-history-performance branch from 33c96f7 to 18d172a Compare April 16, 2026 17:48
@alanpeixinho alanpeixinho marked this pull request as ready for review April 16, 2026 17:49
@gustavobtflores gustavobtflores added Backend Most or all of the changes for this issue will be in the backend code. Queries Issue that involves modifying some DB query labels Apr 16, 2026
Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, only some small comments

Comment thread backend/kernelCI_app/views/testStatusHistoryView.py Outdated
Comment thread backend/kernelCI_app/typeModels/testDetails.py Outdated
Comment thread backend/kernelCI_app/tests/unitTests/queries/test_queries_test.py
@alanpeixinho alanpeixinho force-pushed the fix/improve-test-status-history-performance branch from 18d172a to b513e72 Compare April 17, 2026 20:53
Comment thread backend/kernelCI_app/tests/unitTests/queries/test_queries_test.py Outdated
Comment on lines +72 to +75
start_time: Optional[datetime]
id: str
status: Optional[str]
git_commit_hash: Optional[str]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if reverting to optional[type] then we can keep using the value that was here before. We are just using these types (such as Test_StartTime) to signal that these values come directly from the database

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you were going to change the status field though, to default to "NULL" when it is None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed back to use the database types. But in there, Test__Status is an Optional[Literal["FAIL", "PASS", "SKIP", "ERROR", "MISS", "DONE"]], so the null value is represented as None.
Changing that could impact all uses of this type.

@alanpeixinho alanpeixinho force-pushed the fix/improve-test-status-history-performance branch 7 times, most recently from 14e9d0f to 2861582 Compare April 24, 2026 12:53
Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tested and it seems all good

Comment thread backend/kernelCI_app/queries/test.py Outdated
Comment on lines +86 to +93
if test_start_time is None:
if field_timestamp is None:
query = query.filter(
start_time__isnull=True,
field_timestamp__isnull=True,
)
time_clause = "AND T.START_TIME IS NULL AND T.FIELD_TIMESTAMP IS NULL"
order_clause = "ORDER BY T.FIELD_TIMESTAMP DESC"
else:
query = query.filter(field_timestamp__lte=field_timestamp)
return query.order_by("-field_timestamp")[:group_size]
params["field_timestamp"] = field_timestamp
time_clause = "AND T.FIELD_TIMESTAMP <= %(field_timestamp)s"
order_clause = "ORDER BY T.FIELD_TIMESTAMP DESC"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be T._TIMESTAMP instead of T.FIELD_TIMESTAMP here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I think both the frontend and tests do dot give us this case.
I will fix and include some tests to catch it.

Comment on lines +125 to +128
cursor.execute("SET LOCAL enable_nestloop = off")
cursor.execute(query, params)
rows = dict_fetchall(cursor)
set_query_cache(key=cache_key, params=params, rows=rows)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see some EXPLAIN ANALYZE with this turned on and off attached in the PR, just to ensure we aren't missing anything here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the regular queries (which are already fast) the benefit, if it exists should be minimal (with some chance of even being slightly worse).
I am including one of the slowest queries, where the benefit can be seen. The main problem is that the regular query offered by the planner is bad in its worst case scenarios.

Example obtained with this query

Before analyze-nested.md
After analyze-no-nested.md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More regular case from this query

Before analyze-nested-small.md
After analyze-no-nested-small.md

  * Moving to a raw SQL query, instead of ORM.
  * Force the query to use a hash based comparison, instead of a slower
    nested loop

    Closes kernelci#1131
@alanpeixinho alanpeixinho force-pushed the fix/improve-test-status-history-performance branch from 2861582 to 8887737 Compare April 27, 2026 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backend Most or all of the changes for this issue will be in the backend code. Queries Issue that involves modifying some DB query

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve test status history query

3 participants