Skip to content

Commit

Permalink
Merge pull request #2782 from wazuh/feature/2287-move-get-datetime-di…
Browse files Browse the repository at this point in the history
…ff-to-wazuh-testing

Move the 'get_datetime_diff' function to 'wazuh-testing' utils module
  • Loading branch information
jmv74211 authored May 30, 2022
2 parents c764de8 + c09339e commit c51f746
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
24 changes: 17 additions & 7 deletions deps/wazuh_testing/wazuh_testing/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
import ipaddress
import json
import logging
import numbers
import re
import socket
import string
import numbers
from datetime import datetime
from functools import wraps
from random import randint, SystemRandom
from time import sleep
from random import randint, SystemRandom, choice
import string
import json
import socket
import ipaddress


def retry(exceptions, attempts=5, delay=1, delay_multiplier=2):
Expand Down Expand Up @@ -179,3 +178,14 @@ def format_ipv6_long(ipv6_address):
str: IPV6 long form
"""
return (ipaddress.ip_address(ipv6_address).exploded).upper()


def get_datetime_diff(phase_datetimes, date_format):
"""Calculate the difference between two datetimes.
Args:
phase_datetimes (list): List containing start and end datetimes.
date_format (str): Expected datetime shape.
"""
return datetime.strptime(phase_datetimes[1], date_format) - datetime.strptime(phase_datetimes[0], date_format)

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ resources:
workers:
max: 47.85
mean: 9.35
reg_cof: 0.06
reg_cof: 0.1
FD:
master:
max: 160
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Copyright (C) 2015, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

from datetime import datetime
from os import listdir
from os.path import join, dirname, realpath

import pytest
from yaml import safe_load

from wazuh_testing.tools.performance.csv_parser import ClusterCSVTasksParser, ClusterCSVResourcesParser, ClusterEnvInfo
from wazuh_testing.tools.utils import get_datetime_diff

test_data_path = join(dirname(realpath(__file__)), 'data')
configurations = {file.replace('_thresholds.yaml', ''): safe_load(open(join(test_data_path, file))) for file in
Expand All @@ -21,22 +21,12 @@
# Fixtures
@pytest.fixture()
def n_workers(pytestconfig):
return pytestconfig.getoption("n_workers")
return pytestconfig.getoption('n_workers')


@pytest.fixture()
def n_agents(pytestconfig):
return pytestconfig.getoption("n_agents")


# Functions
def get_datetime_diff(phase_datetimes):
"""Calculate the difference between two datetimes.
Args:
phase_datetimes (list): List containing start and end datetimes.
"""
return datetime.strptime(phase_datetimes[1], date_format) - datetime.strptime(phase_datetimes[0], date_format)
return pytestconfig.getoption('n_agents')


def test_cluster_performance(artifacts_path, n_workers, n_agents):
Expand Down Expand Up @@ -64,19 +54,19 @@ def test_cluster_performance(artifacts_path, n_workers, n_agents):
try:
cluster_info = ClusterEnvInfo(artifacts_path).get_all_info()
except FileNotFoundError:
pytest.fail(f'Path "{artifacts_path}" could not be found or it may not follow the proper structure.')
pytest.fail(f"Path '{artifacts_path}' could not be found or it may not follow the proper structure.")

if cluster_info.get('worker_nodes', 0) != int(n_workers):
pytest.fail(f'Information of {n_workers} workers was expected inside the artifacts folder, but '
f'{cluster_info.get("worker_nodes", 0)} were found.')
pytest.fail(f"Information of {n_workers} workers was expected inside the artifacts folder, but "
f"{cluster_info.get('worker_nodes', 0)} were found.")

# Calculate stats from data inside artifacts path.
data = {'tasks': ClusterCSVTasksParser(artifacts_path).get_stats(),
'resources': ClusterCSVResourcesParser(artifacts_path).get_stats()}

if not data['tasks'] or not data['resources']:
pytest.fail(f'Stats could not be retrieved, "{artifacts_path}" path may not exist, it is empty or it may not'
f' follow the proper structure.')
pytest.fail(f"Stats could not be retrieved, '{artifacts_path}' path may not exist, it is empty or it may not"
f" follow the proper structure.")

# Compare each stat with its threshold.
for data_name, data_stats in data.items():
Expand All @@ -92,16 +82,15 @@ def test_cluster_performance(artifacts_path, n_workers, n_agents):
'phase': phase})

try:
assert not exceeded_thresholds, f"Some thresholds were exceeded:\n- " + '\n- '.join(
assert not exceeded_thresholds, 'Some thresholds were exceeded:\n- ' + '\n- '.join(
'{stat} {column} {value} >= {threshold} ({node}, {file}, {phase})'.format(**item) for item in
exceeded_thresholds)
finally:
# Add useful information to report as stdout
try:
print('\n')
print(f'Setup phase took {get_datetime_diff(cluster_info["phases"]["setup_phase"])}s '
f'({cluster_info["phases"]["setup_phase"][0]} - {cluster_info["phases"]["setup_phase"][1]}).')
print(f'Stable phase took {get_datetime_diff(cluster_info["phases"]["stable_phase"])}s '
f'({cluster_info["phases"]["stable_phase"][0]} - {cluster_info["phases"]["stable_phase"][1]}).')
print(f"\nSetup phase took {get_datetime_diff(cluster_info['phases']['setup_phase'], date_format)}s "
f"({cluster_info['phases']['setup_phase'][0]} - {cluster_info['phases']['setup_phase'][1]}).")
print(f"Stable phase took {get_datetime_diff(cluster_info['phases']['stable_phase'], date_format)}s "
f"({cluster_info['phases']['stable_phase'][0]} - {cluster_info['phases']['stable_phase'][1]}).")
except KeyError:
print('No information available about test phases.')

0 comments on commit c51f746

Please sign in to comment.