本文整理汇总了Python中locust.stats.RequestStats类的典型用法代码示例。如果您正苦于以下问题:Python RequestStats类的具体用法?Python RequestStats怎么用?Python RequestStats使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RequestStats类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request_stats
def request_stats():
global _request_stats_context_cache
if not _request_stats_context_cache or _request_stats_context_cache[
"last_time"
] < time() - _request_stats_context_cache.get("cache_time", DEFAULT_CACHE_TIME):
cache_time = _request_stats_context_cache.get("cache_time", DEFAULT_CACHE_TIME)
now = time()
stats = []
for s in chain(_sort_stats(runners.locust_runner.request_stats), [RequestStats.sum_stats("Total")]):
stats.append(
{
"method": s.method,
"name": s.name,
"num_reqs": s.num_reqs,
"num_failures": s.num_failures,
"avg_response_time": s.avg_response_time,
"min_response_time": s.min_response_time,
"max_response_time": s.max_response_time,
"current_rps": s.current_rps,
"median_response_time": s.median_response_time,
"avg_content_length": s.avg_content_length,
}
)
report = {"stats": stats, "errors": list(runners.locust_runner.errors.iteritems())}
if stats:
report["total_rps"] = stats[len(stats) - 1]["current_rps"]
report["fail_ratio"] = RequestStats.sum_stats("Total").fail_ratio
# since generating a total response times dict with all response times from all
# urls is slow, we make a new total response time dict which will consist of one
# entry per url with the median response time as key and the number of requests as
# value
response_times = defaultdict(int) # used for calculating total median
for i in xrange(len(stats) - 1):
response_times[stats[i]["median_response_time"]] += stats[i]["num_reqs"]
# calculate total median
stats[len(stats) - 1]["median_response_time"] = median_from_dict(
stats[len(stats) - 1]["num_reqs"], response_times
)
is_distributed = isinstance(runners.locust_runner, MasterLocustRunner)
if is_distributed:
report["slave_count"] = runners.locust_runner.slave_count
report["state"] = runners.locust_runner.state
report["user_count"] = runners.locust_runner.user_count
elapsed = time() - now
cache_time = max(
cache_time, elapsed * 2.0
) # Increase cache_time when report generating starts to take longer time
_request_stats_context_cache = {"last_time": elapsed - now, "report": report, "cache_time": cache_time}
else:
report = _request_stats_context_cache["report"]
return json.dumps(report)
示例2: test_percentile
def test_percentile(self):
s = RequestStats("GET", "percentile_test")
for x in xrange(100):
s.log(x, 0)
self.assertEqual(s.get_response_time_percentile(0.5), 50)
self.assertEqual(s.get_response_time_percentile(0.6), 60)
self.assertEqual(s.get_response_time_percentile(0.95), 95)
示例3: test_request_stats_content_length
def test_request_stats_content_length(self):
class MyLocust(Locust):
host = "http://127.0.0.1:%i" % self.port
locust = MyLocust()
locust.client.get("/ultra_fast")
self.assertEqual(RequestStats.get("GET", "/ultra_fast").avg_content_length, len("This is an ultra fast response"))
locust.client.get("/ultra_fast")
self.assertEqual(RequestStats.get("GET", "/ultra_fast").avg_content_length, len("This is an ultra fast response"))
示例4: test_request_connection_error
def test_request_connection_error(self):
class MyLocust(Locust):
host = "http://localhost:1"
locust = MyLocust()
response = locust.client.get("/", timeout=0.1)
self.assertFalse(response)
self.assertEqual(1, RequestStats.get("GET", "/").num_failures)
self.assertEqual(0, RequestStats.get("GET", "/").num_reqs)
示例5: test_request_stats_query_variables
def test_request_stats_query_variables(self):
class MyLocust(Locust):
host = "http://127.0.0.1:%i" % self.port
locust = MyLocust()
locust.client.get("/ultra_fast?query=1")
self.assertEqual(1, RequestStats.get("GET", "/ultra_fast?query=1").num_reqs)
示例6: test_request_stats_named_endpoint
def test_request_stats_named_endpoint(self):
class MyLocust(Locust):
host = "http://127.0.0.1:%i" % self.port
locust = MyLocust()
locust.client.get("/ultra_fast", name="my_custom_name")
self.assertEqual(1, RequestStats.get("GET", "my_custom_name").num_reqs)
示例7: test_request_stats_no_content_length_no_prefetch
def test_request_stats_no_content_length_no_prefetch(self):
class MyLocust(Locust):
host = "http://127.0.0.1:%i" % self.port
l = MyLocust()
path = "/no_content_length"
r = l.client.get(path, prefetch=False)
self.assertEqual(0, RequestStats.get("GET", path).avg_content_length)
示例8: test_request_stats_no_content_length
def test_request_stats_no_content_length(self):
class MyLocust(Locust):
host = "http://127.0.0.1:%i" % self.port
l = MyLocust()
path = "/no_content_length"
r = l.client.get(path)
self.assertEqual(RequestStats.get("GET", path).avg_content_length, len("This response does not have content-length in the header"))
示例9: distribution_stats_csv
def distribution_stats_csv():
rows = [",".join((
'"Name"',
'"# requests"',
'"50%"',
'"66%"',
'"75%"',
'"80%"',
'"90%"',
'"95%"',
'"98%"',
'"99%"',
'"100%"',
))]
for s in chain(_sort_stats(runners.locust_runner.request_stats), [RequestStats.sum_stats("Total", full_request_history=True)]):
if s.num_reqs:
rows.append(s.percentile(tpl='"%s",%i,%i,%i,%i,%i,%i,%i,%i,%i,%i'))
else:
rows.append('"%s",0,"N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A"' % s.name)
response = make_response("\n".join(rows))
file_name = "distribution_{0}.csv".format(time())
disposition = "attachment;filename={0}".format(file_name)
response.headers["Content-type"] = "text/csv"
response.headers["Content-disposition"] = disposition
return response
示例10: request_stats_csv
def request_stats_csv():
from core import locust_runner
rows = [
",".join([
'"Name"',
'"# requests"',
'"# failures"',
'"Median response time"',
'"Average response time"',
'"Min response time"',
'"Max response time"',
'"Average Content-Length"',
'"Reqests/s"',
])
]
for s in chain(_sort_stats(locust_runner.request_stats), [RequestStats.sum_stats("Total", full_request_history=True)]):
rows.append('"%s",%i,%i,%i,%i,%i,%i,%i,%.2f' % (
s.name,
s.num_reqs,
s.num_failures,
s.median_response_time,
s.avg_response_time,
s.min_response_time or 0,
s.avg_content_length,
s.max_response_time,
s.total_rps,
))
response = make_response("\n".join(rows))
response.headers["Content-type"] = "text/csv"
return response
示例11: test_error_grouping_errors_with_memory_addresses
def test_error_grouping_errors_with_memory_addresses(self):
# reset stats
self.stats = RequestStats()
class Dummy(object):
pass
self.stats.log_error("GET", "/", Exception("Error caused by %r" % Dummy()))
assert 1 == len(self.stats.errors)
示例12: test_log_request_name_argument
def test_log_request_name_argument(self):
from locust.stats import RequestStats
self.response = ""
class MyLocust(Locust):
tasks = []
host = "http://127.0.0.1:%i" % self.port
@task()
def t1(l):
self.response = l.client.get("/ultra_fast", name="new name!")
my_locust = MyLocust()
my_locust.t1()
self.assertEqual(1, RequestStats.get("GET", "new name!").num_reqs)
self.assertEqual(0, RequestStats.get("GET", "/ultra_fast").num_reqs)
示例13: test_serialize_through_message
def test_serialize_through_message(self):
"""
Serialize a RequestStats instance, then serialize it through a Message,
and unserialize the whole thing again. This is done "IRL" when stats are sent
from slaves to master.
"""
s1 = RequestStats("GET", "test")
s1.log(10, 0)
s1.log(20, 0)
s1.log(40, 0)
u1 = RequestStats.unserialize(s1.serialize())
data = Message.unserialize(Message("dummy", s1.serialize(), "none").serialize()).data
u1 = RequestStats.unserialize(data)
self.assertEqual(20, u1.median_response_time)
示例14: test_max_requests
def test_max_requests(self):
class MyTaskSet(TaskSet):
@task
def my_task(self):
self.client.get("/ultra_fast")
class MyLocust(Locust):
host = "http://127.0.0.1:%i" % self.port
task_set = MyTaskSet
min_wait = 1
max_wait = 1
try:
from locust.exception import StopLocust
RequestStats.clear_all()
RequestStats.global_max_requests = 2
l = MyLocust()
self.assertRaises(StopLocust, lambda: l.task_set(l).run())
self.assertEqual(2, RequestStats.total_num_requests)
RequestStats.clear_all()
RequestStats.global_max_requests = 2
self.assertEqual(0, RequestStats.total_num_requests)
l.run()
self.assertEqual(2, RequestStats.total_num_requests)
finally:
RequestStats.clear_all()
RequestStats.global_max_requests = None
示例15: setUp
def setUp(self):
RequestStats.global_start_time = time.time()
self.s = RequestStats("GET", "test_entry")
self.s.log(45, 0)
self.s.log(135, 0)
self.s.log(44, 0)
self.s.log_error(Exception("dummy fail"))
self.s.log_error(Exception("dummy fail"))
self.s.log(375, 0)
self.s.log(601, 0)
self.s.log(35, 0)
self.s.log(79, 0)
self.s.log_error(Exception("dummy fail"))