perftester 0.7.0__py3-none-any.whl → 0.8.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
perftester/__init__.py CHANGED
@@ -1,16 +1,16 @@
1
- from .perftester import (
2
- IncorrectArgumentError,
3
- TimeTestError,
4
- MemoryTestError,
5
- FunctionError,
6
- CLIPathError,
7
- LogFilePathError,
8
- Config,
9
- config,
10
- memory_usage_benchmark,
11
- memory_usage_test,
12
- time_benchmark,
13
- time_test,
14
- pp,
15
- MiB_TO_MB_FACTOR,
16
- )
1
+ from .perftester import (
2
+ IncorrectArgumentError,
3
+ TimeTestError,
4
+ MemoryTestError,
5
+ FunctionError,
6
+ CLIPathError,
7
+ LogFilePathError,
8
+ Config,
9
+ config,
10
+ memory_usage_benchmark,
11
+ memory_usage_test,
12
+ time_benchmark,
13
+ time_test,
14
+ pp,
15
+ MiB_TO_MB_FACTOR,
16
+ )
perftester/__main__.py CHANGED
@@ -1,178 +1,178 @@
1
- """Module responsible for the CLI perftester command."""
2
- import importlib
3
- import inspect
4
- import os
5
- import pathlib
6
- import sys
7
- from collections import namedtuple
8
- from easycheck import check_if_paths_exist
9
- from perftester import config, TimeTestError, MemoryTestError, CLIPathError
10
-
11
-
12
- TestResults = namedtuple("TestResults", "passed_tests failed_tests")
13
-
14
- config.full_traceback()
15
-
16
-
17
- def main():
18
- files = _read_cli_args()
19
- _initialize_log_file(len(files))
20
- _import_settings_from_config_file()
21
- passed_perftesters, failed_perftesters = [], []
22
-
23
- for file in files:
24
- module, module_name = _import_module(file)
25
- perftester_functions = _find_perftester_functions(module)
26
- for func in perftester_functions:
27
- this_test = _perftester(module_name, func)
28
- if this_test:
29
- failed_perftesters.append(
30
- f"{str(module_name)}.{func.__name__}"
31
- )
32
- else:
33
- passed_perftesters.append(
34
- f"{str(module_name)}.{func.__name__}"
35
- )
36
-
37
- test_results = TestResults(
38
- passed_tests=passed_perftesters, failed_tests=failed_perftesters
39
- )
40
- _log_perftester_results(test_results)
41
-
42
-
43
- def _read_cli_args():
44
- if len(sys.argv) == 1:
45
- path = pathlib.Path(os.getcwd())
46
- else:
47
- # sys.argv[1] is always a string, so no need to check it
48
- path = pathlib.Path(sys.argv[1])
49
- check_if_paths_exist(
50
- path,
51
- CLIPathError,
52
- "Incorrent path provided with perftester CLI command",
53
- )
54
- if path.is_dir():
55
- files = _find_perftester_files(path)
56
- elif path.is_file:
57
- files = [
58
- path,
59
- ]
60
- else:
61
- raise CLIPathError(
62
- f"Unexpected problem with path {path}, please double check"
63
- )
64
- return files
65
-
66
-
67
- def _log(message):
68
- print(message)
69
- if config.log_to_file:
70
- try:
71
- with open(config.log_file, "a") as f:
72
- f.write(message + "\n")
73
- f.flush()
74
- except OSError as e:
75
- print(
76
- f"Error in writing to {config.log_file}: {e}. "
77
- "perftester log will not be saved there."
78
- )
79
- config.log_to_file = False
80
-
81
-
82
- def _import_settings_from_config_file():
83
- settings_file = config.config_file
84
- if settings_file.exists():
85
- sys.path.append(str(settings_file.parent.absolute()))
86
- importlib.import_module("config_perftester")
87
- _log(f"Importing settings from {settings_file}.")
88
- else:
89
- _log(
90
- "No settings file detected, using default perftester configuration."
91
- )
92
-
93
-
94
- def _initialize_log_file(files_len):
95
- if config.log_to_file:
96
- try:
97
- with open(config.log_file, "w") as f:
98
- f.write("")
99
- except OSError as e:
100
- print(
101
- f"Error in writing to {config.log_file}: {e}. "
102
- "perftester log will not be saved there."
103
- )
104
- config.log_to_file = False
105
-
106
- _log(
107
- "Performance tests using perftester\n"
108
- "perftester: https://github.com/nyggus/perftester\n"
109
- "--------------------------------------------"
110
- f"\n\nCollected {files_len} "
111
- f"perftester module{'s' if files_len > 1 else ''} for testing.\n"
112
- )
113
-
114
-
115
- def _import_module(file):
116
- path_str = str(file.parent.absolute())
117
- if path_str not in sys.path:
118
- sys.path.append(path_str)
119
- module_name = file.name[:-3]
120
- module = importlib.import_module(module_name)
121
- return module, module_name
122
-
123
-
124
- def _perftester(module_name, func, *args, **kwargs):
125
- try:
126
- func(*args, **kwargs)
127
- except TimeTestError as e:
128
- _log(f"\nTimeTestError in {module_name}.{func.__name__}\n{e}")
129
- return 1
130
- except MemoryTestError as e:
131
- _log(f"\nMemoryTestError in {module_name}.{func.__name__}\n{e}")
132
- return 1
133
- except Exception as e:
134
- _log(
135
- "\nUnexpected error in test function "
136
- f"{module_name}.{func.__name__}\n{e}\n"
137
- )
138
- return 1
139
- return 0
140
-
141
-
142
- def _find_perftester_functions(module):
143
- functions = []
144
- items = dir(module)
145
- for item in items:
146
- item_real = getattr(module, item)
147
- if inspect.isfunction(item_real) and item.startswith("perftester_"):
148
- functions.append(item_real)
149
- return functions
150
-
151
-
152
- def _find_perftester_files(path: pathlib.Path):
153
- return [file for file in path.rglob("perftester_*.py")]
154
-
155
-
156
- def _log_perftester_results(test_results):
157
- passed_perftesters = test_results.passed_tests
158
- failed_perftesters = test_results.failed_tests
159
- n_of_tests = len(passed_perftesters) + len(failed_perftesters)
160
-
161
- _log(
162
- f"\n\nPerfomance testing done.\nOut of {n_of_tests} tests,"
163
- f" {len(passed_perftesters)} has passed"
164
- f" and {len(failed_perftesters)} has failed."
165
- )
166
- if len(passed_perftesters) > 0:
167
- _log("\nPassed tests:")
168
- for test in passed_perftesters:
169
- _log(test)
170
- if len(failed_perftesters) > 0:
171
- _log(f"\nFailed tests:")
172
- for test in failed_perftesters:
173
- _log(test)
174
- print()
175
-
176
-
177
- if __name__ == "__main__":
178
- main()
1
+ """Module responsible for the CLI perftester command."""
2
+ import importlib
3
+ import inspect
4
+ import os
5
+ import pathlib
6
+ import sys
7
+ from collections import namedtuple
8
+ from easycheck import check_if_paths_exist
9
+ from perftester import config, TimeTestError, MemoryTestError, CLIPathError
10
+
11
+
12
+ TestResults = namedtuple("TestResults", "passed_tests failed_tests")
13
+
14
+ config.full_traceback()
15
+
16
+
17
+ def main():
18
+ files = _read_cli_args()
19
+ _initialize_log_file(len(files))
20
+ _import_settings_from_config_file()
21
+ passed_perftesters, failed_perftesters = [], []
22
+
23
+ for file in files:
24
+ module, module_name = _import_module(file)
25
+ perftester_functions = _find_perftester_functions(module)
26
+ for func in perftester_functions:
27
+ this_test = _perftester(module_name, func)
28
+ if this_test:
29
+ failed_perftesters.append(
30
+ f"{str(module_name)}.{func.__name__}"
31
+ )
32
+ else:
33
+ passed_perftesters.append(
34
+ f"{str(module_name)}.{func.__name__}"
35
+ )
36
+
37
+ test_results = TestResults(
38
+ passed_tests=passed_perftesters, failed_tests=failed_perftesters
39
+ )
40
+ _log_perftester_results(test_results)
41
+
42
+
43
+ def _read_cli_args():
44
+ if len(sys.argv) == 1:
45
+ path = pathlib.Path(os.getcwd())
46
+ else:
47
+ # sys.argv[1] is always a string, so no need to check it
48
+ path = pathlib.Path(sys.argv[1])
49
+ check_if_paths_exist(
50
+ path,
51
+ CLIPathError,
52
+ "Incorrent path provided with perftester CLI command",
53
+ )
54
+ if path.is_dir():
55
+ files = _find_perftester_files(path)
56
+ elif path.is_file:
57
+ files = [
58
+ path,
59
+ ]
60
+ else:
61
+ raise CLIPathError(
62
+ f"Unexpected problem with path {path}, please double check"
63
+ )
64
+ return files
65
+
66
+
67
+ def _log(message):
68
+ print(message)
69
+ if config.log_to_file:
70
+ try:
71
+ with open(config.log_file, "a") as f:
72
+ f.write(message + "\n")
73
+ f.flush()
74
+ except OSError as e:
75
+ print(
76
+ f"Error in writing to {config.log_file}: {e}. "
77
+ "perftester log will not be saved there."
78
+ )
79
+ config.log_to_file = False
80
+
81
+
82
+ def _import_settings_from_config_file():
83
+ settings_file = config.config_file
84
+ if settings_file.exists():
85
+ sys.path.append(str(settings_file.parent.absolute()))
86
+ importlib.import_module("config_perftester")
87
+ _log(f"Importing settings from {settings_file}.")
88
+ else:
89
+ _log(
90
+ "No settings file detected, using default perftester configuration."
91
+ )
92
+
93
+
94
+ def _initialize_log_file(files_len):
95
+ if config.log_to_file:
96
+ try:
97
+ with open(config.log_file, "w") as f:
98
+ f.write("")
99
+ except OSError as e:
100
+ print(
101
+ f"Error in writing to {config.log_file}: {e}. "
102
+ "perftester log will not be saved there."
103
+ )
104
+ config.log_to_file = False
105
+
106
+ _log(
107
+ "Performance tests using perftester\n"
108
+ "perftester: https://github.com/nyggus/perftester\n"
109
+ "--------------------------------------------"
110
+ f"\n\nCollected {files_len} "
111
+ f"perftester module{'s' if files_len > 1 else ''} for testing.\n"
112
+ )
113
+
114
+
115
+ def _import_module(file):
116
+ path_str = str(file.parent.absolute())
117
+ if path_str not in sys.path:
118
+ sys.path.append(path_str)
119
+ module_name = file.name[:-3]
120
+ module = importlib.import_module(module_name)
121
+ return module, module_name
122
+
123
+
124
+ def _perftester(module_name, func, *args, **kwargs):
125
+ try:
126
+ func(*args, **kwargs)
127
+ except TimeTestError as e:
128
+ _log(f"\nTimeTestError in {module_name}.{func.__name__}\n{e}")
129
+ return 1
130
+ except MemoryTestError as e:
131
+ _log(f"\nMemoryTestError in {module_name}.{func.__name__}\n{e}")
132
+ return 1
133
+ except Exception as e:
134
+ _log(
135
+ "\nUnexpected error in test function "
136
+ f"{module_name}.{func.__name__}\n{e}\n"
137
+ )
138
+ return 1
139
+ return 0
140
+
141
+
142
+ def _find_perftester_functions(module):
143
+ functions = []
144
+ items = dir(module)
145
+ for item in items:
146
+ item_real = getattr(module, item)
147
+ if inspect.isfunction(item_real) and item.startswith("perftester_"):
148
+ functions.append(item_real)
149
+ return functions
150
+
151
+
152
+ def _find_perftester_files(path: pathlib.Path):
153
+ return [file for file in path.rglob("perftester_*.py")]
154
+
155
+
156
+ def _log_perftester_results(test_results):
157
+ passed_perftesters = test_results.passed_tests
158
+ failed_perftesters = test_results.failed_tests
159
+ n_of_tests = len(passed_perftesters) + len(failed_perftesters)
160
+
161
+ _log(
162
+ f"\n\nPerfomance testing done.\nOut of {n_of_tests} tests,"
163
+ f" {len(passed_perftesters)} has passed"
164
+ f" and {len(failed_perftesters)} has failed."
165
+ )
166
+ if len(passed_perftesters) > 0:
167
+ _log("\nPassed tests:")
168
+ for test in passed_perftesters:
169
+ _log(test)
170
+ if len(failed_perftesters) > 0:
171
+ _log(f"\nFailed tests:")
172
+ for test in failed_perftesters:
173
+ _log(test)
174
+ print()
175
+
176
+
177
+ if __name__ == "__main__":
178
+ main()
perftester/perftester.py CHANGED
File without changes