Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrote first api performance tests #2218

Merged
merged 23 commits into from
Aug 22, 2024

Conversation

stevensonmichel
Copy link
Contributor

What is this PR doing:

Issue: AAH-####

Reviewers must know:

PR Author & Reviewers: Keep or remove backport labels per Backporting Guidelines
Reviewers: Look for sound code, no code smells, docs & test coverage
Merger: When merging, include the Jira issue link in the squashed commit

Comment on lines 15 to 49
@pytest.mark.deployment_community
def test_api_performance(ansible_config):


config = ansible_config("admin")
api_client = get_client(
config=config,
request_token=False,
require_auth=False
)

threshold = 0.25

resp = api_client('http://localhost:5001/api/_ui/v1/settings/', method='GET')
print("Thhhhhhhe response is")
print (resp)

results = []

for url, baseline in URLS.items():

start_time = time.time()
resp = api_client('http://localhost:5001' + url, method='GET')
elapsed_time = time.time() - start_time

difference = (elapsed_time - baseline) / baseline
if difference > threshold:
results.append(f"{url} exceeds threshold with {difference}")
else:
results.append(f"{url} does not exceed threshold and its difference is {difference}")

print("httttttttttttthe result is")
print(results)

assert True
Copy link
Member

@rochacbruno rochacbruno Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a local fixture for the client, this way other tests could use the same.

Also it looks like a good idea to use parameterized test so each URL is tested individually and we can see each one as a result in the tests

@pytest.fixture(scope="function")
def api_client(ansible_config):
    config = ansible_config("admin")
    api_client = get_client(
        config=config,
        request_token=False,
        require_auth=False
    )
    return api_client


@pytest.mark.deployment_community
@pytest.mark.parametrize("url,baseline", URLS.items())
def test_api_performance(api_client, url, baseline):
    
    threshold = 0.25
    results = []

    start_time = time.time()
    resp = api_client('http://localhost:5001' + url, method='GET')
    elapsed_time = time.time() - start_time
    
    difference = (elapsed_time - baseline) / baseline
    assert difference < threshold

Comment on lines 1 to 16
URLS = {
'/api/_ui/v1/settings/' : 0.5,
'/api/ui/v1/namespaces/' : 0.5,
'/api/_ui/v1/auth/login/' : 0.5,
'/api/_ui/v1/collection-versions/' : 0.5,
'/api/_ui/v1/distributions/' : 0.5,
'/api/_ui/v1/imports/collections/' : 0.5,
'/api/_ui/v1/landing-page/': 0.5,
'/api/_ui/v1/me/' : 0.5,
'/api/_ui/v1/remotes/' : 0.5,
'/api/_ui/v1/search/' : 0.5,
'/api/_ui/v1/settings/' : 0.5,
'/api/_ui/v1/synclists/': 0.5,
'/api/_ui/v1/users/': 0.5,
'/api/_ui/v1/tags/roles/': 0.5,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this object could get more nesting data, so we can play with different methods.

URLS = {
    '/api/_ui/v1/settings/': {
         'baseline': 0.5,
         'method': 'GET'
    },
    '/api/other_url': {
        'baseline': 0.7,
        'method': 'POST',
        'payload': {...}
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I will integrate your advice.

@stevensonmichel stevensonmichel marked this pull request as ready for review August 22, 2024 15:36
@jctanner jctanner merged commit 7b05719 into ansible:master Aug 22, 2024
19 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants