Skip to content

Instantly share code, notes, and snippets.

@gsemwezi
Created February 20, 2014 12:12
Show Gist options
  • Save gsemwezi/9112211 to your computer and use it in GitHub Desktop.
Save gsemwezi/9112211 to your computer and use it in GitHub Desktop.
import string
import subprocess
def print_statistics_for_requests(name, failed_requests, latency_request_list):
latency_request_list_size = len(latency_request_list)
average_latency = get_average_latency(latency_request_list)
max_latency = max(latency_request_list)
min_latency = min(latency_request_list)
failed_requests = failed_requests - latency_request_list_size
successful_requests = latency_request_list_size
print "Statistics for " + name
print latency_request_list
print 'Average latency:' + str(average_latency)
print 'Max latency:' + str(max_latency)
print 'Min latency:' + str(min_latency)
print 'Successful: ' + str(successful_requests)
print 'Failures: ' + str(failed_requests)
def run_test():
number_of_requests = 5
failed_requests = number_of_requests
latency_request_list= []
command = 'curl -o /dev/null -s -w %{http_code},%{time_total} "http://127.0.0.1/router/receive?backend=console&sender=3&message=no"'
while(number_of_requests > 0):
shell_response = execute_curl_command(command)
latency = get_latency(shell_response)
if (latency != 0):
latency_request_list.append(latency)
number_of_requests = number_of_requests - 1
print_statistics_for_requests("Curl Tests", failed_requests, latency_request_list)
uwsgi_latency_list = get_latency_times_from_uwsgi()
print_statistics_for_requests("Uwsgi Tests", failed_requests, uwsgi_latency_list)
code_latency_list = get_latency_times_from_code_analysis()
print_statistics_for_requests("Code Tests",failed_requests,code_latency_list)
def get_latency_times_from_uwsgi():
latency_list = []
command ='curl -s -i "http://127.0.0.1/__profiler__" | grep "<td>[0-9]*\.[0-9]*</td>"'
shell_response = execute_curl_command(command)
for response in shell_response.stdout.readlines():
latency_list.append(float(string.replace(string.replace(response,'<td>',''),'</td>','')))
return latency_list
def get_latency_times_from_code_analysis():
latency_list = []
command = "grep \"views.py.*(receive)\" my_profile.log | awk '{ print $4;}'"
shell_response = execute_curl_command(command)
for response in shell_response.stdout.readlines():
latency_list.append(float(response))
return latency_list
def execute_curl_command(command):
return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def get_latency(shell_response):
latency = 0
result = shell_response.stdout.readlines()[0].split(',')
if(result[0] == '200'):
latency = float(result[1])
return latency
def get_average_latency(latency_request_list):
return sum(latency_request_list) / len(latency_request_list)
run_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment