|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +DRY_RUN=false |
| 4 | + |
| 5 | +owner=pymc-devs |
| 6 | +repo=pymc |
| 7 | +issue_number=7686 |
| 8 | +title="Speed up test times :rocket:" |
| 9 | +workflow=tests |
| 10 | +contributing_url="https://www.pymc.io/projects/docs/en/stable/contributing/index.html" |
| 11 | +action_url="https://github.com/$owner/$repo/blob/main/.github/workflows/slow-tests-issue.yml" |
| 12 | +latest_id=$(gh run list --limit 30 --workflow $workflow --status success --json databaseId,startedAt,updatedAt --jq ' |
| 13 | +. | map({ |
| 14 | + databaseId: .databaseId, |
| 15 | + startedAt: .startedAt, |
| 16 | + updatedAt: .updatedAt, |
| 17 | + minutes: (((.updatedAt | fromdate) - (.startedAt | fromdate)) / 60) |
| 18 | +} | select(.minutes > 10)) |
| 19 | +| .[0].databaseId |
| 20 | +') |
| 21 | +gh api /repos/$owner/$repo/actions/runs/$latest_id/jobs --jq ' |
| 22 | + .jobs |
| 23 | + | map({name, id, run_id, node_id, started_at, completed_at}) |
| 24 | +' > tmp.json |
| 25 | + |
| 26 | +# Skip 3.10, float32, and Benchmark tests |
| 27 | +function skip_job() { |
| 28 | + name=$1 |
| 29 | + # if [[ $name == *"py3.10"* ]]; then |
| 30 | + # return 0 |
| 31 | + # fi |
| 32 | + # |
| 33 | + # if [[ $name == *"float32 1"* ]]; then |
| 34 | + # return 0 |
| 35 | + # fi |
| 36 | + # |
| 37 | + # if [[ $name == *"Benchmark"* ]]; then |
| 38 | + # return 0 |
| 39 | + # fi |
| 40 | + |
| 41 | + return 1 |
| 42 | +} |
| 43 | + |
| 44 | +# Remove common prefix from the name |
| 45 | +function remove_prefix() { |
| 46 | + name=$1 |
| 47 | + echo $name |
| 48 | + # echo $name | sed -e 's/^ubuntu-latest test py3.12 numpy>=2.0 : fast-compile 0 : float32 0 : //' |
| 49 | +} |
| 50 | + |
| 51 | +function human_readable_time() { |
| 52 | + started_at=$1 |
| 53 | + completed_at=$2 |
| 54 | + |
| 55 | + start_seconds=$(date -d "$started_at" +%s) |
| 56 | + end_seconds=$(date -d "$completed_at" +%s) |
| 57 | + |
| 58 | + seconds=$(($end_seconds - $start_seconds)) |
| 59 | + |
| 60 | + if [ $seconds -lt 60 ]; then |
| 61 | + echo "$seconds seconds" |
| 62 | + else |
| 63 | + echo "$(date -u -d @$seconds +'%-M minutes %-S seconds')" |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +all_times="" |
| 69 | +cat tmp.json | jq -c '.[]' | while IFS= read -r job; do |
| 70 | + id=$(printf '%s' "$job" | jq -r '.id') |
| 71 | + name=$(printf '%s' "$job" | jq -r '.name') |
| 72 | + run_id=$(printf '%s' "$job" | jq -r '.run_id') |
| 73 | + started_at=$(printf '%s' "$job" | jq -r '.started_at') |
| 74 | + completed_at=$(printf '%s' "$job" | jq -r '.completed_at') |
| 75 | + |
| 76 | + if skip_job $name; then |
| 77 | + echo "Skipping $name" |
| 78 | + continue |
| 79 | + fi |
| 80 | + |
| 81 | + echo "Processing job: $name (ID: $id, Run ID: $run_id)" |
| 82 | + |
| 83 | + # Seeing a bit more stabilty with the API rather than the CLI |
| 84 | + # https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#download-job-logs-for-a-workflow-run |
| 85 | + times=$(gh api /repos/$owner/$repo/actions/jobs/$id/logs | python extract-slow-tests.py) |
| 86 | + # times=$(gh run view --job $id --log | python extract-slow-tests.py) |
| 87 | + |
| 88 | + if [ -z "$times" ]; then |
| 89 | + # Some of the jobs are non-test jobs, so we skip them |
| 90 | + echo "No tests found for '$name', skipping" |
| 91 | + continue |
| 92 | + fi |
| 93 | + |
| 94 | + echo $times |
| 95 | + |
| 96 | + human_readable=$(human_readable_time $started_at $completed_at) |
| 97 | + name=$(remove_prefix $name) |
| 98 | + |
| 99 | + top="<details><summary>($human_readable) $name</summary>\n\n\n\`\`\`" |
| 100 | + bottom="\`\`\`\n\n</details>" |
| 101 | + |
| 102 | + formatted_times="$top\n$times\n$bottom" |
| 103 | + |
| 104 | + if [ -n "$all_times" ]; then |
| 105 | + all_times="$all_times\n$formatted_times" |
| 106 | + else |
| 107 | + all_times="$formatted_times" |
| 108 | + fi |
| 109 | +done |
| 110 | + |
| 111 | +if [ -z "$all_times" ]; then |
| 112 | + echo "No slow tests found, exiting" |
| 113 | + exit 1 |
| 114 | +fi |
| 115 | + |
| 116 | +run_date=$(date +"%Y-%m-%d") |
| 117 | +body=$(cat << EOF |
| 118 | +If you are motivated to help speed up some tests, we would appreciate it! |
| 119 | +
|
| 120 | +Here are some of the slowest test times: |
| 121 | +
|
| 122 | +$all_times |
| 123 | +
|
| 124 | +You can find more information on how to contribute [here]($contributing_url) |
| 125 | +
|
| 126 | +Automatically generated by [GitHub Action]($action_url) |
| 127 | +Latest run date: $run_date |
| 128 | +Run logs: [$latest_id](https://github.com/$owner/$repo/actions/runs/$latest_id) |
| 129 | +EOF |
| 130 | +) |
| 131 | + |
| 132 | +if [ "$DRY_RUN" = true ]; then |
| 133 | + echo "Dry run, not updating issue" |
| 134 | + echo $body |
| 135 | + exit |
| 136 | +fi |
| 137 | + |
| 138 | +echo $body | gh issue edit $issue_number --body-file - --title "$title" |
| 139 | +echo "Updated issue $issue_number with all times" |
0 commit comments