How to use median_response_time method in locust

Best Python code snippet using locust

savannahStatistics.py

Source:savannahStatistics.py Github

copy

Full Screen

1#!/usr/bin/env python262import sys3import re4import time5import urllib6import codecs7from xml.dom import minidom8from datetime import datetime9from math import sqrt, fabs10from calendar import timegm11from optparse import OptionParser12#Allows to write utf-8 when redirecting output13sys.stdout = codecs.getwriter('utf-8')(sys.stdout)14#print u'%35s %50s %6s' % ("Squad", "Time to close a ticket".center(50), "Closed tickets")15#print u'%35s %25s %25s %6s' % ("", "Median".center(25), "Average".center(25), "")16#print u'%35s %10.2f \u00B1 %6.2f %5s %10.2f \u00B1 %6.2f %13i' % ("cmscompinfrasup-t2plcracow", 0.054, 0.02, "", 0.29, 0.09, 27)17#sys.exit()18def getText(nodelist):19 rc = []20 for node in nodelist:21 if node.nodeType == node.TEXT_NODE:22 rc.append(node.data)23 return ''.join(rc)24def getTag(node,tagname):25 return getText(node.getElementsByTagName(tagname)[0].childNodes)26def getTimeOfFirstMeaningfulResponse(item):27 submitter = getTag(item, 'submitted_by')28 history = item.getElementsByTagName('history')29 if len(history) > 0:30 backup_date = 031 first_event_date = 032 events = history[0].getElementsByTagName('event')33 for event in events:34 if first_event_date == 0:35 first_event_date = float(getTag(event, 'date'))36 fields = event.getElementsByTagName('field')37 for field in fields:38 name = getTag(field, 'field_name')39 modified_by = getTag(field, 'modified_by')40 if modified_by != submitter and name == 'Original_Submission':41 return float(getTag(event, 'date'))42 elif backup_date == 0 and name == 'Original_Submission':43 backup_date = float(getTag(event, 'date'))44 if backup_date != 0:45 return backup_date46 else:47 return first_event_date48 return float(getTag(item, 'submitted_on'))49def median(x):50 if len(x) < 1:51 raise Exception('Empty array')52 if len(x) == 1:53 return x[0]54 x = sorted(x)55 if len(x) % 2 == 0:56 return (x[len(x)/2] + x[len(x)/2 - 1])/257 return x[(len(x) - 1)/2]58def mad(x, m):59 deviations = []60 for i in x:61 deviations.append(fabs(i - m))62 return median(deviations)63def meanstdev(x):64 if len(x) < 1:65 raise Exception('Empty array')66 if len(x) == 1:67 return (x[0], 0.0)68 i = 069 M = 0.070 S = 0.071 for value in x:72 i += 173 delta = value - M74 M += delta/i75 S += delta*(value - M)76 stdev = sqrt(S/(i-1))77 mean = M78 return (mean, stdev)79current = datetime.utcnow()80current_unix = timegm(current.timetuple())81parser = OptionParser(add_help_option=False)82parser.add_option("-h", "--help", action="help")83parser.add_option("-i", "--input", dest="inFile",84 help="(Optional) Input XML file, under http, containing all the savannah information")85parser.add_option("-o", "--output", dest="outFile",86 help="(Mandatory) HTML file containing the output of this script")87parser.add_option("-d", "--days", dest="openedInTheLastNDays", type="int",88 help="(Mandatory) Number of days to look at")89(options, args) = parser.parse_args()90if options.outFile == None:91 parser.error("You must provide the output file name (use -o option) or consult --help\n")92if options.inFile == None:93 url='https://savannah.cern.ch/export/cmscompinfrasup/gutsche/535.xml'94 input = urllib.urlopen(url)95else:96 input = urllib.urlopen(options.inFile)97if options.openedInTheLastNDays == None:98 parser.error("You must provide the number of days to look at")99outf = open(options.outFile, 'w')100RE_XML_ILLEGAL = u'([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])' + \101 u'|' + \102 u'([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])' % \103 (unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),104 unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),105 unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff))106x = re.sub(RE_XML_ILLEGAL, "?", input.read()) # takes ages107# Remove broken control unicode chars108brokenControl = '\xc2([^\x80-\xbf])'109x = re.sub(brokenControl, '?\g<1>', x)110xmldoc = minidom.parseString(x)111savanaexport = xmldoc.getElementsByTagName('savaneexport')[0]112items = savanaexport.getElementsByTagName('item')113squads = {}114tickets = {}115tickets = {'close_times': [], 'response_times': [], 'median_close_time': 0.0, 'median_response_time': 0.0, 'close_time_error': 0.0, 'response_time_error': 0.0, 'avg_close_time': 0.0, 'avg_response_time': 0.0, 'stdev_close_time': 0.0, 'stdev_response_time': 0.0}116for item in items:117 squad = getTag(item, 'assigned_to')118 submitted_on = float(getTag(item, 'submitted_on'))119 closed_on = float(getTag(item, 'closed_on'))120 first_response_on = getTimeOfFirstMeaningfulResponse(item)121 if (current_unix - submitted_on) > (options.openedInTheLastNDays*86400):122 continue123 if squad not in squads.keys():124 squads[squad] = {'close_times': [], 'response_times': [], 'median_close_time': 0.0, 'median_response_time': 0.0, 'close_time_error': 0.0, 'response_time_error': 0.0, 'avg_close_time': 0.0, 'avg_response_time': 0.0, 'stdev_close_time': 0.0, 'stdev_response_time': 0.0}125 time_until_closed = closed_on - submitted_on126 time_until_first_response = first_response_on - submitted_on127 squads[squad]['close_times'].append(time_until_closed)128 squads[squad]['response_times'].append(time_until_first_response)129 tickets['close_times'].append(time_until_closed)130 tickets['response_times'].append(time_until_first_response)131tickets['median_close_time'] = median(tickets['close_times'])132tickets['median_response_time'] = median(tickets['response_times'])133tickets['close_time_error'] = mad(tickets['close_times'], tickets['median_close_time'])134tickets['response_time_error'] = mad(tickets['response_times'], tickets['median_response_time'])135(tickets['avg_close_time'], tickets['stdev_close_time']) = meanstdev(tickets['close_times'])136(tickets['avg_response_time'], tickets['stdev_response_time']) = meanstdev(tickets['response_times'])137for squad in squads.keys():138 squads[squad]['median_close_time'] = median(squads[squad]['close_times'])139 squads[squad]['median_response_time'] = median(squads[squad]['response_times'])140 squads[squad]['close_time_error'] = mad(squads[squad]['close_times'], squads[squad]['median_close_time'])141 squads[squad]['response_time_error'] = mad(squads[squad]['response_times'], squads[squad]['median_response_time'])142 (squads[squad]['avg_close_time'], squads[squad]['stdev_close_time']) = meanstdev(squads[squad]['close_times'])143 (squads[squad]['avg_response_time'], squads[squad]['stdev_response_time']) = meanstdev(squads[squad]['response_times'])144outf.write('<html>\n')145outf.write('<pre>\n')146outf.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n')147outf.write('--------------------------------------------------------------------------------\n')148outf.write('Report generated on ' + str(current) + ' UTC\n')149outf.write('--------------------------------------------------------------------------------\n')150outf.write('--------------------------------------------------------------------------------\n')151outf.write('Summary: average time to close tickets per squad\n')152outf.write('--------------------------------------------------------------------------------\n')153outf.write(u'%35s %50s %6s' % ("Squad", "Time to close a ticket".center(50), "Closed tickets\n"))154outf.write(u'%35s %25s %25s %6s' % ("", "Median".center(25), "Average".center(25), "\n"))155sorted_squads = sorted(squads.items(), key = lambda x: x[1]['median_close_time'], reverse = True)156for (squad,d) in sorted_squads:157 error_close_time_squad = squads[squad]['close_time_error']/86400158 median_close_time_squad = squads[squad]['median_close_time']/86400159 stdev_close_time_squad = squads[squad]['stdev_close_time']/86400160 mean_close_time_squad = squads[squad]['avg_close_time']/86400161 outstr = u'%35s %10.2f \u00B1 %6.2f %5s %10.2f \u00B1 %6.2f %13i\n' % (squad.replace("cmscompinfrasup-",""), median_close_time_squad, error_close_time_squad, "", mean_close_time_squad, stdev_close_time_squad, len(squads[squad]['close_times']))162 outf.write(outstr.encode('utf8'))163outf.write('--------------------------------------------------------------------------------\n')164outf.write('Summary: average time to first response to a ticket per squad\n')165outf.write('--------------------------------------------------------------------------------\n')166outf.write(u'%35s %50s %6s' % ("Squad", "Time to respond to a ticket".center(50), "Closed tickets\n"))167outf.write(u'%35s %25s %25s %6s' % ("", "Median".center(25), "Average".center(25), "\n"))168sorted_squads = sorted(squads.items(), key = lambda x: x[1]['median_response_time'], reverse = True)169for (squad,d) in sorted_squads:170 error_response_time_squad = squads[squad]['response_time_error']/86400171 median_response_time_squad = squads[squad]['median_response_time']/86400172 stdev_response_time_squad = squads[squad]['stdev_response_time']/86400173 mean_response_time_squad = squads[squad]['avg_response_time']/86400174 outstr = u'%35s %10.2f \u00B1 %6.2f %5s %10.2f \u00B1 %6.2f %13i\n' % (squad.replace("cmscompinfrasup-",""), median_response_time_squad, error_response_time_squad, "", mean_response_time_squad, stdev_response_time_squad, len(squads[squad]['response_times']))175 outf.write(outstr.encode('utf8'))176outf.write('--------------------------------------------------------------------------------\n')177outf.write('Summary: closed tickets summary\n')178outf.write('--------------------------------------------------------------------------------\n')179error_response_time_total = tickets['response_time_error']/86400180error_close_time_total = tickets['close_time_error']/86400181median_close_time_total = tickets['median_close_time']/86400182median_response_time_total = tickets['median_response_time']/86400183stdev_response_time_total = tickets['stdev_response_time']/86400184stdev_close_time_total = tickets['stdev_close_time']/86400185mean_close_time_total = tickets['avg_close_time']/86400186mean_response_time_total = tickets['avg_response_time']/86400187outf.write(unicode('Total number of closed tickets: %i\n' % (len(tickets['close_times']))))188outf.write((u'Median time to close a ticket: %6.2f \u00B1 %6.2f days\n' % (median_close_time_total, error_close_time_total)).encode('utf8'))189outf.write((u'Median time to the first response to a ticket: %6.2f \u00B1 %6.2f days\n' % (median_response_time_total, error_response_time_total)).encode('utf8'))190outf.write((u'Average time to close a ticket: %6.2f \u00B1 %6.2f days\n' % (mean_close_time_total, stdev_close_time_total)).encode('utf8'))191outf.write((u'Average time to the first response to a ticket: %6.2f \u00B1 %6.2f days\n' %(mean_response_time_total, stdev_response_time_total)).encode('utf8'))192outf.write('</pre>\n')193outf.write('</html>\n')...

Full Screen

Full Screen

ResultHandler.py

Source:ResultHandler.py Github

copy

Full Screen

1import os23from Utility import mkdir4from Variable import *567class _ResultFileHandler(object):8 def __init__(self):9 super(_ResultFileHandler, self).__init__()1011 @staticmethod12 def save(file_path, data):13 folder = os.path.dirname(file_path)14 mkdir(folder)15 with open(file_path, "wb") as f:16 if isinstance(data, list):17 f.write("\n".join(data).encode('utf-8'))18 else:19 f.write("{}\n".format(data).encode('utf-8'))202122class Analysis(_ResultFileHandler):23 def __init__(self):24 super(Analysis, self).__init__()25 self.ResponseTimes = []2627 def parse(self, file_path, maximum_response_time=True, minimum_response_time=True, average_response_time=True,28 median_response_time=True):29 if os.path.exists(file_path) and os.path.isfile(file_path):30 pass31 else:32 return {}33 result = {}34 with open(file_path, "rb") as f:35 lines = f.readlines()36 if not lines:37 # empty file38 return {}39 for line in lines:40 row = eval(line)41 status = row["status"]42 if status == PASS:43 self.ResponseTimes.append(row["ResponseTime"])44 total_rows_count = len(lines)45 first_row = eval(lines[0])46 last_row = eval(lines[-1])47 start_time = first_row["createTime"]48 end_time = last_row["createTime"]49 duration = end_time - start_time50 if not duration:51 # Duration is 052 duration = 153 throughput = len(self.ResponseTimes)54 speed = float(throughput) / float(duration)55 failure_count = total_rows_count - throughput56 failure_ratio = (1 - float(throughput) / float(total_rows_count)) * 10057 result.update({"Throughput": throughput,58 "Duration": duration,59 "Speed": speed,60 "FailureCount": failure_count,61 "FailureRatio": "{}%%".format(failure_ratio)})6263 if self.ResponseTimes:6465 response_time_exceeded_3s_count = 066 response_time_exceeded_10s_count = 067 response_time_exceeded_30s_count = 068 for response_time in self.ResponseTimes:69 if response_time >= 3:70 response_time_exceeded_3s_count += 171 if response_time >= 10:72 response_time_exceeded_10s_count += 173 if response_time >= 30:74 response_time_exceeded_30s_count += 175 result.update({"ResponseTime_Exceeded_3s_Count": response_time_exceeded_3s_count,76 "ResponseTime_Exceeded_10s_Count": response_time_exceeded_10s_count,77 "ResponseTime_Exceeded_30s_Count": response_time_exceeded_30s_count,78 "ResponseTime_Exceeded_3s_Ratio": "{} %%".format(79 response_time_exceeded_3s_count / throughput * 100),80 "ResponseTime_Exceeded_10s_Ratio": "{} %%".format(81 response_time_exceeded_10s_count / throughput * 100),82 "ResponseTime_Exceeded_30s_Ratio": "{} %%".format(83 response_time_exceeded_30s_count / throughput * 100),84 })8586 if self.ResponseTimes and maximum_response_time:87 maximum_response_time = max(self.ResponseTimes)88 result.update({"MaximumResponseTime": maximum_response_time})89 if self.ResponseTimes and minimum_response_time:90 minimum_response_time = min(self.ResponseTimes)91 result.update({"MinimumResponseTime": minimum_response_time})92 if self.ResponseTimes and average_response_time:93 average_response_time = sum(self.ResponseTimes) / len(self.ResponseTimes)94 result.update({"AverageResponseTime": average_response_time})95 if self.ResponseTimes and median_response_time:96 self.ResponseTimes.sort()97 median_response_time = self.ResponseTimes[int(len(self.ResponseTimes) / 2)]98 result.update({"MedianResponseTime": median_response_time})99 return result100101102class ResultParser(_ResultFileHandler):103 def __init__(self):104 super(ResultParser, self).__init__()105106 @staticmethod107 def show(file_path):108 outputs = []109 with open(file_path, "rb") as f:110 lines = f.readlines()111 if lines:112 for line in lines:113 result = eval(line)114115 case_id = result["caseId"]116 average_response_time = "{} sec".format(117 result["AverageResponseTime"] if "AverageResponseTime" in result else "NA")118 median_response_time = "{} sec".format(119 result["MedianResponseTime"] if "MedianResponseTime" in result else "NA")120 maximum_response_time = "{} sec".format(121 result["MaximumResponseTime"] if "MaximumResponseTime" in result else "NA")122 minimum_response_time = "{} sec".format(123 result["MinimumResponseTime"] if "MinimumResponseTime" in result else "NA")124 # ResponseTime_Exceeded_3s_Count = result["ResponseTime_Exceeded_3s_Count"] if result.has_key("ResponseTime_Exceeded_3s_Count") else "NA"125 response_time_exceeded_3s_ratio = result[126 "ResponseTime_Exceeded_3s_Ratio"] if "ResponseTime_Exceeded_3s_Ratio" in result else "NA"127 # ResponseTime_Exceeded_10s_Count = result["ResponseTime_Exceeded_10s_Count"] if result.has_key("ResponseTime_Exceeded_10s_Count") else "NA"128 response_time_exceeded_10s_ratio = result[129 "ResponseTime_Exceeded_10s_Ratio"] if "ResponseTime_Exceeded_10s_Ratio" in result else "NA"130 # ResponseTime_Exceeded_30s_Count = result["ResponseTime_Exceeded_30s_Count"] if result.has_key("ResponseTime_Exceeded_30s_Count") else "NA"131 response_time_exceeded_30s_ratio = result[132 "ResponseTime_Exceeded_30s_Ratio"] if "ResponseTime_Exceeded_30s_Ratio" in result else "NA"133 failure_count = result["FailureCount"]134 failure_ratio = result["FailureRatio"]135 throughput = result["Throughput"]136 speed = result["Speed"]137 duration = result["Duration"]138 outputs.append("*" * 20)139 outputs.append("caseId : {}{}{}".format(PRINT_GREEN, case_id, PRINT_END))140 outputs.append("Duration : {} sec".format(duration))141 outputs.append("Throughput : {}{}{}".format(PRINT_GREEN, throughput, PRINT_END))142 outputs.append("Speed : {} /s".format(speed))143 outputs.append("Failure Count : {}{}{}".format(PRINT_RED, failure_count, PRINT_END))144 outputs.append("Failure Ratio : {}{}{}".format(PRINT_RED, failure_ratio, PRINT_END))145 outputs.append("Response Time : [{} ~ {}]".format(minimum_response_time, maximum_response_time))146 outputs.append("Average Response Time : {}".format(average_response_time))147 outputs.append("Median Response Time : {}".format(median_response_time))148 outputs.append(149 "Response Time > 3 sec : {}{}{}".format(PRINT_RED, response_time_exceeded_3s_ratio, PRINT_END))150 outputs.append(151 "Response Time > 10 sec : {}{}{}".format(PRINT_RED, response_time_exceeded_10s_ratio, PRINT_END))152 outputs.append(153 "Response Time > 30 sec : {}{}{}".format(PRINT_RED, response_time_exceeded_30s_ratio, PRINT_END)) ...

Full Screen

Full Screen

analyze.py

Source:analyze.py Github

copy

Full Screen

1from get_data import times2from datetime import datetime3import numpy as np4from aerosandbox.tools.pretty_plots import mpl, plt, sns, show_plot5sns.set_style('ticks')6### Clean the data7times = np.array(times)8valid_range = (9 datetime(year=1900, month=1, day=1, hour=18, minute=42),10 datetime(year=1900, month=1, day=1, hour=21)11)12plot_range = (13 datetime(year=1900, month=1, day=1, hour=18, minute=30),14 datetime(year=1900, month=1, day=1, hour=21)15)16times = times[np.logical_and(times > valid_range[0], times < valid_range[1])]17### Set a time format18time_formatter = mpl.dates.DateFormatter("%I:%M %p")19### Compute statistics about the data20email_send_time = datetime(year=1900, month=1, day=1, hour=18, minute=43)21median_latency = np.median(np.array([22 time - email_send_time for time in times23]))24median_response_time = email_send_time + median_latency25### Plot the data26fig, ax = plt.subplots(figsize=(8, 6))27# Plot the responses28sns.histplot(29 times,30 bins=30,31 kde=True32)33# Plot the email send time34plt.axvline(x=email_send_time, ls='--', color="k")35plt.text(36 x=email_send_time,37 y=0.9 * ax.get_ylim()[1] + (1 - 0.9) * ax.get_ylim()[0],38 s=f"Email sent ({email_send_time.time().__format__('%I:%M %p')})",39 color="k",40 horizontalalignment='right',41 verticalalignment='top',42 rotation=9043)44# Plot the median response45plt.axvline(x=median_response_time, ls='--', color="k")46plt.text(47 x=median_response_time,48 y=0.9 * ax.get_ylim()[1] + (1 - 0.9) * ax.get_ylim()[0],49 s=f"Median receipt time\n({median_response_time.time().__format__('%I:%M %p')}, {int(np.round(median_latency.seconds / 60))} min)",50 color="k",51 horizontalalignment='right',52 verticalalignment='top',53 rotation=9054)55# Format and show56ax.xaxis.set_major_formatter(time_formatter)57ax.xaxis.set_major_locator(mpl.dates.MinuteLocator([0, 30]))58ax.xaxis.set_minor_locator(mpl.dates.MinuteLocator([0, 10, 20, 30, 40, 50]))59plt.xlim(*plot_range)60plt.gcf().autofmt_xdate(rotation=45, ha='center')61show_plot(62 f"Email Latency of free-food@mit.edu and free-foods@mit.edu (N={len(times)})",63 "Receipt Time",64 pretty_grids=False,65 show=False66)67plt.savefig("plot.png", dpi=300)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful