• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

reactive-firewall-org / multicast / 16954025989

14 Aug 2025 02:00AM UTC coverage: 97.128%. Remained the same
16954025989

push

github

reactive-firewall
[HOTFIX] Fix for CI-CD regression

Changes in file .github/workflows/CI-CHGLOG.yml:
 * hotfix for regression of download-artifacts action logic after version bump

127 of 133 branches covered (95.49%)

Branch coverage included in aggregate %.

2173 of 2235 relevant lines covered (97.23%)

6.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/tests/profiling.py
1
#! /usr/bin/env python
2
# -*- coding: utf-8 -*-
3

4
# Python Test Repo Template
5
# ..................................
6
# Copyright (c) 2017-2025, Mr. Walls
7
# ..................................
8
# Licensed under MIT (the "License");
9
# you may not use this file except in compliance with the License.
10
# You may obtain a copy of the License at
11
# ..........................................
12
# https://github.com/reactive-firewall/python-repo/blob/HEAD/LICENSE.md
13
# ..........................................
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS,
16
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
# See the License for the specific language governing permissions and
18
# limitations under the License.
19

20
# Third-party Acknowledgement:
21
# ..........................................
22
# Some code (namely: class timewith, @do_cprofile, @do_line_profile) was modified/derived from:
23
# https://github.com/zapier/profiling-python-like-a-boss/tree/1ab93a1154
24
# Copyright (c) 2013, Zapier Inc. All rights reserved.
25
# which was under BSD-3 Clause license.
26
# see https://github.com/zapier/profiling-python-like-a-boss/blob/1ab93a1154/LICENSE.md for details
27
# ..........................................
28
# NO ASSOCIATION
29

30
__module__ = "tests.profiling"
4✔
31
"""This is pythonrepo testing module Template."""
4✔
32

33
try:
4✔
34
        import sys
4✔
35
        if not hasattr(sys, 'modules') or not sys.modules:  # pragma: no branch
36
                raise ModuleNotFoundError(
37
                        "[CWE-440] OMG! sys.modules is not available or empty.",
38
                ) from None
39
except ImportError as _cause:
40
        raise ImportError("[CWE-440] Unable to import sys module.") from _cause
41

42
try:
4✔
43
        if 'os' not in sys.modules:
44
                import os
45
        else:  # pragma: no branch
46
                os = sys.modules["os"]
47
except Exception as _cause:  # pragma: no branch
48
        baton = ModuleNotFoundError(_cause, str("[CWE-758] Test module failed completely."))
49
        baton.module = __module__
50
        baton.path = __file__
51
        baton.__cause__ = _cause
52
        raise baton from _cause
53

54
try:
4✔
55
        if 'functools' not in sys.modules:
56
                import functools
57
        else:  # pragma: no branch
58
                functools = sys.modules["functools"]
59
except Exception as _cause:  # pragma: no branch
60
        baton = ModuleNotFoundError(_cause, str("[CWE-758] Test module failed completely."))
61
        baton.module = __module__
62
        baton.path = __file__
63
        baton.__cause__ = _cause
64
        raise baton from _cause
65

66
try:
4✔
67
        import time
4✔
68
        if time.__name__ is None:  # pragma: no branch
4✔
69
                raise NotImplementedError("[CWE-440] We could not import time. Are we in the speed-force!")
70
except Exception as _cause:  # pragma: no branch
71
        baton = ImportError(_cause, str("[CWE-758] Test module failed completely."))
72
        baton.module = __module__
73
        baton.path = __file__
74
        baton.__cause__ = _cause
75
        raise baton from _cause
76

77
try:
4✔
78
        if 'cProfile' not in sys.modules:
79
                import cProfile
80
        else:  # pragma: no branch
81
                cProfile = sys.modules["cProfile"]
82
except Exception as _cause:  # pragma: no branch
83
        baton = ModuleNotFoundError(_cause, str("[CWE-758] Test module failed completely."))
84
        baton.module = __module__
85
        baton.path = __file__
86
        baton.__cause__ = _cause
87
        raise baton from _cause
88

89
try:
4✔
90
        try:
4✔
91
                sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), str('..'))))
4✔
92
                sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), str('.'))))
4✔
93
        except Exception as _root_cause:  # pragma: no branch
94
                raise ImportError(
95
                        _root_cause, str("[CWE-758] Profile module failed completely."),
96
                ) from _root_cause
97
except Exception as _cause:  # pragma: no branch
98
        baton = ImportError(_cause, str("[CWE-758] Test module failed completely."))
99
        baton.module = __module__
100
        baton.path = __file__
101
        baton.__cause__ = _cause
102
        raise baton from _cause
103

104

105
class timewith():
4✔
106
        """Basic timer for do_time_profile."""
107

108
        def __init__(self, name=""):
4✔
109
                self.name = name
4✔
110
                self.start = time.time()
4✔
111

112
        @property
4✔
113
        def elapsed(self):
4✔
114
                return time.time() - self.start
4✔
115

116
        def checkpoint(self, name=""):
4✔
117
                print(
4✔
118
                        str(f"{self.name} {name} took {self.elapsed} seconds").strip()
119
                )
120

121
        def __enter__(self):
4✔
122
                return self
4✔
123

124
        def __exit__(self, type, value, traceback):  # skipcq: PYL-W0622
125
                self.checkpoint(str("finished"))
126

127

128
def do_time_profile(func, timer_name="time_profile"):
4✔
129
        """Run a function with a timer.
130

131
        Time Testing:
132

133
                First some test fixtures:
134

135
                >>> import tests.context as context
136
                >>> from context import profiling as profiling
137
                >>>
138

139
        Testcase 0: test the time_profile.
140

141
                >>> def doWork():
142
                ...    \"""Does some work.\"""
143
                ...    for i in range(0, 42):
144
                ...        print(str("Do Task {}").format(int(i)))
145
                >>>
146
                >>> profiling.do_time_profile(
147
                ...    doWork,
148
                ...    timer_name=str("work time test")
149
                ... )() #doctest: -DONT_ACCEPT_BLANKLINE, +ELLIPSIS
150
                work...Start Timer...
151
                ...Do Task...
152
                work...Stop Timer...
153
                work...took ... seconds
154
                >>>
155

156
        """
157
        @functools.wraps(func)
4✔
158
        def timer_profile_func(*args, **kwargs):
4✔
159
                """Wraps a function in timewith() function."""
160
                theOutput = None
4✔
161
                with timewith(timer_name) as timer:
4✔
162
                        timer.checkpoint(str("Start Timer"))
4✔
163
                        theOutput = func(*args, **kwargs)
4✔
164
                        timer.checkpoint(str("Stop Timer"))
4✔
165
                return theOutput
4✔
166
        return timer_profile_func
4✔
167

168

169
def do_cprofile(func):
4✔
170
        """Use built-in profiler to profile.
171

172
        Time Testing:
173

174
                First some test fixtures:
175

176
                >>> import tests.context as context
177
                >>> from context import profiling as profiling
178
                >>>
179

180
        Testcase 0: test the time_profile.
181

182
                >>> def doWork():
183
                ...    \"""Does some work.\"""
184
                ...    for i in range(0, 42):
185
                ...        print(str("Do Task {}").format(int(i)))
186
                >>>
187
                >>> profiling.do_cprofile(
188
                ...    doWork
189
                ... )() #doctest: -DONT_ACCEPT_BLANKLINE, +ELLIPSIS
190
                Do Task 0...Do Task 10...Do Task 20...Do Task 30...Do Task 40...
191
                ...function calls in ... seconds...Ordered by: standard name...
192
                ...ncalls  tottime  percall  cumtime  percall filename:lineno(function)...
193
                ...<...>:1(doWork)...{built-in method builtins.print}...
194
                <BLANKLINE>
195
                <BLANKLINE>
196
                >>>
197

198
        """
199
        @functools.wraps(func)
4✔
200
        def profiled_func(*args, **kwargs):
4✔
201
                """Wraps a function in profile.enable/disable() functions."""
202
                profile = cProfile.Profile()
4✔
203
                try:
4✔
204
                        profile.enable()
4✔
205
                        result = func(*args, **kwargs)
4✔
206
                        profile.disable()
4✔
207
                        return result
4✔
208
                finally:
209
                        profile.print_stats()
4✔
210
        return profiled_func
4✔
211

212

213
try:  # noqa
4✔
214
        from line_profiler import LineProfiler
4✔
215

216
        def do_profile(follow=None):  # pragma: no cover
217
                if follow is None:
218
                        follow = []
219

220
                def inner(func):
221
                        def profiled_func(*args, **kwargs):
222
                                try:
223
                                        profiler = LineProfiler()
224
                                        profiler.add_function(func)
225
                                        for f in follow:
226
                                                profiler.add_function(f)
227
                                        profiler.enable_by_count()
228
                                        return func(*args, **kwargs)
229
                                finally:
230
                                        profiler.print_stats()
231
                        return profiled_func
232
                return inner
233

234
except ImportError:  # pragma: no cover
235
        def do_profile(follow=None):
236
                "Helpful if you accidentally leave in production!"
237
                if follow is None:
238
                        follow = []
239

240
                def inner(func):
241
                        def nothing(*args, **kwargs):
242
                                return func(*args, **kwargs)
243
                        return nothing
244
                return inner
245

246

247
def main(*argv):  # pragma: no cover
248
        """The Main Event makes no sense to profiling."""
249
        raise NotImplementedError("CRITICAL - test profiling main() not implemented. yet?") from None
250

251

252
if __name__ in '__main__':  # pragma: no cover
253
        exitcode = 2
254
        try:
255
                exitcode = main(sys.argv[1:])
256
        finally:
257
                exit(exitcode)  # skipcq: PYL-R1722 - intentionally allow overwriteing exit for testing
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc