Skip to content

Commit 2abb766

Browse files
committed
add scripts
1 parent 6fbfac8 commit 2abb766

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""This script parses the GitHub action log for test times.
2+
3+
Taken from https://github.com/pymc-labs/pymc-marketing/tree/main/scripts/slowest_tests/extract-slow-tests.py
4+
5+
"""
6+
7+
import re
8+
import sys
9+
from pathlib import Path
10+
11+
12+
start_pattern = re.compile(r"==== slow")
13+
separator_pattern = re.compile(r"====")
14+
time_pattern = re.compile(r"(\d+\.\d+)s ")
15+
16+
17+
def extract_lines(lines: list[str]) -> list[str]:
18+
times = []
19+
20+
in_section = False
21+
for line in lines:
22+
detect_start = start_pattern.search(line)
23+
detect_end = separator_pattern.search(line)
24+
25+
if detect_start:
26+
in_section = True
27+
28+
if in_section:
29+
times.append(line)
30+
31+
if not detect_start and in_section and detect_end:
32+
break
33+
34+
return times
35+
36+
37+
def trim_up_to_match(pattern, string: str) -> str:
38+
match = pattern.search(string)
39+
if not match:
40+
return ""
41+
42+
return string[match.start() :]
43+
44+
45+
def trim(pattern, lines: list[str]) -> list[str]:
46+
return [trim_up_to_match(pattern, line) for line in lines]
47+
48+
49+
def strip_ansi(text: str) -> str:
50+
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
51+
return ansi_escape.sub("", text)
52+
53+
54+
def format_times(times: list[str]) -> list[str]:
55+
return (
56+
trim(separator_pattern, times[:1])
57+
+ trim(time_pattern, times[1:-1])
58+
+ [strip_ansi(line) for line in trim(separator_pattern, times[-1:])]
59+
)
60+
61+
62+
def read_lines_from_stdin():
63+
return sys.stdin.read().splitlines()
64+
65+
66+
def read_from_file(file: Path):
67+
"""For testing purposes."""
68+
return file.read_text().splitlines()
69+
70+
71+
def main(read_lines):
72+
lines = read_lines()
73+
times = extract_lines(lines)
74+
parsed_times = format_times(times)
75+
print("\n".join(parsed_times))
76+
77+
78+
if __name__ == "__main__":
79+
read_lines = read_lines_from_stdin
80+
main(read_lines)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/zsh
2+
3+
DRY_RUN=false
4+
5+
owner=pymc-devs
6+
repo=pytensor
7+
issue_number=1124
8+
title="Speed up test times :rocket:"
9+
workflow=Tests
10+
latest_id=$(gh run list --workflow $workflow --status success --limit 1 --json databaseId --jq '.[0].databaseId')
11+
jobs=$(gh api /repos/$owner/$repo/actions/runs/$latest_id/jobs --jq '.jobs | map({name: .name, run_id: .run_id, id: .id})')
12+
13+
all_times=""
14+
echo "$jobs" | jq -c '.[]' | while read -r job; do
15+
id=$(echo $job | jq -r '.id')
16+
name=$(echo $job | jq -r '.name')
17+
run_id=$(echo $job | jq -r '.run_id')
18+
19+
echo "Processing job: $name (ID: $id, Run ID: $run_id)"
20+
times=$(gh run view --job $id --log | python extract-slow-tests.py)
21+
22+
if [ -z "$times" ]; then
23+
# Some of the jobs are non-test jobs, so we skip them
24+
continue
25+
fi
26+
27+
echo $times
28+
29+
top="<details><summary>$name</summary>\n\n\n\`\`\`"
30+
bottom="\`\`\`\n\n</details>"
31+
32+
formatted_times="$top\n$times\n$bottom"
33+
34+
if [ -n "$all_times" ]; then
35+
all_times="$all_times\n$formatted_times"
36+
else
37+
all_times="$formatted_times"
38+
fi
39+
done
40+
41+
run_date=$(date +"%Y-%m-%d")
42+
body=$(cat << EOF
43+
If you are motivated to help speed up some tests, we would appreciate it!
44+
45+
Here are some of the slowest test times:
46+
47+
$all_times
48+
49+
You can find more information on how to contribute [here](https://pytensor.readthedocs.io/en/latest/dev_start_guide.html)
50+
51+
Automatically generated by [GitHub Action](https://github.com/pymc-devs/pytensor/blob/main/.github/workflows/slow-tests-issue.yml)
52+
Latest run date: $run_date
53+
EOF
54+
)
55+
56+
if [ "$DRY_RUN" = true ]; then
57+
echo "Dry run, not updating issue"
58+
echo $body
59+
exit
60+
fi
61+
echo $body | gh issue edit $issue_number --body-file - --title "$title"
62+
echo "Updated issue $issue_number with all times"

0 commit comments

Comments
 (0)