Skip to content

Commit

Permalink
Seasonal cleanup + replacing a deprecated module (#27)
Browse files Browse the repository at this point in the history
* replaced deprecated module 'optparse' with 'argparse'

* changes to fix unittesting with argparse

* fixed singleton comparisons

* removed unnecessary control

* removed unused imports

* better doc for when to use a deprecated function

* fixed the minor issue with url validator
  • Loading branch information
engshahrad authored Jan 29, 2024
1 parent 6b70353 commit 7439aec
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 63 deletions.
61 changes: 30 additions & 31 deletions comparative_analyzer/ComparativeAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import argparse
from datetime import datetime
import imp
from optparse import OptionParser
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import (
inset_axes,
Expand All @@ -20,7 +18,6 @@
import pickle
import seaborn as sns
import sys
import time

sys.path = ["./", "../"] + sys.path

Expand Down Expand Up @@ -208,34 +205,10 @@ def RelativeDegradation(combined_stat_df):
plt.close()


def main(argv=None):
def main(options):
"""
The main function.
"""
parser = OptionParser()
parser.add_option(
"-s",
"--since",
dest="since",
help="compare archives since time",
action="store_true",
)
parser.add_option(
"-p",
"--plot",
dest="plot",
help="plots default comparative test results",
action="store_true",
)
parser.add_option(
"-c",
"--customized_plot",
dest="customized_plot",
help="specify a customized plotting string",
metavar="FILE",
)
(options, args) = parser.parse_args()

logger.info("Comparative Analyzer started")
print("Log file -> logs/CA.log")

Expand All @@ -259,7 +232,7 @@ def main(argv=None):
archive_files, options.plot
)

if (options.plot) or (argv is None):
if options.plot:
ComparativePlotting(t_df=combined_test_df, p_df_dic=combined_perf_df_dic)
elif options.customized_plot is not None:
cp = options.customized_plot.replace(".py", "")
Expand All @@ -274,4 +247,30 @@ def main(argv=None):


if __name__ == "__main__":
main(sys.argv)
"""
The main function.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-s",
"--since",
dest="since",
help="compare archives since time",
action="store_true",
)
parser.add_argument(
"-p",
"--plot",
dest="plot",
help="plots default comparative test results",
action="store_true",
)
parser.add_argument(
"-c",
"--customized_plot",
dest="customized_plot",
help="specify a customized plotting string",
metavar="FILE",
)
options = parser.parse_args()
main(options)
2 changes: 1 addition & 1 deletion synthetic_workload_invoker/WorkloadChecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def CheckWorkloadValidity(workload):
"Please enter a valid value for test_duration_in_seconds field in the config file."
)
return False
elif int(test_duration_in_seconds) <= 0:
if int(test_duration_in_seconds) <= 0:
logger_wlch.error("test_duration_in_seconds should be greater than zero!")
return False
except:
Expand Down
16 changes: 8 additions & 8 deletions synthetic_workload_invoker/WorkloadInvoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# LICENSE file in the root directory of this source tree.

# Standard imports
import argparse
import json
from optparse import OptionParser
import os
from requests_futures.sessions import FuturesSession
import subprocess
Expand Down Expand Up @@ -39,9 +39,10 @@
RESULT = "true"


def PROCESSInstanceGenerator(instance, instance_script, instance_times, blocking_cli):
def PROCESSInstanceGenerator(instance_script, instance_times, blocking_cli):
"""
Deprecated. This function was used to invoke a function in OpenWhisk using new processes.
You can use this approach, but it is not recommended due to high overhead.
"""
if len(instance_times) == 0:
return False
Expand Down Expand Up @@ -73,7 +74,7 @@ def HTTPInstanceGeneratorOW(action, instance_times, blocking_cli, param_file=Non
authentication = (user_pass[0], user_pass[1])
after_time, before_time = 0, 0

if param_file == None:
if param_file is None:
st = 0
for t in instance_times:
st = st + t - (after_time - before_time)
Expand Down Expand Up @@ -149,7 +150,7 @@ def HTTPInstanceGeneratorGeneric(instance_times, blocking_cli, url, data):
"""
if len(instance_times) == 0:
return False
if (validators.url(url) != True):
if validators.url(url) is not True:
logger.error("Invalid URL: " + url)
return False

Expand Down Expand Up @@ -209,7 +210,6 @@ def CreateActionInvocationThreads(workload, all_events):
args=[instance_times, blocking_cli, url, data],
)
)
pass

return threads

Expand All @@ -220,15 +220,15 @@ def main(argv):
"""
logger.info("Workload Invoker started")
print("Log file -> logs/SWI.log")
parser = OptionParser()
parser.add_option(
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config_json",
dest="config_json",
help="The input json config file describing the synthetic workload.",
metavar="FILE",
)
(options, args) = parser.parse_args()
options = parser.parse_args()

if not CheckJSONConfig(options.config_json):
logger.error("You should specify a JSON config file using -c option!")
Expand Down
7 changes: 4 additions & 3 deletions tests/unit_tests/ComparativeAnalyzer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# LICENSE file in the root directory of this source tree.

import datetime
import os
from os.path import exists
import unittest
from comparative_analyzer.ComparativeAnalyzer import *
import comparative_analyzer.ComparativeAnalyzer as CA


class TestComparativeAnalyzer(unittest.TestCase):
Expand All @@ -27,9 +27,10 @@ def test_GetTimeFromDFNameNoInput(self):
self.assertEqual(str(sampleDT), "2021-01-18 04:37:00")

def test_ComparativeAnalyzer_Script(self):
CA.main()
os.system("./ComparativeAnalyzer -p")
self.assertTrue(exists("sample_comparative_plot.png"))


if __name__ == "__main__":
unittest.main()
runner = unittest.TextTestRunner()
runner.run(unittest.TestLoader().loadTestsFromTestCase(TestComparativeAnalyzer))
3 changes: 2 additions & 1 deletion tests/unit_tests/EventGenerator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ def test_GenericEventGenerator_Normal(self):


if __name__ == "__main__":
unittest.main()
runner = unittest.TextTestRunner()
runner.run(unittest.TestLoader().loadTestsFromTestCase(TestEventGenerator))
3 changes: 2 additions & 1 deletion tests/unit_tests/WorkloadAnalyzer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ def test_ConstructConfigDataframe(self):


if __name__ == "__main__":
unittest.main()
runner = unittest.TextTestRunner()
runner.run(unittest.TestLoader().loadTestsFromTestCase(TestWorkloadAnalyzer))
3 changes: 2 additions & 1 deletion tests/unit_tests/WorkloadChecker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ def test_CheckWorkloadValidity(self):


if __name__ == "__main__":
unittest.main()
runner = unittest.TextTestRunner()
runner.run(unittest.TestLoader().loadTestsFromTestCase(TestWorkloadChecker))
7 changes: 4 additions & 3 deletions tests/unit_tests/WorkloadInvoker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_HTTPInstanceGeneratorGenerics(self):
status = HTTPInstanceGeneratorGeneric(
instance_times=[1.0, 1.0, 1.0, 1.0],
blocking_cli=0,
url="http://localhost:8080",
url="http://127.0.0.1:8080",
data="",
)
self.assertTrue(status)
Expand All @@ -21,7 +21,7 @@ def test_HTTPInstanceGeneratorGenerics_with_empty_instance_times(self):
status = HTTPInstanceGeneratorGeneric(
instance_times=[],
blocking_cli=0,
url="http://localhost:8080",
url="http://127.0.0.1:8080",
data="",
)
self.assertFalse(status)
Expand All @@ -46,4 +46,5 @@ def test_HTTPInstanceGeneratorGenerics_with_invalid_url(self):


if __name__ == "__main__":
unittest.main()
runner = unittest.TextTestRunner()
runner.run(unittest.TestLoader().loadTestsFromTestCase(TestWorkloadInvoker))
28 changes: 14 additions & 14 deletions workload_analyzer/WorkloadAnalyzer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env python3

# Copyright (c) 2019 Princeton University
# Copyright (c) 2019 Princeton University, 2024 UBC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Standard
import argparse
from datetime import datetime
import json
from optparse import OptionParser
import os
from os.path import isfile, join
import pandas as pd
Expand Down Expand Up @@ -178,9 +178,9 @@ def NormalizeMemoryValue(mem_string):
"""
if mem_string[-1] == "G":
return float(mem_string[:-1])
elif mem_string[-1] == "M":
if mem_string[-1] == "M":
return float(mem_string[:-1]) / 1024.0
elif mem_string[-1] == "K":
if mem_string[-1] == "K":
return float(mem_string[:-1]) / (1024 * 1024.0)


Expand Down Expand Up @@ -221,7 +221,7 @@ def GetControlGroupsRecords(since=None):
"docker_tasks": [],
"memory": [],
}
if since == None:
if since is None:
logger.error("No since parameter entered!")
return None
log_path = FAAS_ROOT + "/logs"
Expand Down Expand Up @@ -274,53 +274,53 @@ def main(argv):
"""
The main function.
"""
parser = OptionParser()
parser.add_option(
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
help="prints the detailed test data",
action="store_true",
)
parser.add_option(
parser.add_argument(
"-p", "--plot", dest="plot", help="plots the test results", action="store_true"
)
parser.add_option(
parser.add_argument(
"-s",
"--save_plot",
dest="save_plot",
help="save test result plots",
action="store_true",
)
parser.add_option(
parser.add_argument(
"-a",
"--archive",
dest="archive",
help="archive the test results in an pickle file",
action="store_true",
)
parser.add_option(
parser.add_argument(
"-c",
"--capacity_factor",
dest="capacity_factor",
help="returns the capacity factor",
action="store_true",
)
parser.add_option(
parser.add_argument(
"-o",
"--override_testname",
dest="override_testname",
help="override the JSON test name",
metavar="FILE",
)
parser.add_option(
parser.add_argument(
"-r",
"--read_results",
dest="read_results",
help="gather also the results of function invocations",
action="store_true",
)
(options, args) = parser.parse_args()
options = parser.parse_args()

logger.info("Workload Analyzer started")
print("Log file -> logs/WA.log")
Expand Down

0 comments on commit 7439aec

Please sign in to comment.