Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Connection speed test (#526)
* Update routes.py

Add connection_speed_test

* Update fl_events.py

Fix Max Cycle Limit

* Update routes.py

Fix connection_speed_route

* Update routes.py

fix typo

* Update exceptions.py

Add MaxCycleLimiteExceeded Exception

* Update routes.py

Update test_download / test_upload methods

* Update requirements.txt

Add requests-toolbelt dependency

* Remove unused folder/files

* Add requests_toolbelt dependency

* Update routes.py
- Increase the data sample size to 64 MBytes

Co-authored-by: Héricles Emanuel <hericles.me@gmail.com>
  • Loading branch information
IonesioJunior and monuelo committed Apr 2, 2020
1 parent 76690d7 commit 97944bd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/main/events/fl_events.py
Expand Up @@ -3,7 +3,7 @@
from binascii import unhexlify
from .socket_handler import SocketHandler
from ..codes import MSG_FIELD, RESPONSE_MSG, CYCLE, FL_EVENTS
from ..exceptions import CycleNotFoundError
from ..exceptions import CycleNotFoundError, MaxCycleLimitExceededError
from ..processes import processes
from ..auth import workers
from ..tasks.cycle import complete_cycle, run_task_once
Expand Down Expand Up @@ -127,6 +127,9 @@ def cycle_request(message: dict, socket) -> str:
except CycleNotFoundError:
# Nothing to do
response[CYCLE.STATUS] = CYCLE.REJECTED
except MaxCycleLimitExceededError as e:
response[CYCLE.STATUS] = CYCLE.REJECTED
response[MSG_FIELD.MODEL] = e.name
except Exception as e:
response[CYCLE.STATUS] = CYCLE.REJECTED
response[RESPONSE_MSG.ERROR] = str(e) + traceback.format_exc()
Expand Down
6 changes: 6 additions & 0 deletions app/main/exceptions.py
Expand Up @@ -70,3 +70,9 @@ class InvalidRequestKeyError(PyGridError):
def __init__(self):
message = "Invalid request key!"
super().__init__(message)


class MaxCycleLimitExceededError(PyGridError):
def __init__(self):
message = "There are no cycles remaining"
super().__init__(message)
39 changes: 39 additions & 0 deletions app/main/routes.py
Expand Up @@ -24,6 +24,8 @@
from .events.fl_events import cycle_request, report
from .exceptions import InvalidRequestKeyError, PyGridError
from .codes import MSG_FIELD, CYCLE, RESPONSE_MSG
from requests_toolbelt import MultipartEncoder


# All grid nodes registered at grid network will be stored here
grid_nodes = {}
Expand Down Expand Up @@ -370,6 +372,43 @@ def download_model():
)


@main.route("/federated/speed-test", methods=["GET", "POST"])
def connection_speed_test():
""" Connection speed test. """
response_body = {}
status_code = None

try:
_worker_id = request.args.get("worker_id", None)
_random = request.args.get("random", None)

if not _worker_id or not _random:
raise PyGridError

# If GET method
if request.method == "GET":
# Download data sample (1MB)
data_sample = b"x" * 67108864 # 64 Megabyte
response = {"sample": data_sample}
form = MultipartEncoder(response)
return Response(form.to_string(), mimetype=form.content_type)
elif request.method == "POST": # Otherwise, it's POST method
if request.file:
status_code = 200 # Success
else:
raise PyGridError
except PyGridError as e:
status_code = 400 # Bad Request
response_body[RESPONSE_MSG.ERROR] = str(e)
except Exception as e:
status_code = 500 # Internal Server Error
response_body[RESPONSE_MSG.ERROR] = str(e)

return Response(
json.dumps(response_body), status_code=status_code, mimetype="application/json"
)


@main.route("/federated/authenticate", methods=["POST"])
def auth():
"""uses JWT (HSA/RSA) to authenticate"""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -11,6 +11,7 @@ Jinja2==2.10.1
MarkupSafe==1.1.1
gevent==1.4.0
Werkzeug==0.15.3
requests_toolbelt
requests==2.22.0
flask_sqlalchemy
flask_migrate
Expand Down

0 comments on commit 97944bd

Please sign in to comment.