Skip to content

Commit

Permalink
Enable uploading perf-lab-report.json files (dotnet#506)
Browse files Browse the repository at this point in the history
* Enable uploading perf-lab-report.json files

Instead of trying to extract functionality
from the benchview scripts for uploading, add a new script
that does it. Uses AzCopy to copy files up.

* Use a different formatting technqiue due to old python on linux machines

* remove extra call to setup_loggers from testing

* Make code look more like python

Also removed the #! since it wasn't really necessary. This is more of a library script.
  • Loading branch information
billwert authored May 27, 2019
1 parent 9a63ac3 commit cf57c87
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
90 changes: 90 additions & 0 deletions scripts/azcopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""AzCopy"""

import os
from os import makedirs, path, sys
from glob import glob
from logging import getLogger
from tarfile import TarFile
from urllib.request import urlopen
from zipfile import ZipFile

from performance.common import (RunCommand, get_artifacts_directory,
get_tools_directory)


class AzCopy:
"""wrapper for calling AzCopy"""
def __init__(self, sas: str, container_path: str, verbose: bool):
self.container_url = 'https://pvscmdupload.blob.core.windows.net/results/'
self.container_path = container_path
self.sas = sas
self.verbose = verbose

if sys.platform == 'win32':
self.archivename = 'azcopy.zip'
self.exename = 'azcopy.exe'
self.download_url = 'https://aka.ms/downloadazcopy-v10-windows'
else:
self.archivename = 'azcopy.tar.gz'
self.exename = 'azcopy'
self.download_url = 'https://aka.ms/downloadazcopy-v10-linux'

def get_azcopy_directory(self) -> str:
return path.join(get_tools_directory(), 'azcopy')

def archive_path(self) -> str:
return path.join(self.get_azcopy_directory(), self.archivename)

def exe_path(self) -> str:
return path.join(self.get_azcopy_directory(), self.exename)

def get_upload_url(self) -> str:
return "{0}{1}{2}".format(self.container_url, self.container_path, self.sas)

def download_azcopy(self) -> None:
if path.exists(self.exe_path()):
return

getLogger().info('downloading azcopy')
if not path.isdir(self.get_azcopy_directory()):
makedirs(self.get_azcopy_directory())

with urlopen(self.download_url) as response, open(self.archive_path(), 'wb') as zipfile:
zipfile.write(response.read())

if sys.platform == 'win32':
with ZipFile(self.archive_path()) as zipfile:
item, = (zipfile for zipfile in zipfile.infolist() if zipfile.filename.endswith('.exe'))
item.filename = path.basename(item.filename)
zipfile.extract(item, self.get_azcopy_directory())
else:
tar = TarFile.open(self.archive_path())
item, = (tar for tar in tar.getmembers() if tar.name.endswith('azcopy'))
item.name = path.basename(item.name)
tar.extract(item, self.get_azcopy_directory())
tar.close()

def upload_files(self, search_path: str):
self.download_azcopy()
cmdline = [
self.exe_path(), 'copy', search_path, self.get_upload_url(), '--recursive=true'
]
RunCommand(cmdline, verbose=self.verbose).run()

@staticmethod
def upload_results(container_path: str, verbose: bool) -> None:
if os.getenv('PERFLAB_UPLOAD_TOKEN'):
files = glob(path.join(
get_artifacts_directory(),
'**',
'*perf-lab-report.json'), recursive=True)
if files:
dirname = path.dirname(files[0])
if len(files) == 1:
# need to work around a bug in azcopy which loses file name if
# there is only one file.
# https://github.com/Azure/azure-storage-azcopy/issues/410
container_path = path.join(container_path, path.basename(files[0]))
AzCopy(os.environ['PERFLAB_UPLOAD_TOKEN'],
container_path,
verbose).upload_files(path.join(dirname, '*perf-lab-report.json'))
12 changes: 12 additions & 0 deletions scripts/benchmarks_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import benchview
import dotnet
import micro_benchmarks
from azcopy import AzCopy


if sys.platform == 'linux' and "linux_distribution" not in dir(platform):
Expand Down Expand Up @@ -142,6 +143,12 @@ def __is_valid_datetime(dt: str) -> str:
"%%Y-%%m-%%dT%%H:%%M:%%SZ").'''
)

parser.add_argument('--upload-to-perflab-container',
dest="upload_to_perflab_container",
required=False,
help="Container to upload perf lab results to."
)

# Generic arguments.
parser.add_argument(
'-q', '--quiet',
Expand Down Expand Up @@ -259,6 +266,11 @@ def __main(args: list) -> int:
)

benchview.run_scripts(args, verbose, BENCHMARKS_CSPROJ)

if(args.upload_to_perflab_container):
AzCopy.upload_results(args.upload_to_perflab_container, verbose)


# TODO: Archive artifacts.


Expand Down

0 comments on commit cf57c87

Please sign in to comment.