Skip to content

Commit 39f2a72

Browse files
committed
["tottime"] is a reasonable default for keys
1 parent 3449b0e commit 39f2a72

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

docs/config.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,27 +191,35 @@ Defaults to `false`.
191191

192192
Profiling configuration. Allows for runtime profiling of the processing.
193193
Must be one of the following:
194+
194195
* Type _boolean_.
195196
If set, profiling is enabled and output is logged using level INFO. Otherwise, profiling is disabled.
197+
196198
* Type _string_.
197199
Profile path. Enables profiling and writes a profiling report to given path.
200+
198201
* Type _object_.
199202

200203
* `enabled`:
201204
Type _boolean_.
202205
Enable or disable profiling.
206+
203207
* `path`:
204208
Type _string_.
205209
Local file path for profiling output.
210+
206211
* `log_level`:
207212
Log level. Use 'NOTSET' to disable logging.
208213
Defaults to `"INFO"`.
209214
Must be one of `"CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"`.
215+
210216
* `keys`:
211217
Type _array_.
212218
Sort output according to the supplied column names. Refer to [Stats.sort_stats(*keys)](https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats).
219+
Defaults to `["tottime"]`.
213220

214221
Must be one of `"calls", "cumulative", "cumtime", "file", "filename", "module", "ncalls", "pcalls", "line", "name", "nfl", "stdname", "time", "tottime"`.
222+
215223
* `restrictions`:
216224
Type _array_.
217225
Used to limit the list down to the significant entries in the profiling report. Refer to [Stats.print_stats(*restrictions)](https://docs.python.org/3/library/profile.html#pstats.Stats.print_stats).

tests/test_profiler.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,38 @@
1212
class ProfilerTest(unittest.TestCase):
1313
def test_disabled(self):
1414
profiler = Profiler(None)
15-
self.assertEqual(False, profiler.enabled)
15+
self.assert_profiler_config(profiler, False)
1616
with profiler:
1717
pass
1818

1919
profiler = Profiler(False)
20-
self.assertEqual(False, profiler.enabled)
20+
self.assert_profiler_config(profiler, False)
2121
with profiler:
2222
pass
2323

2424
profiler = Profiler({"enabled": False})
25-
self.assertEqual(False, profiler.enabled)
25+
self.assert_profiler_config(profiler, False)
2626
with profiler:
2727
pass
2828

2929
profiler = Profiler({"log_level": "NOTSET"})
30-
self.assertEqual(False, profiler.enabled)
30+
self.assert_profiler_config(profiler, False, "NOTSET")
31+
with profiler:
32+
pass
33+
34+
profiler = Profiler({"enabled": True, "log_level": "NOTSET"})
35+
self.assert_profiler_config(profiler, False, "NOTSET")
3136
with profiler:
3237
pass
3338

3439
def test_enabled(self):
3540
profiler = Profiler(True)
36-
self.assertEqual(True, profiler.enabled)
37-
self.assertEqual("INFO", profiler.log_level)
38-
self.assertEqual(None, profiler.path)
41+
self.assert_profiler_config(profiler, True, "INFO")
3942
with profiler:
4043
pass
4144

4245
profiler = Profiler("prof.out")
43-
self.assertEqual(True, profiler.enabled)
44-
self.assertEqual("INFO", profiler.log_level)
45-
self.assertEqual("prof.out", profiler.path)
46+
self.assert_profiler_config(profiler, True, "INFO", "prof.out")
4647
try:
4748
with profiler:
4849
pass
@@ -52,13 +53,30 @@ def test_enabled(self):
5253
os.remove("prof.out")
5354

5455
profiler = Profiler({"path": "prof.out", "log_level": "DEBUG"})
55-
self.assertEqual(True, profiler.enabled)
56-
self.assertEqual("DEBUG", profiler.log_level)
57-
self.assertEqual("prof.out", profiler.path)
56+
self.assert_profiler_config(profiler, True, "DEBUG", "prof.out")
5857
try:
5958
with profiler:
6059
pass
6160
self.assertTrue(os.path.exists("prof.out"))
6261
finally:
6362
if os.path.exists("prof.out"):
6463
os.remove("prof.out")
64+
65+
def assert_profiler_config(
66+
self,
67+
profiler: Profiler,
68+
expected_enabled,
69+
expected_log_level="INFO",
70+
expected_path=None,
71+
expected_keys=None,
72+
expected_restrictions=None,
73+
):
74+
if expected_keys is None:
75+
expected_keys = ["tottime"]
76+
if expected_restrictions is None:
77+
expected_restrictions = []
78+
self.assertEqual(expected_enabled, profiler.enabled)
79+
self.assertEqual(expected_log_level, profiler.log_level)
80+
self.assertEqual(expected_path, profiler.path)
81+
self.assertEqual(expected_keys, profiler.keys)
82+
self.assertEqual(expected_restrictions, profiler.restrictions)

zappend/config/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
"(https://docs.python.org/3/library/profile.html"
235235
"#pstats.Stats.sort_stats)."
236236
),
237+
"default": ["tottime"],
237238
"type": "array",
238239
"items": {
239240
"enum": [

zappend/profiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, profiling_config: dict[str, Any] | str | bool | None):
2626
# Defaults
2727
path = None
2828
log_level = "INFO"
29-
keys = []
29+
keys = ["tottime"]
3030
restrictions = []
3131

3232
if isinstance(profiling_config, str):

0 commit comments

Comments
 (0)