Repository navigation
FEAT: Profiler #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
FEAT: Profiler #552
Changes from 7 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
5e15451
FEAT: Add performance profiling infrastructure and documentation
bewithgaurav 1eb6650
DOC: Add latest status update for profiler branch
bewithgaurav 2a87987
Add profiling infrastructure with Python + C++ instrumentation
bewithgaurav 0a23cf9
my_bench and delete run_profiler
bewithgaurav 11a3c44
Merge origin/main into bewithgaurav/profiling
bewithgaurav dcb5537
CHORE: remove profiler dev scratch docs and bench script
bewithgaurav 321a22c
FIX: restore main's same-SQL param shortcut for single-container params
bewithgaurav 417a8c7
Merge origin/main into bewithgaurav/profiling
bewithgaurav cd498fa
FIX: exclude profiler/ dev tooling from the packaged wheel
bewithgaurav da6bf23
CHORE: add tests for the performance profiler
bewithgaurav 31ef66e
DOC: document the profiler counter's single-mutex design decision
bewithgaurav b837868
CHORE: gate profiler behind ENABLE_PROFILING build flag, off by default
bewithgaurav 0dbba47
DOC: rewrite profiler README as minimal from-basics docs
bewithgaurav 99e8502
Merge origin/main into bewithgaurav/profiling
bewithgaurav 1b22b8c
FIX: make profiler counter config atomic and capture timer active-state
bewithgaurav c72232c
FIX: isolate profiler measurement windows so scenarios don't contamin…
bewithgaurav 0abc283
FIX: address Copilot review — header includes, perf_stop guard, excep…
bewithgaurav f902803
FIX: address second Copilot review — timeline epoch, build.bat flag, …
bewithgaurav d3e6723
FIX: address external review — arrow scenario, scenario isolation, pe…
bewithgaurav bb75af0
FIX: make profiler timer destructor non-throwing and broaden CLI erro…
bewithgaurav c068e0e
FIX: close cursor in run_script even when the user script fails to co…
bewithgaurav 0d33882
FIX: script wall-time accuracy, narrow arrow catch, measure executema…
bewithgaurav 320f639
FIX: nanosecond accumulation + metadata timer scope in profiler
bewithgaurav 243fe03
FIX: preserve original traceback in profiler error paths
bewithgaurav f5acae9
DOCS: link the profiler guide from the top-level README
bewithgaurav d639bc9
FIX: correct README overhead claim, drop unused import, sync timeline…
bewithgaurav 9160ac4
Merge branch 'main' into bewithgaurav/profiling
gargsaumya c599232
FIX: tie profiler samples to their measurement window with a generati…
bewithgaurav 89a4cf9
Merge commit '9160ac4c2e3c72ce50830cc6ae73b3ca9c1845cd' into bewithga…
bewithgaurav 37f0985
FIX: address profiler review, timeline epoch order, phase label, scen…
bewithgaurav 6430518
FIX: Harden profiler window boundaries and failure cleanup
bewithgaurav 1277237
FIX: Preserve script execution context and guard dev-only tests
bewithgaurav 7debeb6
FIX: Make profiler recording and snapshots reentrancy-safe
bewithgaurav fd47406
Merge branch 'main' into bewithgaurav/profiling
bewithgaurav 9dbd632
Merge branch 'main' into bewithgaurav/profiling
bewithgaurav 20ca697
CHORE: Add bounded profiler finalizer regression
bewithgaurav 4759584
Merge branch 'main' into bewithgaurav/profiling
jahnvi480 a574a08
Merge branch 'main' into bewithgaurav/profiling
jahnvi480 08002bc
Merge branch 'main' into bewithgaurav/profiling
bewithgaurav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
| """ | ||
| Lightweight phase-level profiling for the Python layer. | ||
|
|
||
| Usage in cursor.py: | ||
| from mssql_python.perf_timer import perf_phase | ||
|
|
||
| with perf_phase("py::execute::param_type_detection"): | ||
| ... | ||
|
|
||
| Control from profiler script: | ||
| from mssql_python.perf_timer import enable, disable, get_stats, reset | ||
|
|
||
| Stats dict matches the C++ profiling format so both layers can be | ||
| printed with the same reporter. Entries use a "py::" prefix to | ||
| distinguish from C++ timers. | ||
| """ | ||
|
|
||
| import time | ||
| from contextlib import contextmanager | ||
|
|
||
| _enabled = False | ||
| _stats: dict[str, dict] = {} | ||
| _timeline: list[dict] = [] | ||
| _timeline_enabled = False | ||
| _epoch_ns: int = 0 | ||
|
|
||
|
|
||
| def enable(): | ||
| global _enabled | ||
| _enabled = True | ||
|
|
||
|
|
||
| def disable(): | ||
| global _enabled | ||
| _enabled = False | ||
|
|
||
|
|
||
| def is_enabled() -> bool: | ||
| return _enabled | ||
|
|
||
|
|
||
| def reset(): | ||
| _stats.clear() | ||
| _timeline.clear() | ||
|
|
||
|
|
||
| def reset_stats_only(): | ||
| _stats.clear() | ||
|
|
||
|
|
||
| def enable_timeline(): | ||
| global _timeline_enabled, _epoch_ns | ||
| _timeline_enabled = True | ||
| _epoch_ns = time.perf_counter_ns() | ||
|
|
||
|
bewithgaurav marked this conversation as resolved.
Resolved
|
||
|
|
||
| def disable_timeline(): | ||
| global _timeline_enabled | ||
| _timeline_enabled = False | ||
|
|
||
|
|
||
| def get_timeline() -> list[dict]: | ||
| return [ | ||
| { | ||
| "name": ev["name"], | ||
| "start_us": ev["start_ns"] // 1000, | ||
| "duration_us": ev["duration_ns"] // 1000, | ||
| } | ||
| for ev in _timeline | ||
| ] | ||
|
|
||
|
|
||
| def get_stats() -> dict: | ||
| out = {} | ||
| for name, s in _stats.items(): | ||
| out[name] = { | ||
| "calls": s["calls"], | ||
| "total_us": s["total_ns"] // 1000, | ||
| "min_us": s["min_ns"] // 1000, | ||
| "max_us": s["max_ns"] // 1000, | ||
| } | ||
| return out | ||
|
|
||
|
|
||
| @contextmanager | ||
| def perf_phase(name: str): | ||
| if not _enabled: | ||
| yield | ||
| return | ||
| t0 = time.perf_counter_ns() | ||
| yield | ||
| elapsed = time.perf_counter_ns() - t0 | ||
| _record(name, elapsed, t0) | ||
|
|
||
|
|
||
| def perf_start() -> int: | ||
| if not _enabled: | ||
| return 0 | ||
| return time.perf_counter_ns() | ||
|
|
||
|
|
||
| def perf_stop(name: str, t0: int): | ||
| if not _enabled: | ||
| return | ||
| _record(name, time.perf_counter_ns() - t0, t0) | ||
|
bewithgaurav marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def _record(name: str, elapsed: int, start_ns: int = 0): | ||
| entry = _stats.get(name) | ||
| if entry is None: | ||
| _stats[name] = { | ||
| "calls": 1, | ||
| "total_ns": elapsed, | ||
| "min_ns": elapsed, | ||
| "max_ns": elapsed, | ||
| } | ||
| else: | ||
| entry["calls"] += 1 | ||
| entry["total_ns"] += elapsed | ||
| if elapsed < entry["min_ns"]: | ||
| entry["min_ns"] = elapsed | ||
| if elapsed > entry["max_ns"]: | ||
| entry["max_ns"] = elapsed | ||
|
|
||
| if _timeline_enabled and start_ns: | ||
| _timeline.append( | ||
| { | ||
| "name": name, | ||
| "start_ns": start_ns - _epoch_ns, | ||
| "duration_ns": elapsed, | ||
| } | ||
| ) | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.