{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "import time\n", "import uuid\n", "import requests\n", "import json\n", "!pip install -q websocket-client\n", "import websocket" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "URL = \"18.236.68.146\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from concurrent.futures import ThreadPoolExecutor\n", "\n", "def run_in_parallel(func, n):\n", " # Ensure the callable and repetitions are valid\n", " if not callable(func) or not isinstance(n, int) or n < 1:\n", " raise ValueError(\"Invalid function or number of repetitions\")\n", " \n", " # Define a wrapper function to execute\n", " def task_wrapper():\n", " return func()\n", " \n", " # Use ThreadPoolExecutor to run tasks in parallel\n", " with ThreadPoolExecutor(max_workers=n) as executor:\n", " futures = [executor.submit(task_wrapper) for _ in range(n)]\n", " \n", " # Wait for all futures to complete and collect results\n", " results = [future.result() for future in futures]\n", " \n", " return results\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Gradio 4" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "def request():\n", "\tstart_time = time.time()\n", "\tsession_hash = uuid.uuid4().hex\n", "\tpayload = {\"data\": [\"test\"], \"fn_index\": 0, \"session_hash\": session_hash}\n", "\turl = f\"http://{URL}/\"\n", "\tresp = requests.post(f\"{url}queue/join\", json=payload, timeout=5)\n", "\tassert resp.status_code == 200\n", "\n", "\tmessage_count = 0\n", "\toutput = \"\"\n", "\twith requests.get(f\"{url}queue/data?session_hash={session_hash}\", stream=True) as response:\n", "\t\tresponse.raise_for_status()\n", "\t\tfor line in response.iter_lines():\n", "\t\t\tif line:\n", "\t\t\t\tdecoded_line = line.decode('utf-8')\n", "\t\t\t\tif decoded_line.startswith(\"data:\"):\n", "\t\t\t\t\tdata = decoded_line.replace(\"data: \", \"\")\n", "\t\t\t\t\tif \"close_stream\" in data:\n", "\t\t\t\t\t\tbreak\n", "\t\t\t\t\toutput = data\n", "\t\t\t\t\tmessage_count += 1\n", "\t\n", "\tend_time = time.time()\n", "\tduration = end_time - start_time\n", "\treturn (duration, message_count, json.loads(output)[\"output\"][\"data\"])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5.6847522258758545,\n", " 504,\n", " [[['test',\n", " 'Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem']]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 5)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 25)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "83.2809367132187 535.58\n" ] } ], "source": [ "output = run_in_parallel(request, 100)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "95.03129840755463 526.68\n" ] } ], "source": [ "output = run_in_parallel(request, 250)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Gradio 3.x" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def request():\n", " start_time = time.time()\n", " url = f\"ws://{URL}/\"\n", " session_hash = uuid.uuid4().hex\n", " output = None\n", " message_count = 0\n", " start_time = time.time()\n", " try:\n", " # Connect to WebSocket server\n", " ws = websocket.create_connection(f\"{url}queue/join\")\n", "\n", " while True:\n", " message = ws.recv() # Wait and receive incoming message\n", " message_count += 1\n", " message = json.loads(message)\n", " msg = message[\"msg\"]\n", "\n", " if msg == \"send_hash\":\n", " ws.send(json.dumps({\"session_hash\": session_hash, \"fn_index\": 0}))\n", "\n", " if msg == \"send_hash\":\n", " ws.send(json.dumps({\"data\":[\"test\"],\"event_data\":None,\"fn_index\":0,\"session_hash\":session_hash}\t))\n", "\n", " if msg == \"process_completed\":\n", " output = message[\"output\"][\"data\"]\n", " break\n", "\n", "\n", " finally:\n", " ws.close() # Ensure the connection is closed properly\n", " \n", " duration = time.time() - start_time\n", " return duration, message_count, output\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(7.217260122299194,\n", " 506,\n", " [[['test',\n", " 'Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem']]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 5)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 25)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "18.25279019832611 506.0\n" ] } ], "source": [ "output = run_in_parallel(request, 100)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "46.61669938659668 506.408\n" ] } ], "source": [ "output = run_in_parallel(request, 250)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple SSE" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def request():\n", "\tstart_time = time.time()\n", "\turl = f\"http://{URL}/\"\n", "\tmessage_count = 0\n", "\toutput = \"\"\n", "\twith requests.get(f\"{url}sse\", stream=True) as response:\n", "\t\tresponse.raise_for_status()\n", "\t\tfor line in response.iter_lines():\n", "\t\t\tif line:\n", "\t\t\t\tdecoded_line = line.decode('utf-8')\n", "\t\t\t\tif decoded_line.startswith(\"data:\"):\n", "\t\t\t\t\toutput = decoded_line.replace(\"data: \", \"\")\n", "\t\t\t\t\tmessage_count += 1\n", "\t\t\tif message_count == 500:\n", "\t\t\t\tbreak\n", "\t\n", "\tend_time = time.time()\n", "\tduration = end_time - start_time\n", "\treturn (duration, message_count, output)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "request()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 5)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 25)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 100)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 250)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple Websocket" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def request():\n", " start_time = time.time()\n", " url = f\"ws://{URL}/\"\n", " output = None\n", " message_count = 0\n", " start_time = time.time()\n", " try:\n", " # Connect to WebSocket server\n", " ws = websocket.create_connection(f\"{url}ws\")\n", "\n", " while True:\n", " message = ws.recv() # Wait and receive incoming message\n", " message_count += 1\n", " output = message\n", " if message_count == 500:\n", " break\n", "\n", "\n", " finally:\n", " ws.close() # Ensure the connection is closed properly\n", " \n", " duration = time.time() - start_time\n", " return duration, message_count, output\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "request()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 5)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 25)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 100)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 250)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SSE w/ Workers" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "def request():\n", "\tstart_time = time.time()\n", "\tpayload = {\"data\": \"test\"}\n", "\turl = f\"http://{URL}/\"\n", "\tresp = requests.post(f\"{url}sse/send\", json=payload, timeout=5)\n", "\tassert resp.status_code == 200\n", "\tsession_id = resp.json()[\"session_id\"]\n", "\n", "\tmessage_count = 0\n", "\toutput = \"\"\n", "\twith requests.get(f\"{url}sse/listen?session_id={session_id}\", stream=True) as response:\n", "\t\tresponse.raise_for_status()\n", "\t\tfor line in response.iter_lines():\n", "\t\t\tif line:\n", "\t\t\t\tdecoded_line = line.decode('utf-8')\n", "\t\t\t\tif decoded_line.startswith(\"data:\"):\n", "\t\t\t\t\toutput = decoded_line.replace(\"data: \", \"\")\n", "\t\t\t\t\tmessage_count += 1\n", "\t\n", "\tend_time = time.time()\n", "\tduration = end_time - start_time\n", "\treturn (duration, message_count, output)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5.302220106124878,\n", " 500,\n", " 'Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem ')" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 5)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 25)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.664539747238159 500.0\n" ] } ], "source": [ "output = run_in_parallel(request, 100)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11.787519352912902 500.0\n" ] } ], "source": [ "output = run_in_parallel(request, 250)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Websockets w/ Workers" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "def request():\n", " start_time = time.time()\n", " url = f\"ws://{URL}/\"\n", " output = None\n", " message_count = 0\n", " start_time = time.time()\n", " try:\n", " ws = websocket.create_connection(f\"{url}ws\")\n", " ws.send(\"test\")\n", "\n", " while True:\n", " message = ws.recv() # Wait and receive incoming message\n", " message_count += 1\n", " output = message\n", " if message_count == 500:\n", " break\n", " \n", "\n", " finally:\n", " ws.close() # Ensure the connection is closed properly\n", " \n", " duration = time.time() - start_time\n", " return duration, message_count, output\n" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5.335849285125732,\n", " 500,\n", " 'Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem Lorem ')" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 5)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = run_in_parallel(request, 25)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16.153259081840517 500.0\n" ] } ], "source": [ "output = run_in_parallel(request, 100)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40.5195159034729 500.0\n" ] } ], "source": [ "output = run_in_parallel(request, 250)\n", "avg_duration = sum(o[0] for o in output) / len(output)\n", "avg_msg = sum(o[1] for o in output) / len(output)\n", "print(avg_duration, avg_msg)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "projectenv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" } }, "nbformat": 4, "nbformat_minor": 2 }