-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_artifacts.py
95 lines (73 loc) · 2.53 KB
/
analyze_artifacts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import argparse
import asyncio
import io
import json
import os
import statistics
import zipfile
from collections import defaultdict
import aiohttp
from gidgethub.aiohttp import GitHubAPI
from gidgethub.sansio import accept_format
GH_API = "https://api.github.com"
GH_OWNER = "reizio"
GH_REPO = "benchmarks"
GH_USERNAME = "ReeseBot"
# A variant of gidgethub.getiter with the support for
# custom element iterators (e.g artifacts)
async def getiter(gh, url, identifier, **url_vars):
data = b""
accept = accept_format()
data, more = await gh._make_request("GET", url, url_vars, data, accept)
if isinstance(data, dict) and identifier in data:
data = data[identifier]
for item in data:
yield item
if more:
async for item in getiter(gh, more, identifier, **url_vars):
yield item
async def collect_reports(gh, session, auth):
total_count = 0
async for artifact in getiter(
gh,
f"/repos/{GH_OWNER}/{GH_REPO}/actions/artifacts",
"artifacts",
per_page=100,
):
async with session.get(
f"{GH_API}/repos/{GH_OWNER}/{GH_REPO}/actions"
f"/artifacts/{artifact['id']}/zip",
auth=auth,
) as resp:
buffer = io.BytesIO(await resp.read())
zip_obj = zipfile.ZipFile(buffer)
assert len(zip_obj.filelist) == 1
assert zip_obj.filelist[0].filename == "benchmark_report.json"
with zip_obj.open("benchmark_report.json") as stream:
report = json.load(stream)
yield artifact["updated_at"], report
def process_results(original_results):
results = defaultdict(dict)
for date, report in original_results.items():
for key, timings in report.items():
results[key][date] = statistics.fmean(timings)
return results
async def runner(data_file):
auth = aiohttp.BasicAuth(GH_USERNAME, os.getenv("GITHUB_TOKEN"))
async with aiohttp.ClientSession() as session:
gh = GitHubAPI(
session, "ReeseBot", oauth_token=os.getenv("GITHUB_TOKEN")
)
results = {
date: report
async for date, report in collect_reports(gh, session, auth)
}
with open(data_file, "w") as stream:
json.dump(process_results(results), stream, indent=4)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data-file", type=str, default="static/results.json")
options = parser.parse_args()
asyncio.run(runner(options.data_file))
if __name__ == "__main__":
main()