|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Export per-job queue, preparation and verification seconds using the GitHub API. |
| 18 | +
|
| 19 | +Usage: python3 .github/scripts/actions-timings.py --repo apache/shenyu --limit 50 > timings.csv |
| 20 | +Queue includes dependency wait; runner_queue_seconds shows the API-observable runner wait. |
| 21 | +Verification is the duration of test/check steps, including compilation within those steps. |
| 22 | +Image builds, tool installation, deployment, cache handling and post steps are preparation. |
| 23 | +Failed, skipped and cancelled runs remain visible and must not be averaged with successful runs. |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +import concurrent.futures |
| 28 | +import csv |
| 29 | +import datetime |
| 30 | +import json |
| 31 | +import re |
| 32 | +import subprocess |
| 33 | +import sys |
| 34 | +import time |
| 35 | + |
| 36 | +VERIFY = re.compile( |
| 37 | + r"^(?:Run (?:test|Maven tests|E2E Tests|Java 21 compatibility tests|Apache RAT)|" |
| 38 | + r"Build with Maven$|Replay registration contract|Check resolved registry|" |
| 39 | + r"Check Agent|Check License|Check WAT|Verify list/detail|Run native fresh|" |
| 40 | + r"Check harness|Build disposable Admin|Perform CodeQL|Test |Check test result|" |
| 41 | + r"Cluster Test|Require every|checking job status|python3 .*test-)", re.IGNORECASE) |
| 42 | + |
| 43 | + |
| 44 | +def api(path): |
| 45 | + """Retry transient API failures once, respecting the repository's backoff policy.""" |
| 46 | + for attempt in range(2): |
| 47 | + result = subprocess.run(["gh", "api", path], text=True, capture_output=True) |
| 48 | + if result.returncode == 0: |
| 49 | + return json.loads(result.stdout) |
| 50 | + if attempt == 0 and re.search(r"429|5\d\d|timeout|timed out", result.stderr, re.IGNORECASE): |
| 51 | + time.sleep(20 if "429" in result.stderr else 2) |
| 52 | + continue |
| 53 | + raise RuntimeError(result.stderr.strip()) |
| 54 | + |
| 55 | + |
| 56 | +def seconds(start, end): |
| 57 | + """Incomplete/skipped timestamps have no measurable duration.""" |
| 58 | + if not start or not end: |
| 59 | + return 0 |
| 60 | + return max(0, (datetime.datetime.fromisoformat(end.replace("Z", "+00:00")) |
| 61 | + - datetime.datetime.fromisoformat(start.replace("Z", "+00:00"))).total_seconds()) |
| 62 | + |
| 63 | + |
| 64 | +def row(run, job, repo): |
| 65 | + """Partition the job's lifetime, retaining skipped jobs as zero-duration rows.""" |
| 66 | + elapsed = seconds(job.get("started_at"), job.get("completed_at")) |
| 67 | + verification = 0 |
| 68 | + for step in job.get("steps", []): |
| 69 | + if step.get("conclusion") == "skipped": |
| 70 | + continue |
| 71 | + name = step["name"] |
| 72 | + # Build-only jobs use this same label; their tests are disabled explicitly. |
| 73 | + if name == "Build with Maven" and not (run["name"] == "ci" and job["name"] == "pr_build"): |
| 74 | + continue |
| 75 | + if VERIFY.search(name): |
| 76 | + verification += seconds(step.get("started_at"), step.get("completed_at")) |
| 77 | + skipped = job.get("conclusion") == "skipped" |
| 78 | + queue = 0 if skipped else seconds(run["created_at"], job.get("started_at")) |
| 79 | + runner_queue = 0 if skipped else seconds(job.get("created_at"), job.get("started_at")) |
| 80 | + return { |
| 81 | + "repository": repo, "run_id": run["id"], "attempt": run["run_attempt"], |
| 82 | + "workflow": run["name"], "event": run["event"], "sha": run["head_sha"], |
| 83 | + "job_id": job["id"], "job": job["name"], "status": job["status"], |
| 84 | + "conclusion": job.get("conclusion"), "queue_seconds": queue, |
| 85 | + "runner_queue_seconds": runner_queue, "preparation_seconds": max(0, elapsed - verification), |
| 86 | + "verification_seconds": verification, "url": job["html_url"], |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +def collect(repo, run): |
| 91 | + jobs = [] |
| 92 | + page = 1 |
| 93 | + while True: |
| 94 | + response = api(f"repos/{repo}/actions/runs/{run['id']}/jobs?per_page=100&page={page}") |
| 95 | + jobs.extend(response["jobs"]) |
| 96 | + if len(jobs) >= response["total_count"]: |
| 97 | + return [row(run, job, repo) for job in jobs] |
| 98 | + page += 1 |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + parser = argparse.ArgumentParser(description=__doc__) |
| 103 | + parser.add_argument("--repo", default="apache/shenyu") |
| 104 | + parser.add_argument("--limit", type=int, default=50, choices=range(1, 101)) |
| 105 | + parser.add_argument("--run", action="append", type=int, help="Inspect specific run IDs instead of recent runs") |
| 106 | + args = parser.parse_args() |
| 107 | + if args.run: |
| 108 | + runs = [api(f"repos/{args.repo}/actions/runs/{run_id}") for run_id in args.run] |
| 109 | + else: |
| 110 | + runs = api(f"repos/{args.repo}/actions/runs?per_page={args.limit}")["workflow_runs"] |
| 111 | + with concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool: |
| 112 | + results = list(pool.map(lambda run: collect(args.repo, run), runs)) |
| 113 | + rows = [item for group in results for item in group] |
| 114 | + if rows: |
| 115 | + writer = csv.DictWriter(sys.stdout, fieldnames=list(rows[0])) |
| 116 | + writer.writeheader() |
| 117 | + writer.writerows(rows) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments