1*4882a593Smuzhiyun# 2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 3*4882a593Smuzhiyun# 4*4882a593Smuzhiyun 5*4882a593Smuzhiyunfrom django.core.management.base import BaseCommand 6*4882a593Smuzhiyunfrom django.test.client import Client 7*4882a593Smuzhiyunimport os, sys, re 8*4882a593Smuzhiyunimport requests 9*4882a593Smuzhiyunfrom django.conf import settings 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun# pylint: disable=E1103 12*4882a593Smuzhiyun# Instance of 'WSGIRequest' has no 'status_code' member 13*4882a593Smuzhiyun# (but some types could not be inferred) (maybe-no-member) 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun 16*4882a593Smuzhiyunclass Command(BaseCommand): 17*4882a593Smuzhiyun help = "Test the response time for all toaster urls" 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun def handle(self, *args, **options): 20*4882a593Smuzhiyun root_urlconf = __import__(settings.ROOT_URLCONF) 21*4882a593Smuzhiyun patterns = root_urlconf.urls.urlpatterns 22*4882a593Smuzhiyun global full_url 23*4882a593Smuzhiyun for pat in patterns: 24*4882a593Smuzhiyun if pat.__class__.__name__ == 'RegexURLResolver': 25*4882a593Smuzhiyun url_root_res = str(pat).split('^')[1].replace('>', '') 26*4882a593Smuzhiyun if 'gui' in url_root_res: 27*4882a593Smuzhiyun for url_patt in pat.url_patterns: 28*4882a593Smuzhiyun full_url = self.get_full_url(url_patt, url_root_res) 29*4882a593Smuzhiyun info = self.url_info(full_url) 30*4882a593Smuzhiyun status_code = info[0] 31*4882a593Smuzhiyun load_time = info[1] 32*4882a593Smuzhiyun print('Trying \'' + full_url + '\', ' + str(status_code) + ', ' + str(load_time)) 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun def get_full_url(self, url_patt, url_root_res): 35*4882a593Smuzhiyun full_url = str(url_patt).split('^')[1].replace('$>', '').replace('(?P<file_path>(?:/[', '/bin/busybox').replace('.*', '') 36*4882a593Smuzhiyun full_url = str(url_root_res + full_url) 37*4882a593Smuzhiyun full_url = re.sub('\(\?P<.*?>\\\d\+\)', '1', full_url) 38*4882a593Smuzhiyun full_url = 'http://localhost:8000/' + full_url 39*4882a593Smuzhiyun return full_url 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun def url_info(self, full_url): 42*4882a593Smuzhiyun client = Client() 43*4882a593Smuzhiyun info = [] 44*4882a593Smuzhiyun try: 45*4882a593Smuzhiyun resp = client.get(full_url, follow = True) 46*4882a593Smuzhiyun except Exception as e_status_code: 47*4882a593Smuzhiyun self.error('Url: %s, error: %s' % (full_url, e_status_code)) 48*4882a593Smuzhiyun resp = type('object', (), {'status_code':0, 'content': str(e_status_code)}) 49*4882a593Smuzhiyun status_code = resp.status_code 50*4882a593Smuzhiyun info.append(status_code) 51*4882a593Smuzhiyun try: 52*4882a593Smuzhiyun req = requests.get(full_url) 53*4882a593Smuzhiyun except Exception as e_load_time: 54*4882a593Smuzhiyun self.error('Url: %s, error: %s' % (full_url, e_load_time)) 55*4882a593Smuzhiyun load_time = req.elapsed 56*4882a593Smuzhiyun info.append(load_time) 57*4882a593Smuzhiyun return info 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun def error(self, *args): 60*4882a593Smuzhiyun for arg in args: 61*4882a593Smuzhiyun print(arg, end=' ', file=sys.stderr) 62*4882a593Smuzhiyun print(file=sys.stderr) 63