def test_local_output(self): set_local_input_output(True) # Set up the function msg = { "user": "******", "function": "py_func", "py_user": "******", "py_func": "echo", } json_msg = dumps(msg) set_emulator_message(json_msg) # Check output is initially empty self.assertIsNone(get_output()) # Set output and check output_a = b'12345' set_output(output_a) self.assertEqual(get_output(), output_a) # Set output again and check updated output_b = b'666777' set_output(output_b) self.assertEqual(get_output(), output_b)
def test_setting_emulator_message_returns_id(self): msg = { "user": "******", "function": "bar", } json_msg = dumps(msg) actual = set_emulator_message(json_msg) self.assertGreater(actual, 0)
def test_changing_function_clears_local_output(self): set_local_input_output(True) # Set output and check output_a = b'12345' set_output(output_a) self.assertEqual(get_output(), output_a) # Change function msg = { "user": "******", "function": "bar", } json_msg = dumps(msg) set_emulator_message(json_msg) # Check output is now empty self.assertIsNone(get_output())
def execute_main(json_data): # Set up the emulator again (in case we're running in a separate thread) set_emulator_message(dumps(json_data)) user = json_data["py_user"] func = json_data["py_func"] entry = json_data.get("py_entry", "faasm_main") app.logger.info("Executing {}/{} (entry {})".format(user, func, entry)) # Assume function is in the current path module_name = "{}.{}".format(user, func) mod = __import__(module_name, fromlist=[""]) # Get the entry function and invoke entry_function_obj = getattr(mod, entry) entry_function_obj() set_emulator_status(1)
def setUp(self): self.redis = redis.Redis() self.redis.flushall() self.key_a = "matrix_tester_a" self.key_b = "matrix_tester_b" msg = { "user": "******", "function": "py_func", "py_user": "******", "py_func": "mat_mul", } msg_json = dumps(msg) set_emulator_message(msg_json) # Default matrix set-up self.matrix_size = 1024 self.default_split_level = 3 self.set_up_conf(3) # Enforce local chaining set_local_chaining(True)
def run_func(): global request_count set_local_input_output(True) # Get input json_data = request.get_json() app.logger.info("Knative request: {}".format(json_data)) # Check if we should be doing a cold start is_cold_start = False cold_start_interval_str = json_data.get("cold_start_interval") if cold_start_interval_str: cold_start_interval = int(cold_start_interval_str) is_cold_start = request_count % cold_start_interval == 0 if request_count == 0 or is_cold_start: # Simulate cold start if necessary delay_str = os.environ.get("COLD_START_DELAY_MS", "0") if delay_str != "0": delay_seconds = Decimal(delay_str) / 1000 app.logger.info("Simulating cold start for {} seconds".format(delay_seconds)) sleep(delay_seconds) # TODO - need to clear out state here if it's a cold start # Up the request count request_count += 1 # Set up this main thread with the emulator # Make sure to pass on the message ID for child threads msgId = set_emulator_message(dumps(json_data)) json_data["id"] = msgId if json_data.get("async", False): # Run in background if async request func_thread = threading.Thread(target=execute_main, args=[json_data]) func_thread.start() return get_emulator_async_response() else: # Run in main thread execute_main(json_data) func_output = get_output() if not func_output: return "Empty output" else: return func_output
from pyfaasm.core import ( read_state, read_state_offset, write_state, write_state_offset, push_state, pull_state, set_emulator_message, ) import json msg = { "user": "******", "function": "echo", } set_emulator_message(json.dumps(msg)) # Write and push state key = "pyStateTest" valueLen = 10 fullValue = b"0123456789" write_state(key, fullValue) push_state(key) # Read state back in pull_state(key, valueLen) actual = read_state(key, valueLen) print("In = {} out = {}".format(fullValue, actual)) # Update a segment segment = b"999"