Skip to content

Commit

Permalink
Do not measure overhead on import
Browse files Browse the repository at this point in the history
  • Loading branch information
s7nfo committed Jul 2, 2024
1 parent 4a63998 commit 5f6d4aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
38 changes: 20 additions & 18 deletions python/cirron/cirron.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def __repr__(self):
)


overhead = {}


class Collector:
cirron_lib = CDLL(lib_path)
cirron_lib.start.argtypes = None
Expand All @@ -55,6 +58,23 @@ def __init__(self):
self._fd = None
self.counters = Counter()

# We try to estimate what the overhead of the collector is, taking the minimum
# of 10 runs.
global overhead
if not overhead:
collector = Collector()
for _ in range(10):
with Collector() as collector:
pass

for field, _ in Counter._fields_:
if field not in overhead:
overhead[field] = getattr(collector.counters, field)
else:
overhead[field] = min(
overhead[field], getattr(collector.counters, field)
)

def __enter__(self):
ret_val = Collector.cirron_lib.start()
if ret_val == -1:
Expand All @@ -80,21 +100,3 @@ def __exit__(self, exc_type, exc_value, traceback):
)
else:
setattr(self.counters, field, 0)


# We try to estimate what the overhead of the collector is, taking the minimum
# of 10 runs.
overhead = {}
collector = Collector()
o = {}
for _ in range(10):
with Collector() as collector:
pass

for field, _ in Counter._fields_:
if field not in overhead:
o[field] = getattr(collector.counters, field)
else:
o[field] = min(overhead[field], getattr(collector.counters, field))
overhead = o
del collector
7 changes: 6 additions & 1 deletion python/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import unittest
import os

from cirron import Tracer, Collector


class Test(unittest.TestCase):
def test_tracer(self):
with Tracer() as t:
print(0)

self.assertEqual(len(t.trace), 3)

@unittest.skipIf("GITHUB_ACTIONS" in os.environ, "As of 02/07/2024, GitHub Actions does not support perf_event_open.")
@unittest.skipIf(
"GITHUB_ACTIONS" in os.environ,
"As of 02/07/2024, GitHub Actions does not support perf_event_open.",
)
def test_collector(self):
with Collector() as c:
print(0)
Expand Down

0 comments on commit 5f6d4aa

Please sign in to comment.