|
| 1 | +import time |
| 2 | +import socket |
| 3 | +import json |
| 4 | +import azure.functions as func |
| 5 | +import logging |
| 6 | +from time import process_time_ns |
| 7 | +import math |
| 8 | +from psutil import virtual_memory |
| 9 | +from numpy import empty, float32 |
| 10 | + |
| 11 | +# Global variable for hostname |
| 12 | +hostname = socket.gethostname() |
| 13 | + |
| 14 | +# Placeholder for `execute_function`, retrieve according to server/trace-func-py/trace_func.py |
| 15 | +def execute_function(input, runTime, totalMem): |
| 16 | + startTime = process_time_ns() |
| 17 | + |
| 18 | + chunkSize = 2**10 # size of a kb or 1024 |
| 19 | + totalMem = totalMem*(2**10) # convert Mb to kb |
| 20 | + memory = virtual_memory() |
| 21 | + used = (memory.total - memory.available) // chunkSize # convert to kb |
| 22 | + additional = max(1, (totalMem - used)) |
| 23 | + array = empty(additional*chunkSize, dtype=float32) # make an uninitialized array of that size, uninitialized to keep it fast |
| 24 | + # convert to ns |
| 25 | + runTime = (runTime - 1)*(10**6) # -1 because it should be slighly bellow that runtime |
| 26 | + memoryIndex = 0 |
| 27 | + while process_time_ns() - startTime < runTime: |
| 28 | + for i in range(0, chunkSize): |
| 29 | + sin_i = math.sin(i) |
| 30 | + cos_i = math.cos(i) |
| 31 | + sqrt_i = math.sqrt(i) |
| 32 | + array[memoryIndex + i] = sin_i |
| 33 | + memoryIndex = (memoryIndex + chunkSize) % additional*chunkSize |
| 34 | + return (process_time_ns() - startTime) // 1000 |
| 35 | + |
| 36 | +def main(req: func.HttpRequest) -> func.HttpResponse: |
| 37 | + logging.info("Processing request.") |
| 38 | + |
| 39 | + start_time = time.time() |
| 40 | + |
| 41 | + # Parse JSON request body |
| 42 | + try: |
| 43 | + req_body = req.get_json() |
| 44 | + logging.info(f"Request body: {req_body}") |
| 45 | + except ValueError: |
| 46 | + logging.error("Invalid JSON received.") |
| 47 | + return func.HttpResponse( |
| 48 | + json.dumps({"error": "Invalid JSON"}), |
| 49 | + status_code=400, |
| 50 | + mimetype="application/json" |
| 51 | + ) |
| 52 | + |
| 53 | + runtime_milliseconds = req_body.get('RuntimeInMilliSec', 1000) |
| 54 | + memory_mebibytes = req_body.get('MemoryInMebiBytes', 128) |
| 55 | + |
| 56 | + logging.info(f"Runtime requested: {runtime_milliseconds} ms, Memory: {memory_mebibytes} MiB") |
| 57 | + |
| 58 | + # Directly call the execute_function |
| 59 | + duration = execute_function("",runtime_milliseconds,memory_mebibytes) |
| 60 | + result_msg = f"Workload completed in {duration} microseconds" |
| 61 | + |
| 62 | + # Prepare the response |
| 63 | + response = { |
| 64 | + "Status": "Success", |
| 65 | + "Function": req.url.split("/")[-1], |
| 66 | + "MachineName": hostname, |
| 67 | + "ExecutionTime": int((time.time() - start_time) * 1_000_000), # Total time (includes HTTP, workload, and response prep) |
| 68 | + "DurationInMicroSec": duration, # Time spent on the workload itself |
| 69 | + "MemoryUsageInKb": memory_mebibytes * 1024, |
| 70 | + "Message": result_msg |
| 71 | + } |
| 72 | + |
| 73 | + logging.info(f"Response: {response}") |
| 74 | + |
| 75 | + return func.HttpResponse( |
| 76 | + json.dumps(response), |
| 77 | + status_code=200, |
| 78 | + mimetype="application/json" |
| 79 | + ) |
0 commit comments