def run_health_check(): """ Performs a self test of the capacity planner service by comparing the predicted output with the expected output for a given request :return: HTTP response """ global LAST_CHECK_RESPONSE global LAST_CHECK_TIME current_time = int(time.time()) if (LAST_CHECK_TIME is None or current_time - LAST_CHECK_TIME >= HEALTH_CHECK_INTERVAL): try: expected_tps = 2482.337454686785 returned_tps = get_regressor().predict_point( ["Passthrough", 100, 10240], method=const.NO_SAMPLING) if round(expected_tps, 2) == round(returned_tps, 2): # Rounding TPS to account for changes with different C compilers LAST_CHECK_RESPONSE = Response(status=const.HTTP_200_OK) else: logger.info( "ML model prediction in health check does not match") LAST_CHECK_RESPONSE = Response( status=const.HTTP_503_SERVICE_UNAVAILABLE) except Exception: logger.exception("Exception in Capacity Planner health check: ") LAST_CHECK_RESPONSE = Response( status=const.HTTP_500_INTERNAL_SERVER_ERROR) LAST_CHECK_TIME = current_time return LAST_CHECK_RESPONSE
def test_max_tps_no_sampling(self, mock_check_output): tps, concurrency = get_regressor().max_tps( ["Passthrough", 10240], method=const.NO_SAMPLING, ) self.assertEqual(1500, tps) self.assertEqual(1, concurrency)
def point_prediction(): """ Return Latency and TPS for a given scenario, message size and concurrency :return TPS and Little's law Latency with HTTP 200: """ if request.method == "POST": method = const.DEFAULT_METHOD sample_count = const.DEFAULT_SAMPLE_COUNT data = request.get_json() if data is None: logger.error("Payload is missing.") return jsonify({"error": "Payload is missing"}), \ const.HTTP_400_BAD_REQUEST try: scenario = data.get(const.SCENARIO) concurrency = data.get(const.CONCURRENCY) message_size = data.get(const.MESSAGE_SIZE) is_valid, error = json_value_validator(scenario=scenario, concurrency=concurrency, message_size=message_size, type="point_pred") if not is_valid: logger.error("Invalid values in JSON request: " "constraint violation: point_pred: " + error) return jsonify({"error": "Invalid Request: "+error}), \ const.HTTP_422_UNPROCESSABLE_ENTITY except Exception as e: logger.exception( "Uncaught exception occurred " "in request validation: ", e) return jsonify({ "error": "Uncaught exception occurred in request " "validation" }), const.HTTP_422_UNPROCESSABLE_ENTITY if const.METHOD in data: if data[const.METHOD] == const.SAMPLING: method = const.SAMPLING try: if not (data.get(const.SAMPLE_COUNT) is None): sample_count = data[const.SAMPLE_COUNT] is_valid, error = json_value_validator( sample_count=sample_count, type="sampling_check") if not is_valid: logger.error("Invalid values in JSON request: " "constraint violation for " "sample_count: point_pred " + error) return jsonify({ "error": "Invalid Request: "+error }), \ const.HTTP_422_UNPROCESSABLE_ENTITY except Exception as e: logger.exception( "Uncaught exception occurred " "in request validation block: ", e) return jsonify({"error": "Uncaught exception " "occurred in request validation"}), \ const.HTTP_422_UNPROCESSABLE_ENTITY try: prediction = get_regressor().predict_point( [scenario.capitalize(), concurrency, message_size], method=method, sample_count=sample_count) tps, latency = formatter(tps=prediction, concurrency=concurrency) # Clear PyMC3 cache memoize.clear_cache() return jsonify(tps=tps, latency=latency) except Exception as e: logger.exception("ML Model Error : point_pred: ", e) return jsonify({"error": "Error during prediction " "or post processing"}), \ const.HTTP_500_INTERNAL_SERVER_ERROR else: return Response(status=const.HTTP_405_METHOD_NOT_ALLOWED)
def test_prediction_no_sampling(self, mock_check_output): tps = get_regressor().predict_point( ["Passthrough", 100, 10240], method=const.NO_SAMPLING) self.assertEqual(1500, tps)