mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
8.8 KiB
272 lines
8.8 KiB
10 years ago
|
#!/usr/bin/python
|
||
10 years ago
|
#
|
||
|
# Copyright 2015, Google Inc.
|
||
|
# All rights reserved.
|
||
|
#
|
||
|
# Redistribution and use in source and binary forms, with or without
|
||
|
# modification, are permitted provided that the following conditions are
|
||
|
# met:
|
||
|
#
|
||
|
# * Redistributions of source code must retain the above copyright
|
||
|
# notice, this list of conditions and the following disclaimer.
|
||
|
# * Redistributions in binary form must reproduce the above
|
||
|
# copyright notice, this list of conditions and the following disclaimer
|
||
|
# in the documentation and/or other materials provided with the
|
||
|
# distribution.
|
||
|
# * Neither the name of Google Inc. nor the names of its
|
||
|
# contributors may be used to endorse or promote products derived from
|
||
|
# this software without specific prior written permission.
|
||
|
#
|
||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
#
|
||
10 years ago
|
|
||
|
import os
|
||
|
import sys
|
||
|
import re
|
||
|
import urllib2
|
||
|
import urllib
|
||
|
import json
|
||
|
import time
|
||
|
import subprocess
|
||
10 years ago
|
import fnmatch
|
||
10 years ago
|
|
||
|
CLIENT_ID = '1018396037782-tv81fshn76nemr24uuhuginceb9hni2m.apps.googleusercontent.com'
|
||
|
CLIENT_SECRET = '_HGHXg4DAA59r4w4x8p6ARzD'
|
||
|
GRANT_TYPE = 'http://oauth.net/grant_type/device/1.0'
|
||
|
ACCESS_TOKENS_DIR = '/tmp/auth_lead_access_tokens'
|
||
|
AUTH_TOKEN_LINK = 'https://www.googleapis.com/oauth2/v3/token'
|
||
|
GOOGLE_ACCOUNTS_LINK = 'https://accounts.google.com/o/oauth2/device/code'
|
||
|
USER_INFO_LINK = 'https://www.googleapis.com/oauth2/v1/userinfo'
|
||
|
|
||
10 years ago
|
# Fetches JSON reply object, given a url and parameters
|
||
10 years ago
|
def fetchJSON(url, paramDict):
|
||
|
if len(paramDict) == 0:
|
||
|
req = urllib2.Request(url)
|
||
|
else:
|
||
|
data = urllib.urlencode(paramDict)
|
||
|
req = urllib2.Request(url, data)
|
||
|
|
||
|
try:
|
||
|
response = urllib2.urlopen(req)
|
||
|
result = response.read()
|
||
10 years ago
|
|
||
10 years ago
|
except urllib2.HTTPError, error:
|
||
|
result = error.read()
|
||
|
|
||
|
return result
|
||
|
|
||
10 years ago
|
# Fetch user info; used to check if access token is valid
|
||
10 years ago
|
def getUserInfo(accessToken):
|
||
|
url = USER_INFO_LINK + '?access_token=' + accessToken
|
||
|
paramDict = {}
|
||
|
JSONBody = fetchJSON(url, paramDict)
|
||
|
data = json.loads(JSONBody)
|
||
10 years ago
|
|
||
10 years ago
|
return data
|
||
|
|
||
10 years ago
|
# Returns true if stored access token is valid
|
||
10 years ago
|
def isAccessTokenValid(accessToken):
|
||
|
data = getUserInfo(accessToken);
|
||
|
|
||
|
if 'id' in data:
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
10 years ago
|
# Returns user id given a working access token
|
||
10 years ago
|
def getUserId(accessToken):
|
||
|
data = getUserInfo(accessToken)
|
||
|
|
||
|
email = data['email']
|
||
10 years ago
|
userId = getUserIdFromEmail(email)
|
||
10 years ago
|
|
||
|
return userId
|
||
|
|
||
10 years ago
|
# Extracts a unique user id from an email address
|
||
|
def getUserIdFromEmail(email):
|
||
|
email = email.split('@')[0].lower() # take username and convert to lower case
|
||
|
userId = re.sub('[.]', '', email) # remove periods
|
||
|
|
||
|
return userId
|
||
|
|
||
|
# Use an existing access token
|
||
10 years ago
|
def useAccessToken(userTokFile):
|
||
|
with open(userTokFile, "r") as data_file:
|
||
10 years ago
|
data = json.load(data_file) # load JSON data from file
|
||
10 years ago
|
accessToken = data["access_token"]
|
||
|
|
||
10 years ago
|
# If access token has gone stale, refresh it
|
||
10 years ago
|
if not isAccessTokenValid(accessToken):
|
||
|
return refreshAccessToken(data["refresh_token"], userTokFile)
|
||
|
|
||
|
return accessToken
|
||
|
|
||
10 years ago
|
# refresh stale access token
|
||
10 years ago
|
def refreshAccessToken(refreshToken, userTokFile):
|
||
10 years ago
|
# Parameters for request
|
||
10 years ago
|
paramDict = {'refresh_token':refreshToken, 'client_id':CLIENT_ID, 'client_secret':CLIENT_SECRET, 'grant_type':'refresh_token'}
|
||
10 years ago
|
# Fetch reply to request
|
||
10 years ago
|
JSONBody = fetchJSON(AUTH_TOKEN_LINK, paramDict)
|
||
|
data = json.loads(JSONBody)
|
||
10 years ago
|
|
||
10 years ago
|
if not 'access_token' in data:
|
||
10 years ago
|
# Refresh token has gone stale, re-authentication required
|
||
10 years ago
|
return reauthenticate()
|
||
|
else:
|
||
10 years ago
|
# write fresh access token to tokens file
|
||
10 years ago
|
tokenData = {}
|
||
|
|
||
|
with open(userTokFile, "r") as data_file:
|
||
|
tokenData = json.load(data_file)
|
||
|
|
||
|
with open(userTokFile, "w") as data_file:
|
||
|
tokenData['access_token'] = data['access_token']
|
||
|
json.dump(tokenData, data_file)
|
||
10 years ago
|
|
||
|
# return fresh access token
|
||
10 years ago
|
return data['access_token']
|
||
|
|
||
|
def reauthenticate():
|
||
10 years ago
|
# Request parameters
|
||
10 years ago
|
paramDict = {'client_id':CLIENT_ID, 'scope':'email profile'}
|
||
|
JSONBody = fetchJSON(GOOGLE_ACCOUNTS_LINK, paramDict)
|
||
|
data = json.loads(JSONBody)
|
||
|
|
||
|
print 'User authorization required\n'
|
||
10 years ago
|
print 'Please use the following code in you browser: ', data['user_code'] # Code to be entered by user in browser
|
||
|
print 'Verification URL: ', data['verification_url'] # Authentication link
|
||
10 years ago
|
print '\nAwaiting user authorization. May take a few more seconds after authorizing...\n'
|
||
|
|
||
|
authData = {}
|
||
|
|
||
|
while not 'access_token' in authData:
|
||
10 years ago
|
# Request parameters
|
||
10 years ago
|
authDict = {'client_id':CLIENT_ID, 'client_secret':CLIENT_SECRET, 'code':data['device_code'], 'grant_type':GRANT_TYPE}
|
||
|
JSONBody = fetchJSON(AUTH_TOKEN_LINK, authDict)
|
||
|
authData = json.loads(JSONBody)
|
||
10 years ago
|
# If server pinged too quickly, will get slowdown message; need to wait for specified interval
|
||
10 years ago
|
time.sleep(data['interval'])
|
||
|
|
||
10 years ago
|
# File to write tokens
|
||
10 years ago
|
newUserTokFile = ACCESS_TOKENS_DIR + '/' + getUserId(authData['access_token'])
|
||
|
|
||
10 years ago
|
# Write tokens to file
|
||
10 years ago
|
with open(newUserTokFile, "w") as data_file:
|
||
|
json.dump(authData, data_file)
|
||
|
|
||
10 years ago
|
# return working access token
|
||
10 years ago
|
return authData['access_token']
|
||
|
|
||
10 years ago
|
# Fetch a working access token given user entered email id; authntication may be required
|
||
|
def getAccessToken(email):
|
||
|
# Get unique user id from email address
|
||
|
userId = getUserIdFromEmail(email)
|
||
10 years ago
|
|
||
10 years ago
|
# Token file
|
||
10 years ago
|
userTokFile = ACCESS_TOKENS_DIR + '/' + userId
|
||
|
|
||
|
accessToken = ''
|
||
|
|
||
|
if os.path.exists(userTokFile):
|
||
10 years ago
|
# File containing access token exists; unless refresh token has expired, user authentication will not be required
|
||
10 years ago
|
accessToken = useAccessToken(userTokFile)
|
||
|
else:
|
||
10 years ago
|
# User authentication required
|
||
10 years ago
|
accessToken = reauthenticate()
|
||
|
|
||
10 years ago
|
return accessToken
|
||
|
|
||
|
# If user has not entered full path to test, recursively searches for given test in parent folders
|
||
|
def findTestPath(test):
|
||
|
# If user entered full path to test, return it
|
||
|
if(os.path.isfile(test)):
|
||
|
return test
|
||
|
|
||
|
testName = test.split('/')[-1] # Extract just test name
|
||
|
testPath = ''
|
||
|
|
||
|
# Search for test
|
||
|
for root, dirnames, filenames in os.walk('../../../'):
|
||
10 years ago
|
for fileName in fnmatch.filter(filenames, testName):
|
||
|
testPath = os.path.join(root, fileName)
|
||
10 years ago
|
|
||
|
return testPath
|
||
|
|
||
10 years ago
|
def getSysInfo():
|
||
|
# Fetch system information
|
||
|
sysInfo = os.popen('lscpu').readlines()
|
||
|
|
||
|
NICs = os.popen('ifconfig | cut -c1-8 | sed \'/^\s*$/d\' | sort -u').readlines()
|
||
|
nicAddrs = os.popen('ifconfig | grep -oE "inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}"').readlines()
|
||
|
|
||
|
nicInfo = []
|
||
|
|
||
|
for i in range(0, len(NICs)):
|
||
|
NIC = NICs[i]
|
||
|
NIC = re.sub(r'[^\w]', '', NIC)
|
||
|
|
||
|
ethtoolProcess = subprocess.Popen(["ethtool",NIC], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||
|
ethtoolResult = ethtoolProcess.communicate()[0]
|
||
|
|
||
|
ethtoolResultList = ethtoolResult.split('\n\t')
|
||
|
for ethtoolString in ethtoolResultList:
|
||
|
if ethtoolString.startswith('Speed'):
|
||
|
ethtoolString = ethtoolString.split(':')[1]
|
||
|
ethtoolString = ethtoolString.replace('Mb/s',' Mbps')
|
||
|
nicInfo.append(NIC + ' speed: ' + ethtoolString + '\n')
|
||
|
nicInfo.append(NIC + ' inet address: ' + nicAddrs[i].split(':')[1])
|
||
|
|
||
|
print 'Obtaining network info....'
|
||
|
tcp_rr_rate = str(os.popen('netperf -t TCP_RR -v 0').readlines()[1])
|
||
|
print 'Network info obtained'
|
||
|
|
||
|
nicInfo.append('TCP RR Transmission Rate per sec: ' + tcp_rr_rate + '\n')
|
||
|
sysInfo = sysInfo + nicInfo
|
||
|
|
||
|
return sysInfo
|
||
|
|
||
10 years ago
|
def main():
|
||
|
# If tokens directory does not exist, creates it
|
||
|
if not os.path.exists(ACCESS_TOKENS_DIR):
|
||
|
os.makedirs(ACCESS_TOKENS_DIR)
|
||
|
|
||
|
if len(sys.argv) > 1:
|
||
|
test = sys.argv[1]
|
||
|
else:
|
||
|
test = raw_input('Enter the test path/name: ')
|
||
|
|
||
|
if len(sys.argv) > 2:
|
||
|
email = sys.argv[2]
|
||
|
else:
|
||
|
email = raw_input('Enter your e-mail id: ')
|
||
|
|
||
|
try:
|
||
|
# Fetch working access token
|
||
|
accessToken = getAccessToken(email)
|
||
|
except Exception, e:
|
||
|
print 'Error in authentication'
|
||
10 years ago
|
|
||
10 years ago
|
try:
|
||
10 years ago
|
testPath = findTestPath(test) # Get path to test
|
||
|
testName = testPath.split('/')[-1] # Get test name
|
||
|
|
||
10 years ago
|
sysInfo = getSysInfo()
|
||
10 years ago
|
|
||
10 years ago
|
print '\nBeginning test:\n'
|
||
10 years ago
|
# Run the test
|
||
10 years ago
|
subprocess.call([testPath, '--report_metrics_db=true', '--access_token='+accessToken, '--test_name='+testName, '--sys_info='+str(sysInfo).strip('[]')])
|
||
10 years ago
|
except OSError:
|
||
10 years ago
|
print 'Could not execute the test, please check test name'
|
||
|
|
||
10 years ago
|
if __name__ == "__main__":
|
||
|
main()
|