def request_stats(): stats = [] for s in chain(sort_stats(self.environment.runner.stats.entries), [environment.runner.stats.total]): stats.append({ "method": s.method, "name": s.name, "safe_name": escape(s.name, quote=False), "num_requests": s.num_requests, "num_failures": s.num_failures, "avg_response_time": s.avg_response_time, "min_response_time": 0 if s.min_response_time is None else proper_round(s.min_response_time), "max_response_time": proper_round(s.max_response_time), "current_rps": s.current_rps, "current_fail_per_sec": s.current_fail_per_sec, "median_response_time": s.median_response_time, "ninetieth_response_time": s.get_response_time_percentile(0.9), "avg_content_length": s.avg_content_length, }) errors = [] for e in environment.runner.errors.values(): err_dict = e.to_dict() err_dict["name"] = escape(err_dict["name"]) err_dict["error"] = escape(err_dict["error"]) errors.append(err_dict) # Truncate the total number of stats and errors displayed since a large number of rows will cause the app # to render extremely slowly. Aggregate stats should be preserved. report = {"stats": stats[:500], "errors": errors[:500]} if len(stats) > 500: report["stats"] += [stats[-1]] if stats: report["total_rps"] = stats[len(stats) - 1]["current_rps"] report["fail_ratio"] = environment.runner.stats.total.fail_ratio report["current_response_time_percentile_95"] = environment.runner.stats.total.get_current_response_time_percentile(0.95) report["current_response_time_percentile_50"] = environment.runner.stats.total.get_current_response_time_percentile(0.5) report["current_response_time_percentile_90"] = environment.runner.stats.total.get_current_response_time_percentile(0.9) is_distributed = isinstance(environment.runner, MasterRunner) if is_distributed: workers = [] for worker in environment.runner.clients.values(): workers.append({"id": worker.id, "state": worker.state, "user_count": worker.user_count, "cpu_usage": worker.cpu_usage}) report["workers"] = workers report["state"] = environment.runner.state report["user_count"] = environment.runner.user_count return jsonify(report)
def test_rounding_up(self): self.assertEqual(2, proper_round(1.5)) self.assertEqual(3, proper_round(2.5)) self.assertEqual(4, proper_round(3.5)) self.assertEqual(5, proper_round(4.5)) self.assertEqual(6, proper_round(5.5))
def test_rounding_down(self): self.assertEqual(1, proper_round(1.499999999)) self.assertEqual(5, proper_round(5.499999999)) self.assertEqual(2, proper_round(2.05)) self.assertEqual(3, proper_round(3.05))
def request_stats(): stats = [] for s in chain(sort_stats(self.environment.runner.stats.entries),[environment.runner.stats.total]): stats.append( { "method": s.method, "name": s.name, "safe_name": escape(s.name,quote=False), "num_requests": s.num_requests, "num_failures": s.num_failures, "avg_response_time": s.avg_response_time, "min_response_time": 0 if s.min_response_time is None else proper_round(s.min_response_time), "max_response_time": proper_round(s.max_response_time), "current_rps": s.current_rps, "current_fail_per_sec": s.current_fail_per_sec, "median_response_time": s.median_response_time, "ninetieth_response_time": s.get_response_time_percentile(0.9), "avg_content_length": s.avg_content_length, } ) errors = [] for e in environment.runner.errors.values(): err_dict = e.to_dict() err_dict["name"] = escape(err_dict["name"]) err_dict["error"] = escape(err_dict["error"]) errors.append(err_dict) # Truncate the total number of stats and errors displayed since a large number of rows will cause the app # to render extremely slowly. Aggregate stats should be preserved. report = {"stats": stats[:500],"errors": errors[:500]} if len(stats) > 500: report["stats"] += [stats[-1]] if stats: report["total_rps"] = stats[len(stats) - 1]["current_rps"] report["fail_ratio"] = environment.runner.stats.total.fail_ratio report[ "current_response_time_percentile_95"] = environment.runner.stats.total.get_current_response_time_percentile( 0.95) report[ "current_response_time_percentile_50"] = environment.runner.stats.total.get_current_response_time_percentile( 0.5) is_distributed = isinstance(environment.runner,MasterRunner) if is_distributed: workers = [] missingClientIds=[] for key,worker in environment.runner.clients.items(): if worker.state==runners.STATE_MISSING: missingClientIds.append(key) continue workers.append({ "id": worker.id, "state": worker.state, "user_count": worker.user_count, "cpu_usage": worker.cpu_usage }) # 移除missing的worker for missingClientId in missingClientIds: del environment.runner.clients[missingClientId] report["workers"] = workers report["slaves"] = [{ "slave":x, "clientId": (lambda t:"-" if not t or not t in [w.get("id") for w in workers] else t)(self.workedServser.get(x)), "rectMsg": (lambda t: "" if not t else t)(self.recvMesg.get(x)) } for x in self.etcdt.servAddressList ] # print("environment.runner.state",environment.runner.state) report["state"] = environment.runner.state report["user_count"] = environment.runner.user_count return jsonify(report)