Skip to content

Commit

Permalink
Manage Slow Tests Issue (pymc-labs#1160)
Browse files Browse the repository at this point in the history
* add scripts

* rename

* add GitHub action

* use source instead

* update to link the action

* update the body

* update the permissions

* fix a view action syntax
  • Loading branch information
wd60622 authored Nov 4, 2024
1 parent 273eb76 commit ca15e9a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/slow-tests-issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
name: Slow Tests Issue Body

on:
schedule:
- cron: '0 */6 * * *'

permissions:
issues: write

jobs:
update-comment:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.11"
- name: Trigger the script
working-directory: scripts/slowest_tests
run: source update-slowest-times-issue.sh
env:
GITHUB_TOKEN: ${{ github.token }}
74 changes: 74 additions & 0 deletions scripts/slowest_tests/extract-slow-tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""This script parses the github action log for test times."""

import re
import sys
from pathlib import Path

start_pattern = re.compile(r"==== slow")
separator_pattern = re.compile(r"====")
time_pattern = re.compile(r"(\d+\.\d+)s ")


def extract_lines(lines: list[str]) -> list[str]:
times = []

in_section = False
for line in lines:
detect_start = start_pattern.search(line)
detect_end = separator_pattern.search(line)

if detect_start:
in_section = True

if in_section:
times.append(line)

if not detect_start and in_section and detect_end:
break

return times


def trim_up_to_match(pattern, string: str) -> str:
match = pattern.search(string)
if not match:
return ""

return string[match.start() :]


def trim(pattern, lines: list[str]) -> list[str]:
return [trim_up_to_match(pattern, line) for line in lines]


def strip_ansi(text: str) -> str:
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
return ansi_escape.sub("", text)


def format_times(times: list[str]) -> list[str]:
return (
trim(separator_pattern, times[:1])
+ trim(time_pattern, times[1:-1])
+ [strip_ansi(line) for line in trim(separator_pattern, times[-1:])]
)


def read_lines_from_stdin():
return sys.stdin.read().splitlines()


def read_from_file(file: Path):
return file.read_text().splitlines()


def main(read_lines):
lines = read_lines()
times = extract_lines(lines)
parsed_times = format_times(times)
print("\n".join(parsed_times))


if __name__ == "__main__":
read_lines = read_lines_from_stdin
main(read_lines)
55 changes: 55 additions & 0 deletions scripts/slowest_tests/update-slowest-times-issue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

DRY_RUN=false

owner=pymc-labs
repo=pymc-marketing
issue_number=1158
title="Speed up test times :rocket:"
workflow=Test
latest_id=$(gh run list --workflow $workflow --status completed --limit 1 --json databaseId --jq '.[0].databaseId')
jobs=$(gh api /repos/$owner/$repo/actions/runs/$latest_id/jobs --jq '.jobs | map({name: .name, run_id: .run_id, id: .id})')

all_times=""
echo "$jobs" | jq -c '.[]' | while read -r job; do
id=$(echo $job | jq -r '.id')
name=$(echo $job | jq -r '.name')
run_id=$(echo $job | jq -r '.run_id')

echo "Processing job: $name (ID: $id, Run ID: $run_id)"
times=$(gh run view --job $id --log | python extract-slow-tests.py)

top="<details><summary>$name</summary>\n\n\n\`\`\`"
bottom="\`\`\`\n\n</details>"

formatted_times="$top\n$times\n$bottom"

if [ -n "$all_times" ]; then
all_times="$all_times\n$formatted_times"
else
all_times="$formatted_times"
fi
done

run_date=$(date +"%Y-%m-%d")
body=$(cat << EOF
If you are motivated to help speed up some tests, we would appreciate it!
Here are some of the slowest test times:
$all_times
You can find more information on how to contribute [here](https://www.pymc-marketing.io/en/stable/contributing/index.html).
Automatically generated by [GitHub Action](https://github.com/pymc-labs/pymc-marketing/blob/main/.github/workflows/slow-tests-issue.yml)
Latest run date: $run_date
EOF
)

if [ "$DRY_RUN" = true ]; then
echo "Dry run, not updating issue"
echo $body
exit
fi
echo $body | gh issue edit $issue_number --body-file - --title "$title"
echo "Updated issue $issue_number with all times"

0 comments on commit ca15e9a

Please sign in to comment.