Skip to content
Snippets Groups Projects
Commit 3c686992 authored by Maël Madon's avatar Maël Madon
Browse files

test: added sanity tests to the test suite

parent 521ff1f9
No related branches found
No related tags found
No related merge requests found
import os
class KTH_WL:
swf_file = "workloads/KTH-SP2-1996-2.1-cln.swf"
nb_users = 214
nb_jobs = 28476
thresholds = [0, 30, 1440]
def create_dir_rec_if_needed(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
\ No newline at end of file
import os, sys
import subprocess
import pytest
import json
import sys
from conftest import *
sys.path.append('../user_session_decompo')
from swf2sessions import swf2sessions
class KTH_WL:
swf_file = "workloads/KTH-SP2-1996-2.1-cln.swf"
nb_users = 214
nb_jobs = 28476
thresholds = [0, 30, 1440]
def create_dir_rec_if_needed(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
import os, subprocess
import pytest
import json
def run_test(delim, threshold, input_swf=KTH_WL.swf_file, dyn_red=True, graph=False):
def run_test(delim,
threshold,
input_swf=KTH_WL.swf_file,
dyn_red=True,
graph=False):
test_name = f"KTH_{delim}_t{threshold}"
out_dir = os.path.abspath(f'test-out/{test_name}')
create_dir_rec_if_needed(out_dir)
cp = subprocess.run(['python3', 'swf2sessions.py', '-at', '30', input_swf, out_dir], check=True)
cp = subprocess.run(
['python3', 'swf2sessions.py', '-at', '30', input_swf, out_dir],
check=True)
swf2sessions(input_swf, out_dir, delim, threshold, dyn_red, graph, False)
return out_dir, cp
def test_bad_input():
with pytest.raises(subprocess.CalledProcessError):
run_test('arrival', 30, input_swf="grumpf")
def test_bad_output():
bad_output_name = "/grumpf"
with pytest.raises(subprocess.CalledProcessError):
subprocess.run(['python3', 'swf2sessions.py', '-at', '30', KTH_WL.swf_file, bad_output_name], check=True)
subprocess.run([
'python3', 'swf2sessions.py', '-at', '30', KTH_WL.swf_file,
bad_output_name
],
check=True)
def test_kth_delim_last():
......@@ -48,8 +51,6 @@ def test_kth_delim_last():
assert os.path.exists(out_dir)
assert len(os.listdir(out_dir)) > 0
kth_sanity_check(out_dir)
def test_kth_delim_arrival():
"""Launch swf2sessions with 'arrival' delimitation approach and several threshold values"""
......@@ -60,8 +61,6 @@ def test_kth_delim_arrival():
assert os.path.exists(out_dir)
assert len(os.listdir(out_dir)) > 0
kth_sanity_check(out_dir)
def test_kth_delim_max():
"""Launch swf2sessions with 'max' delimitation approach and several threshold values"""
......@@ -72,30 +71,7 @@ def test_kth_delim_max():
assert os.path.exists(out_dir)
assert len(os.listdir(out_dir)) > 0
kth_sanity_check(out_dir)
def nb_users(out_dir):
"""Return the number of users for which a SABjson file has been created"""
assert os.path.exists(out_dir)
SABjsons = [file for file in os.listdir(out_dir) if file[-8:]==".SABjson"]
return len(SABjsons)
def nb_jobs(out_dir):
"""Return the number of jobs in all the SABjsons in out_dir"""
assert os.path.exists(out_dir)
SABjsons = [file for file in os.listdir(out_dir) if file[-8:]==".SABjson"]
job_count = 0
for file in SABjsons:
# with open(file, ) TODO finish
def kth_sanity_check(out_dir):
"""Perform some sanity checks on the output files"""
assert nb_users(out_dir) == KTH_WL.nb_users
from conftest import KTH_WL, thresholds
import json
import os.path
import random
random.seed(64875)
def SABjson_sanity_check(SABjson_file, arrival_threshold=None):
"""Perform some sanity checks on the given SABjson file.
If the SABjson was created with arrival delimitation approach
(arrival_threshold != None), checks also consistency of inter-arrival times.
Consistency cannot be checked for max and last delimitation approaches as
we lost the information about the original finish times."""
with open(SABjson_file, "r") as fd:
data = json.load(fd)
assert "sessions" in data, "Missing required field 'sessions'"
s_id = 1
session_start_time = -1
job_sub_time = -1
for session in data["sessions"]:
# Session sanity check
assert int(session["id"]) == s_id, "Sessions should have consecutive ids"
assert float(session["first_submit_time"]) >= session_start_time, "Sessions should be in increasing order of start time"
session_start_time = float(session["first_submit_time"])
assert len(session["preceding_sessions"]) == len(session["thinking_time_after_preceding_session"])
# Job sanity check
assert len(session["jobs"]) == int(session["nb_jobs"])
assert float(session["jobs"][0]["subtime"]) == 0, "First job in a session should have a subtime of 0"
for job in session["jobs"]:
previous_job_subtime = job_sub_time
job_sub_time = float(job["subtime"]) + session_start_time
assert job_sub_time >= previous_job_subtime, "Jobs should be in increasing order of submission time"
# Interrarrival consistency
if arrival_threshold is not None and previous_job_subtime != -1:
interrarrival = job_sub_time - previous_job_subtime
if job == session["jobs"][0]:
assert interrarrival > arrival_threshold, f"Session {s_id} was started but the interrarrival time was not suffisiant"
else:
job_id = job["id"]
assert interrarrival <= arrival_threshold, f"Job {job_id} in session {s_id} should be in a new session"
s_id += 1
fd.close()
def nb_users(out_dir):
"""Return the number of users for which a SABjson file has been created"""
assert os.path.exists(out_dir)
SABjsons = [
file for file in os.listdir(out_dir) if file[-8:] == ".SABjson"
]
return len(SABjsons)
def nb_jobs(out_dir):
"""Return the number of jobs in all the SABjsons in out_dir"""
assert os.path.exists(out_dir)
SABjsons = [
file for file in os.listdir(out_dir) if file[-8:] == ".SABjson"
]
job_count = 0
for file in SABjsons:
with open(f"{out_dir}/{file}", 'r') as fp:
user_data = json.load(fp)
for sess in user_data["sessions"]:
job_count += int(sess["nb_jobs"])
return job_count
def kth_sanity_check(out_dir):
"""Checks that the output files correspond to the input trace in features
like total number of jobs or number of users"""
assert nb_users(out_dir) == KTH_WL.nb_users
assert nb_jobs(out_dir) == KTH_WL.nb_jobs
def test_sanity_kth_last_SABjsons():
for threshold in thresholds:
out_dir = os.path.abspath(f"test-out/KTH_last_t{threshold}")
kth_sanity_check(out_dir)
SABjsons_sample = random.sample(os.listdir(out_dir), k=10)
for file in SABjsons_sample:
SABjson_sanity_check(f"{out_dir}/{file}")
def test_sanity_kth_max_SABjsons():
for threshold in thresholds:
out_dir = os.path.abspath(f"test-out/KTH_max_t{threshold}")
kth_sanity_check(out_dir)
SABjsons_sample = random.sample(os.listdir(out_dir), k=10)
for file in SABjsons_sample:
SABjson_sanity_check(f"{out_dir}/{file}")
def test_sanity_kth_arrival_SABjsons():
for threshold in thresholds:
out_dir = os.path.abspath(f"test-out/KTH_arrival_t{threshold}")
kth_sanity_check(out_dir)
SABjsons_sample = random.sample(os.listdir(out_dir), k=10)
for file in SABjsons_sample:
SABjson_sanity_check(f"{out_dir}/{file}", arrival_threshold=threshold)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment