feat(test): report more metrics when running go benchmarks

This commit is contained in:
Łukasz Mierzwa
2020-05-22 17:31:17 +01:00
committed by Łukasz Mierzwa
parent 9b0e92fb8f
commit 9563b9119d
12 changed files with 296 additions and 141 deletions

View File

@@ -5,7 +5,6 @@ from collections import namedtuple
import json
import os
import sys
import urllib2
Bundle = namedtuple('Bundle' , 'seq ext bundleName totalBytes files')
@@ -160,26 +159,12 @@ def summarize(diffs):
sign=sign, totalDiff=humanize(totalDiff))
def postComment(diffs, pr, token, owner='prymitive', repo='karma'):
api = 'api.github.com'
uri = 'https://{api}/repos/{owner}/{repo}/issues/{pr}/comments'.format(
api=api, owner=owner, repo=repo, pr=pr)
def printDiff(diffs):
rows = []
for diff in diffs:
rows.append(printBundleDiff(diff))
summary = summarize(diffs)
data = '*Webpack bundle size diff*\n{summary}\n{rows}'.format(
summary=summary, rows='\n'.join(rows))
encoded = json.dumps({'body': data})
req = urllib2.Request(uri)
req.add_header('Authorization', 'token %s' % token)
req.add_header("Content-Type", "application/json")
try:
response = urllib2.urlopen(req, encoded)
except Exception as e:
print("Request to '%s' failed: %s" % (uri, e))
print('{summary}\n{rows}'.format(summary=summary, rows='\n'.join(rows)))
def diffBundles(a, b):
@@ -231,24 +216,12 @@ if __name__ == '__main__':
print('Usage: PATH1 PATH2')
sys.exit(1)
token = os.getenv('GITHUB_TOKEN')
pr = os.getenv('TRAVIS_PULL_REQUEST')
bundleA = readBundle(sys.argv[1])
bundleB = readBundle(sys.argv[2])
if not token:
print('GITHUB_TOKEN env variable is missing')
sys.exit(1)
if not pr:
print('TRAVIS_PULL_REQUEST env variable is missing')
sys.exit(1)
if not bundleA or not bundleB:
print('Usage: PATH1 PATH2')
sys.exit(1)
diffs = diffBundles(bundleA, bundleB)
if diffs:
print('Found diffs, posting to GitHub')
postComment(diffs, pr, token)
else:
print('No diff found')
printDiff(diffs)

92
scripts/pr-comment.py Executable file
View File

@@ -0,0 +1,92 @@
#!/usr/bin/env python
import json
import os
import sys
import urllib2
def apiRequest(token, path, owner='prymitive', repo='karma'):
api = 'api.github.com'
uri = 'https://{api}/repos/{owner}/{repo}{path}'.format(
api=api, owner=owner, repo=repo, pr=pr, path=path)
req = urllib2.Request(uri)
req.add_header('Authorization', 'token %s' % token)
req.add_header("Content-Type", "application/json")
return req
def findComment(pr, token, commentName):
req = apiRequest(token, "/issues/{pr}/comments".format(pr=pr))
try:
response = urllib2.urlopen(req)
except Exception as e:
print("Request to '%s' failed: %s" % (uri, e))
else:
comments = json.load(response)
for comment in comments:
if comment['body'].startswith(commentName):
return comment['id']
def formatComment(commentName, comment):
return '{name}\n\n{body}'.format(name=commentName, body=comment)
def editComment(pr, token, commentID, commentName, comment):
req = apiRequest(token, "/issues/comments/{id}".format(id=commentID))
req.get_method = lambda: 'PATCH'
print('Editing a comment on {uri}'.format(uri=req.get_full_url()))
try:
response = urllib2.urlopen(
req, json.dumps({"body": formatComment(commentName, comment)}))
except Exception as e:
print("Failed to updated comment '%s': %s" % (req.get_full_url(), e))
def postComment(pr, token, commentName, comment):
req = apiRequest(token, "/issues/{pr}/comments".format(pr=pr))
print('Posting new comment to {uri}'.format(uri=req.get_full_url()))
try:
response = urllib2.urlopen(
req, json.dumps({"body": formatComment(commentName, comment)}))
except Exception as e:
print("Failed to create a comment '%s': %s" % (req.get_full_url(), e))
if __name__ == '__main__':
if len(sys.argv) != 4:
print('Usage: NAME PATH FORMAT')
sys.exit(1)
token = os.getenv('GITHUB_TOKEN')
pr = os.getenv('TRAVIS_PULL_REQUEST')
commentName = sys.argv[1]
commentFile = sys.argv[2]
format = sys.argv[3]
if not token:
print('GITHUB_TOKEN env variable is missing')
sys.exit(1)
if not pr:
print('TRAVIS_PULL_REQUEST env variable is missing')
sys.exit(1)
if not commentName or not commentFile or not format:
print('Usage: NAME PATH FORMAT')
sys.exit(1)
with open(commentFile) as f:
comment = f.read()
if format == 'noformat':
comment = '```\n{body}\n```'.format(body=comment)
commentID = findComment(pr, token, commentName)
if commentID is None:
postComment(pr, token, commentName, comment)
else:
editComment(pr, token, commentID, commentName, comment)