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