Skip to content

Commit

Permalink
Merge pull request #314 from OpenMined/aziz/load_testing
Browse files Browse the repository at this point in the history
load testing with locust
  • Loading branch information
eelcovdw authored Nov 1, 2024
2 parents 69d9154 + b575d40 commit f7bab05
Show file tree
Hide file tree
Showing 5 changed files with 520 additions and 1 deletion.
Empty file added load_testing/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions load_testing/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from locust import FastHttpUser, between, task
from loguru import logger

from syftbox.client.plugins.sync import endpoints


class SyftBoxUser(FastHttpUser):
network_timeout = 5.0
connection_timeout = 5.0
wait_time = between(0.5, 1.5)

def on_start(self):
self.datasites = []
self.email = "aziz@openmined.org"
self.remote_state: dict[str, list[endpoints.FileMetadata]] = {}

@task
def sync_datasites(self):
remote_datasite_states = endpoints.get_datasite_states(
self.client,
email=self.email,
)
# logger.info(f"Syncing {len(remote_datasite_states)} datasites")
all_files = []
for email, remote_state in remote_datasite_states.items():
all_files.extend(remote_state)

all_paths = [str(f.path) for f in all_files][:10]
logger.info(f"Downloading {len(all_paths)} files")
endpoints.download_bulk(
self.client,
all_paths,
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dev-dependencies = [
"bump2version>=1.0.1",
"faker>=30.3.0",
"ipykernel>=6.29.5",
"locust>=2.32.0",
"pre-commit>=4.0.1",
"pytest-cov>=5.0.0",
"pytest-xdist[psutil]>=3.6.1",
Expand Down
47 changes: 47 additions & 0 deletions syftbox/lib/prof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# stdlib
import contextlib
import os
import signal
import subprocess # nosec
import tempfile
import time


@contextlib.contextmanager
def pyspy() -> None: # type: ignore
"""Profile a block of code using py-spy. Intended for development purposes only.
Example:
```
with pyspy():
# do some work
a = [i for i in range(1000000)]
```
"""
fd, fname = tempfile.mkstemp(".svg")
os.close(fd)

command = [
"sudo",
"-S",
"py-spy",
"record",
"-r",
"100",
"-o",
fname,
"--pid",
str(os.getpid()),
]
process = subprocess.Popen(command, preexec_fn=os.setsid) # nosec

start_time = time.time()
yield process
end_time = time.time()

print(f"Execution time: {end_time - start_time}")
try:
os.killpg(os.getpgid(process.pid), signal.SIGINT)
os.chmod(fname, 0o444)
except Exception as e:
print(f"Error: {e}")
Loading

0 comments on commit f7bab05

Please sign in to comment.