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