Skip to content

Commit

Permalink
style: ran python linting
Browse files Browse the repository at this point in the history
  • Loading branch information
TeddyCr committed Dec 20, 2024
1 parent cb86fb5 commit b5d2b81
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 115 deletions.
7 changes: 6 additions & 1 deletion ingestion/src/_openmetadata_testutils/helpers/login_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from locust.contrib.fasthttp import FastHttpSession
from requests.auth import AuthBase


class BearerAuth(AuthBase):
def __init__(self, token):
self.token = token
Expand All @@ -11,7 +12,11 @@ def __call__(self, r):
r.headers["authorization"] = "Bearer " + self.token
return r


def login_user(client: FastHttpSession) -> BearerAuth:
resp = client.post("/api/v1/users/login", json={"email":"admin@open-metadata.org","password":"YWRtaW4="})
resp = client.post(
"/api/v1/users/login",
json={"email": "admin@open-metadata.org", "password": "YWRtaW4="},
)
token = resp.json().get("accessToken")
return BearerAuth(token)
54 changes: 0 additions & 54 deletions ingestion/src/metadata/test_load.py

This file was deleted.

46 changes: 27 additions & 19 deletions ingestion/tests/load/test_load.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
"""Run test case result resource load test"""

import csv
import os
from pathlib import Path
import csv
from unittest import TestCase

import yaml

from ingestion.tests.load.utils import run_load_test


def run_all_resources(summary_file: str, locust_file: str):
"""Test test case result resource"""
args = [
"locust",
"--headless",
"-H",
"http://localhost:8585",
"--user",
os.getenv("LOCUST_USER", "50"),
"--spawn-rate",
"1",
"-f",
str(locust_file),
"--run-time",
os.getenv("LOCUST_RUNTIME", "1m"),
"--only-summary",
"--csv",
str(summary_file),
]
"locust",
"--headless",
"-H",
"http://localhost:8585",
"--user",
os.getenv("LOCUST_USER", "50"),
"--spawn-rate",
"1",
"-f",
str(locust_file),
"--run-time",
os.getenv("LOCUST_RUNTIME", "1m"),
"--only-summary",
"--csv",
str(summary_file),
]

run_load_test(args)


class TestAllResources(TestCase):
"""Test class to run all resources load test"""

def test_all_resources(self):
"""Test all resources"""
directory = Path(__file__).parent
Expand All @@ -46,7 +50,7 @@ def test_all_resources(self):
with open(manifest_file, "r", encoding="utf-8") as f:
manifest = yaml.safe_load(f)

with open(str(summary_file)+"_stats.csv", "r", encoding="utf-8") as f:
with open(str(summary_file) + "_stats.csv", "r", encoding="utf-8") as f:
reader = csv.DictReader(f)

for row in reader:
Expand All @@ -60,4 +64,8 @@ def test_all_resources(self):
stat = row.get(metric)
if stat:
stat = int(stat)
self.assertLessEqual(stat, threshold, msg=f"{metric} for {name} is greater than threshold")
self.assertLessEqual(
stat,
threshold,
msg=f"{metric} for {name} is greater than threshold",
)
45 changes: 20 additions & 25 deletions ingestion/tests/load/test_resources/all_resources.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
"""Test class to run all resources load test"""

from typing import List
import importlib.util
from pathlib import Path
import logging
import inspect
import logging
from pathlib import Path
from typing import List

from locust import TaskSet, constant, HttpUser
from locust import HttpUser, TaskSet, constant

TASKS_DIR = "tasks"

logger = logging.getLogger(__name__)


def get_all_tasks_set() -> List:
resource_classes = []
wd = Path(__file__).parent.joinpath(TASKS_DIR)
for file_path in wd.glob("*.py"):
try:
# parent = file_path.parent
# if parent not in sys.path:
# sys.path.insert(0, str(parent))
if not str(file_path).startswith("base_"):
module_path = str(file_path)
module_name = file_path.stem
spec = importlib.util.spec_from_file_location(module_name, module_path)
if not spec:
logger.error(f"Could not load module {module_name}")
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore

for _, obj in inspect.getmembers(module, inspect.isclass):
if obj.__module__ == module_name:
resource_classes.append(obj)
finally:
pass
# sys.path.remove(str(parent))
if not str(file_path).startswith("base_"):
module_path = str(file_path)
module_name = file_path.stem
spec = importlib.util.spec_from_file_location(module_name, module_path)
if not spec:
logger.error(f"Could not load module {module_name}")
continue
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore

for _, obj in inspect.getmembers(module, inspect.isclass):
if obj.__module__ == module_name:
resource_classes.append(obj)

return resource_classes

Expand All @@ -48,8 +42,9 @@ def set_tasks(cls):
tasks = get_all_tasks_set()
cls.tasks = set(tasks)


class All(HttpUser):
host = "http://localhost:8585"
wait_time = constant(1)
wait_time = constant(1) # closed workload
AllResources.set_tasks()
tasks = [AllResources]
33 changes: 22 additions & 11 deletions ingestion/tests/load/test_resources/tasks/test_case_result_tasks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Load test for the test case result resources"""
from datetime import datetime, timedelta

from _openmetadata_testutils.helpers.login_user import login_user
from locust import task, TaskSet
from locust import TaskSet, task

from _openmetadata_testutils.helpers.login_user import login_user

TEST_CASE_RESULT_RESOURCE_PATH = "/api/v1/dataQuality/testCases/testCaseResults"
TEST_CASE_RESOURCE_PATH = "/api/v1/dataQuality/testCases"


class TestCaseResultTasks(TaskSet):
"""Test case result resource load test"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_cases = []
Expand All @@ -20,43 +22,47 @@ def _list_test_case_results(self, start_ts: int, end_ts: int, days_range: str):
Args:
start_ts (int): start timestamp
end_ts (int): end timestamp
range (str):
range (str):
"""
for test_case in self.test_cases:
fqn = test_case.get("fullyQualifiedName")
if fqn:
self.client.get(
f"{TEST_CASE_RESULT_RESOURCE_PATH}/{fqn}",
params={ # type: ignore
params={ # type: ignore
"startTs": start_ts,
"endTs": end_ts,
},
auth=self.bearer,
name=f"{TEST_CASE_RESULT_RESOURCE_PATH}/[fqn]/{days_range}"
name=f"{TEST_CASE_RESULT_RESOURCE_PATH}/[fqn]/{days_range}",
)

@task(3)
def list_test_case_results_30_days(self):
"""List test case results for the last 30 days. Weighted 3"""
now = datetime.now()
last_30_days = int((now - timedelta(days=30)).timestamp() * 1000)
self._list_test_case_results(last_30_days, int(now.timestamp() * 1000), "30_days")

self._list_test_case_results(
last_30_days, int(now.timestamp() * 1000), "30_days"
)

@task(2)
def list_test_case_results_60_days(self):
"""List test case results for the last 60 days. Weighted 2"""
now = datetime.now()
last_60_days = int((now - timedelta(days=60)).timestamp() * 1000)
self._list_test_case_results(last_60_days, int(now.timestamp() * 1000), "60_days")

self._list_test_case_results(
last_60_days, int(now.timestamp() * 1000), "60_days"
)

@task
def list_test_case_results_180_days(self):
"""List test case results for the last 180 days"""
now = datetime.now()
last_180_days = int((now - timedelta(days=180)).timestamp() * 1000)
self._list_test_case_results(last_180_days, int(now.timestamp() * 1000), "180_days")
self._list_test_case_results(
last_180_days, int(now.timestamp() * 1000), "180_days"
)

@task
def stop(self):
Expand All @@ -65,6 +71,11 @@ def stop(self):
def on_start(self):
"""Get a list of test cases to fetch results for"""
self.bearer = login_user(self.client)
resp = self.client.get(f"{TEST_CASE_RESOURCE_PATH}", params={"limit": 100}, auth=self.bearer, name=f"{TEST_CASE_RESOURCE_PATH}?limit=100")
resp = self.client.get(
f"{TEST_CASE_RESOURCE_PATH}",
params={"limit": 100},
auth=self.bearer,
name=f"{TEST_CASE_RESOURCE_PATH}?limit=100",
)
json = resp.json()
self.test_cases = json.get("data", [])
12 changes: 8 additions & 4 deletions ingestion/tests/load/test_resources/tasks/test_case_tasks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Load test for the test case resources"""

from locust import TaskSet, task

from _openmetadata_testutils.helpers.login_user import login_user
from locust import task, TaskSet

TEST_CASE_RESOURCE_PATH = "/api/v1/dataQuality/testCases"


class TestCaseTasks(TaskSet):
"""Test case resource load test"""

Expand All @@ -14,14 +16,16 @@ def _list_test_cases(self):
f"{TEST_CASE_RESOURCE_PATH}",
params={"limit": 10},
auth=self.bearer,
name=f"{TEST_CASE_RESOURCE_PATH}?limit=10")
name=f"{TEST_CASE_RESOURCE_PATH}?limit=10",
)
after = resp.json().get("paging", {}).get("after")
while after:
resp = self.client.get(
f"{TEST_CASE_RESOURCE_PATH}",
params={"limit": 10,"after": after},
params={"limit": 10, "after": after},
auth=self.bearer,
name=f"{TEST_CASE_RESOURCE_PATH}?limit=10")
name=f"{TEST_CASE_RESOURCE_PATH}?limit=10",
)
after = resp.json().get("paging", {}).get("after")

@task(2)
Expand Down
3 changes: 2 additions & 1 deletion ingestion/tests/load/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Utils functions for load testing."""

from typing import List
import sys
from typing import List

import pytest
from locust import main

TEST_CASE_RESOURCE_PATH = "/api/v1/dataQuality/testCases"
TEST_CASE_RESULT_RESOURCE_PATH = "/api/v1/dataQuality/testCases/testCaseResults"


def run_load_test(args: List[str]):
"""Test test case result resource"""
original_argv = sys.argv
Expand Down

0 comments on commit b5d2b81

Please sign in to comment.